1

我让 Resque Scheduler 与 Rails 4 应用程序一起运行,但我发现无法取消排队的作业。我的控制器中有以下方法:

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

然后在我的SocialMediaPost模型中,我有以下方法:

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

这是我的Postman工人:

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

当我调用Employers::SocialMediaPostsController#cancel它成功调用cancel我的帖子上的方法然后销毁它时,但是如果我检查Delayed调度程序中的选项卡,我仍然看到我的帖子排队。

如果我打开一个控制台会话并调用以下代码手动删除帖子,它就可以工作。

post = SocialMediaPost.find(id)
post.cancel_post

我在这里想念什么?它在控制台会话中有效,但在我调用 API 端点时无效。

4

0 回答 0