简体   繁体   English

如何手动运行发条作业?

[英]How can I run a Clockwork job manually?

Title says it all really. 标题确实说明了一切。

Is it possible to run Clockwork jobs manually? 可以手动运行Clockwork作业吗? Eg if I have a clock.rb file 例如,如果我有一个clock.rb文件

module Clockwork
  every(15.minutes, 'api.sync_shifts') do
    Shift.sync
  end

  on(:after_run) do |event, t|
    REDIS.hset("clockwork:last_run", event.job, t)
  end
end

I'd like to be able to call something like Clockwork.run('api.sync_shifts') . 我希望能够调用Clockwork.run('api.sync_shifts')

I looked through the clockwork source and didn't see anything useful. 我浏览了发条的来源,没有发现任何有用的信息。 Obviously, I can run, in this example, the Shift.sync method manually, but then I'd lose my after_run/before_run callbacks that I'm using to monitor my job statuses. 显然,在此示例中,我可以手动运行Shift.sync方法,但是随后,我将失去用于监视作业状态的after_run / before_run回调。

No, this is not possible. 不,这是不可能的。

This is the relevant method in the clockwork gem that handles callbacks and runs configured events: 这是发条gem中用于处理回调并运行已配置事件的相关方法

def tick(t=Time.now)
  if (fire_callbacks(:before_tick))
    events = events_to_run(t)
    events.each do |event|
      if (fire_callbacks(:before_run, event, t))
        event.run(t)
        fire_callbacks(:after_run, event, t)
      end
    end
  end
  fire_callbacks(:after_tick)
  events
end

This method loads all events that should run at the current time (returned from the events_to_run method). 此方法加载当前应运行的所有事件(从events_to_run方法返回)。 It iterates over all events in events_to_run and fires callbacks and runs the event . 它遍历events_to_run所有事件并触发回调并运行event

To be able to run individual events and still have all callbacks fired, this method would need to change to something like this: 为了能够运行单个事件并仍然触发所有回调,此方法需要更改为以下形式:

def tick(t=Time.now)
  if (fire_callbacks(:before_tick))
    events = events_to_run(t)
    events.each do |event|
      run_event_with_callbacks(event)
    end
  end
  fire_callbacks(:after_tick)
  events
end

def run_event_with_callbacks(event)
  if (fire_callbacks(:before_run, event, t))
    event.run(t)
      fire_callbacks(:after_run, event, t)
    end
  end
end

That would allow running individual events with firing callbacks. 这将允许使用触发回调运行单个事件。 Furthermore, you would need a method to load an individual event. 此外,您将需要一种方法来加载单个事件。 Perhaps something like this: 也许是这样的:

def load_event(job)
  # finds only the first event when there are multiple 
  # events registered for the same job
  @events.find { |event| event.job == job }
end

But since the events are registered in a Manager , you would need an instance of a manager which is correctly initialized with all events defined in the config. 但是,由于事件是在Manager中注册的,因此您将需要使用配置中定义的所有事件正确初始化的Manager实例。

To keep a long story short: This is not possible at the moment and it would need many changes to the structure of the code to make that possible. 长话短说:目前尚无法实现,并且需要对代码结构进行许多更改才能实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM