1

I have Resque Scheduler running with a Rails 4 app and I'm finding it impossible to cancel queued jobs. I have the following method in my controller:

class Employers::SocialMediaPostsController < ApplicationController
    def cancel
        post = SocialMediaPost.find(params[:id])

        respond_to do |format|
            if post.cancel_post && post.destroy
                format.json { render json: {}, status: :ok }
            else
                format.json { render json: {}, status: :bad_request }
            end
        end
    end
end

Then in my SocialMediaPost model I have the following method:

def cancel_post
    success = false
    if self.scheduled_time > DateTime.now
        options = {
            id: self.id,
            services: JSON.parse(self.services),
            posts: JSON.parse(self.posts),
            attachment_url: self.attachment_url,
            media_url: self.media_url,
            time: self.scheduled_time.in_time_zone("Eastern Time (US & Canada)")
        }
        success = Resque.remove_delayed(Postman, options) > 0
    end
    success
end

Here is my Postman worker:

class Postman
    @queue = :social_media_posts

    def self.perform(options)
        post = SocialMediaPost.find(options['id'])
        post.post_tweet if options['services']['twitter'] && options['attachment_url'].blank?
        post.post_tweet_with_media if options['services']['twitter'] && options['attachment_url'].present?
        post.post_facebook_status if options['services']['facebook']
        post.post_linkedin_status if options['services']['linkedin']
    end
end

When I call Employers::SocialMediaPostsController#cancel it successfully calls the cancel method on my post and then destroys it, however if I check the Delayed tab in the scheduler, I still see my post queued up.

If I open a console session and call the following code to manually delete the post, it works.

post = SocialMediaPost.find(id)
post.cancel_post

What am I missing here? It works from a console session, but not when I call my API endpoint.

Daniel Bonnell
  • 4,817
  • 9
  • 48
  • 88
  • Did you ever find out how to do this? I am having the same problem with the latest stable versions and it appears to be intermittent. For the most part it doesn't find my delayed item to remove but once in awhile it does and it removes it. I've gotten around this by coding my job's execution to handle the case where it is not removed but should have been. – gwnp Jul 22 '16 at 05:52
  • I never figured out how to solve it directly. I ended up just working around it by deleting the post. This causes `Resque` to throw an error when it can't find the post, which isn't ideal, but doesn't affect the end user. – Daniel Bonnell Jul 22 '16 at 16:16

0 Answers0