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.