Rails 5: delete an ActiveJob before it gets performed











up vote
0
down vote

favorite












I have an ActiveJob that changes a record's status field from "pending" to "live" X minutes after it's been created. It works well.



When the user edits the record within the X minutes, I need to push back that status change, viz. restart the clock. Presumably I'd do this by cancelling the ActiveJob and creating a new one. The Rails Guides for ActiveJob don't mention how this should be done.



I see that I can assign the ActiveJob to a variable like this:



j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)


and using byebug immediately after this line (in the controller), I see that it outputs like this:



#<ThingLiveJob:0x000000134a5fc0 @arguments=[#<Thing id: 1095, body: "Whatever", user_id: 1, created_at: "2018-11-19 10:34:24", updated_at: "2018-11-19 10:34:24", status: "pending">], @job_id="aab28c66-f8b4-491e-9c7f-af6f27aa482e", @queue_name="default", @priority=nil, @executions=0, @scheduled_at=1542623724.674397, @provider_job_id="61c154d5-9c1b-45a1-8487-824a4412c53c">


However, at the console, neither of these (guesses) work:



j.destroy

j.delete


So how should I go about it?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have an ActiveJob that changes a record's status field from "pending" to "live" X minutes after it's been created. It works well.



    When the user edits the record within the X minutes, I need to push back that status change, viz. restart the clock. Presumably I'd do this by cancelling the ActiveJob and creating a new one. The Rails Guides for ActiveJob don't mention how this should be done.



    I see that I can assign the ActiveJob to a variable like this:



    j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)


    and using byebug immediately after this line (in the controller), I see that it outputs like this:



    #<ThingLiveJob:0x000000134a5fc0 @arguments=[#<Thing id: 1095, body: "Whatever", user_id: 1, created_at: "2018-11-19 10:34:24", updated_at: "2018-11-19 10:34:24", status: "pending">], @job_id="aab28c66-f8b4-491e-9c7f-af6f27aa482e", @queue_name="default", @priority=nil, @executions=0, @scheduled_at=1542623724.674397, @provider_job_id="61c154d5-9c1b-45a1-8487-824a4412c53c">


    However, at the console, neither of these (guesses) work:



    j.destroy

    j.delete


    So how should I go about it?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an ActiveJob that changes a record's status field from "pending" to "live" X minutes after it's been created. It works well.



      When the user edits the record within the X minutes, I need to push back that status change, viz. restart the clock. Presumably I'd do this by cancelling the ActiveJob and creating a new one. The Rails Guides for ActiveJob don't mention how this should be done.



      I see that I can assign the ActiveJob to a variable like this:



      j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)


      and using byebug immediately after this line (in the controller), I see that it outputs like this:



      #<ThingLiveJob:0x000000134a5fc0 @arguments=[#<Thing id: 1095, body: "Whatever", user_id: 1, created_at: "2018-11-19 10:34:24", updated_at: "2018-11-19 10:34:24", status: "pending">], @job_id="aab28c66-f8b4-491e-9c7f-af6f27aa482e", @queue_name="default", @priority=nil, @executions=0, @scheduled_at=1542623724.674397, @provider_job_id="61c154d5-9c1b-45a1-8487-824a4412c53c">


      However, at the console, neither of these (guesses) work:



      j.destroy

      j.delete


      So how should I go about it?










      share|improve this question













      I have an ActiveJob that changes a record's status field from "pending" to "live" X minutes after it's been created. It works well.



      When the user edits the record within the X minutes, I need to push back that status change, viz. restart the clock. Presumably I'd do this by cancelling the ActiveJob and creating a new one. The Rails Guides for ActiveJob don't mention how this should be done.



      I see that I can assign the ActiveJob to a variable like this:



      j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)


      and using byebug immediately after this line (in the controller), I see that it outputs like this:



      #<ThingLiveJob:0x000000134a5fc0 @arguments=[#<Thing id: 1095, body: "Whatever", user_id: 1, created_at: "2018-11-19 10:34:24", updated_at: "2018-11-19 10:34:24", status: "pending">], @job_id="aab28c66-f8b4-491e-9c7f-af6f27aa482e", @queue_name="default", @priority=nil, @executions=0, @scheduled_at=1542623724.674397, @provider_job_id="61c154d5-9c1b-45a1-8487-824a4412c53c">


      However, at the console, neither of these (guesses) work:



      j.destroy

      j.delete


      So how should I go about it?







      ruby-on-rails rails-activejob






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      MSC

      1,58621526




      1,58621526
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I've solved similar situations in my projects as follows... I queue the active_job after every record update or create, but I call it like:



          ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)


          Then in the activejob code...



          def perform(thing_id, thing_updated_at)
          thing = Thing.find(thing_id)
          return if thing_updated_at != thing.updated_at
          ...
          end


          So essentially I let the active job run, but the active job checks that the thing object hasn't been updated since the current active job was queued.






          share|improve this answer





















          • Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
            – MSC
            2 days ago










          • I'm doing same.
            – Pavel Mikhailyuk
            2 days ago










          • I am getting "Unsupported argument type: Time". I need to read the docs again...
            – MSC
            yesterday










          • I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
            – MSC
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373100%2frails-5-delete-an-activejob-before-it-gets-performed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          I've solved similar situations in my projects as follows... I queue the active_job after every record update or create, but I call it like:



          ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)


          Then in the activejob code...



          def perform(thing_id, thing_updated_at)
          thing = Thing.find(thing_id)
          return if thing_updated_at != thing.updated_at
          ...
          end


          So essentially I let the active job run, but the active job checks that the thing object hasn't been updated since the current active job was queued.






          share|improve this answer





















          • Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
            – MSC
            2 days ago










          • I'm doing same.
            – Pavel Mikhailyuk
            2 days ago










          • I am getting "Unsupported argument type: Time". I need to read the docs again...
            – MSC
            yesterday










          • I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
            – MSC
            yesterday















          up vote
          2
          down vote



          accepted










          I've solved similar situations in my projects as follows... I queue the active_job after every record update or create, but I call it like:



          ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)


          Then in the activejob code...



          def perform(thing_id, thing_updated_at)
          thing = Thing.find(thing_id)
          return if thing_updated_at != thing.updated_at
          ...
          end


          So essentially I let the active job run, but the active job checks that the thing object hasn't been updated since the current active job was queued.






          share|improve this answer





















          • Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
            – MSC
            2 days ago










          • I'm doing same.
            – Pavel Mikhailyuk
            2 days ago










          • I am getting "Unsupported argument type: Time". I need to read the docs again...
            – MSC
            yesterday










          • I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
            – MSC
            yesterday













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          I've solved similar situations in my projects as follows... I queue the active_job after every record update or create, but I call it like:



          ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)


          Then in the activejob code...



          def perform(thing_id, thing_updated_at)
          thing = Thing.find(thing_id)
          return if thing_updated_at != thing.updated_at
          ...
          end


          So essentially I let the active job run, but the active job checks that the thing object hasn't been updated since the current active job was queued.






          share|improve this answer












          I've solved similar situations in my projects as follows... I queue the active_job after every record update or create, but I call it like:



          ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)


          Then in the activejob code...



          def perform(thing_id, thing_updated_at)
          thing = Thing.find(thing_id)
          return if thing_updated_at != thing.updated_at
          ...
          end


          So essentially I let the active job run, but the active job checks that the thing object hasn't been updated since the current active job was queued.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          SteveTurczyn

          25.6k42738




          25.6k42738












          • Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
            – MSC
            2 days ago










          • I'm doing same.
            – Pavel Mikhailyuk
            2 days ago










          • I am getting "Unsupported argument type: Time". I need to read the docs again...
            – MSC
            yesterday










          • I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
            – MSC
            yesterday


















          • Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
            – MSC
            2 days ago










          • I'm doing same.
            – Pavel Mikhailyuk
            2 days ago










          • I am getting "Unsupported argument type: Time". I need to read the docs again...
            – MSC
            yesterday










          • I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
            – MSC
            yesterday
















          Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
          – MSC
          2 days ago




          Sounds good. I'll see whether anyone else has a better idea overnight and come back to you in the morning.
          – MSC
          2 days ago












          I'm doing same.
          – Pavel Mikhailyuk
          2 days ago




          I'm doing same.
          – Pavel Mikhailyuk
          2 days ago












          I am getting "Unsupported argument type: Time". I need to read the docs again...
          – MSC
          yesterday




          I am getting "Unsupported argument type: Time". I need to read the docs again...
          – MSC
          yesterday












          I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
          – MSC
          yesterday




          I needed to convert both parameters to strings to get this to work: ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id.to_s, @thing.updated_at.to_i.to_s)
          – MSC
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373100%2frails-5-delete-an-activejob-before-it-gets-performed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith