如何排队 rufus-scheduler 作业
How to queue rufus-scheduler jobs
我有如下内容:
# myScript.rb
require 'rufus-scheduler'
def loop
"hey I am i the loop"
end
def run_schedule(url, count, method, interval)
puts "running scheduler"
scheduler = Rufus::Scheduler.new
scheduler.every interval do
loop(url, count, method)
end
end
run_schedule(url, count, method, interval)
我的期望是当我 运行:
bundle exec ruby myScript.rb url, count, method, interval
我看到根据间隔向 STD 输出一堆 "hey I am in the loop"。
发生的事情是我退出到命令行提示符并且再也看不到循环 运行。
你怎么能期待
def loop
"hey I am i the loop"
end
将任何内容输出到 stdout(不是 STD)?它只是返回一个字符串,而不是调用 print
或 puts
...
# myScript.rb
require 'rufus-scheduler'
def _loop(u, c, m)
# "loop" is a bad name, it's a Ruby keyword, so using "_loop" instead
# it's still a bad name
p "hey I am i the loop"
p [ Time.now, [ u, c, m ] ]
# without p or puts nothing gets to stdout
end
$scheduler = Rufus::Scheduler.new
# creating a single scheduler for the whole script
# not creating a new scheduler each time run_schedule is called
def run_schedule(url, count, method, interval)
#puts "running scheduler"
#scheduler = Rufus::Scheduler.new
# commenting out...
$scheduler.every interval do
_loop(url, count, method)
end
end
#run_schedule(url, count, method, interval)
run_schedule('url', 'count', 'method', '3s')
$scheduler.join
# let the Ruby main thread join the scheduler thread so that
# the Ruby process does not exit and so scheduling may happen
所以它是这样的:
"hey I am i the loop"
[2017-02-08 06:06:01 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:05 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:08 +0900, ["url", "count", "method"]]
注意脚本末尾的 $scheduler.join
。这会阻止 Ruby 进程退出。由于该进程不存在,其中的线程(在我们的例子中,rufus-scheduler 实例中的线程)存在并执行它们的工作。正如预期的那样,您的初始脚本只是退出,释放了所有资源。
我有如下内容:
# myScript.rb
require 'rufus-scheduler'
def loop
"hey I am i the loop"
end
def run_schedule(url, count, method, interval)
puts "running scheduler"
scheduler = Rufus::Scheduler.new
scheduler.every interval do
loop(url, count, method)
end
end
run_schedule(url, count, method, interval)
我的期望是当我 运行:
bundle exec ruby myScript.rb url, count, method, interval
我看到根据间隔向 STD 输出一堆 "hey I am in the loop"。
发生的事情是我退出到命令行提示符并且再也看不到循环 运行。
你怎么能期待
def loop
"hey I am i the loop"
end
将任何内容输出到 stdout(不是 STD)?它只是返回一个字符串,而不是调用 print
或 puts
...
# myScript.rb
require 'rufus-scheduler'
def _loop(u, c, m)
# "loop" is a bad name, it's a Ruby keyword, so using "_loop" instead
# it's still a bad name
p "hey I am i the loop"
p [ Time.now, [ u, c, m ] ]
# without p or puts nothing gets to stdout
end
$scheduler = Rufus::Scheduler.new
# creating a single scheduler for the whole script
# not creating a new scheduler each time run_schedule is called
def run_schedule(url, count, method, interval)
#puts "running scheduler"
#scheduler = Rufus::Scheduler.new
# commenting out...
$scheduler.every interval do
_loop(url, count, method)
end
end
#run_schedule(url, count, method, interval)
run_schedule('url', 'count', 'method', '3s')
$scheduler.join
# let the Ruby main thread join the scheduler thread so that
# the Ruby process does not exit and so scheduling may happen
所以它是这样的:
"hey I am i the loop"
[2017-02-08 06:06:01 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:05 +0900, ["url", "count", "method"]]
"hey I am i the loop"
[2017-02-08 06:06:08 +0900, ["url", "count", "method"]]
注意脚本末尾的 $scheduler.join
。这会阻止 Ruby 进程退出。由于该进程不存在,其中的线程(在我们的例子中,rufus-scheduler 实例中的线程)存在并执行它们的工作。正如预期的那样,您的初始脚本只是退出,释放了所有资源。