使用Sucker Punch with Active Job,有没有办法取消排队的作业?

时间:2016-03-30 17:33:03

标签: ruby-on-rails rails-activejob sucker-punch

所以我有 MyJob.perform_in(60, @user), 这将在60秒内完成我的工作。

如果再次运行这行代码替换队列,我想取消此作业。

我没有运气研究。

2 个答案:

答案 0 :(得分:0)

获取与SuckerPunch作业相关的统计数据

[14] pry(main)> SuckerPunch::Queue.stats
{
    "CreateVVLinkJob" => {
        "workers" => {
            "total" => 1,
             "busy" => 0,
             "idle" => 1
        },
           "jobs" => {
            "processed" => 1,
               "failed" => 0,
             "enqueued" => 0
        }
    }
}

并清除以前的工作

[24] pry(main)> SuckerPunch::Queue.clear
[
    [0] #<SuckerPunch::Queue:0x0000000b150da0 @__lock__=#<Mutex:0x0000000b150d50>, @__condition__=#<Thread::ConditionVariable:0x0000000b150d28>, @running=false, @name="CreateVVLinkJob", @pool=#<Concurrent::ThreadPoolExecutor:0x0000000b146ad0 @__lock__=#<Mutex:0x0000000b1469e0>, @__condition__=#<Thread::ConditionVariable:0x0000000b1469b8>, @min_length=2, @max_length=2, @idletime=60, @max_queue=0, @fallback_policy=:abort, @auto_terminate=false, @pool=[], @ready=[], @queue=[], @scheduled_task_count=1, @completed_task_count=1, @largest_length=1, @ruby_pid=22314, @gc_interval=30, @next_gc_time=25973.834404648, @StopEvent=#<Concurrent::Event:0x0000000b1468c8 @__lock__=#<Mutex:0x0000000b146878>, @__condition__=#<Thread::ConditionVariable:0x0000000b146850>, @set=true, @iteration=0>, @StoppedEvent=#<Concurrent::Event:0x0000000b1467d8 @__lock__=#<Mutex:0x0000000b146788>, @__condition__=#<Thread::ConditionVariable:0x0000000b146760>, @set=true, @iteration=0>>>
]

希望这很有用!

答案 1 :(得分:0)

SuckerPunch框架内部没有内置方法,我可以从源代码中看到取消单个排队作业或当前正在执行的作业。看来清除作业队列是一个全有或全无的功能。

也就是说,添加一个查询底层ConcurrentTask框架的扩展方法并根据@user参数的值将新作业与已经排队的作业匹配应该是一件小事。