Ruby 如何对rufus调度程序作业排队
我有如下几点:Ruby 如何对rufus调度程序作业排队,ruby,rufus-scheduler,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 l
# 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的输出是一堆“嘿,我在循环中”
发生的情况是,我退出到命令行提示符,却从未看到循环运行。您怎么能期望
def loop
"hey I am i the loop"
end
要将任何内容输出到标准输出(非标准)?它只是返回一个字符串,而不是调用print
或put
# 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调度程序实例中的线程)活动并执行它们的工作。您的初始脚本只是退出,释放了所有资源,正如预期的那样。我认为这是一个输入错误,您不需要最后一个结尾。如果不使用,为什么要使用paramsurl
count
方法呢?这些都是在命令行中传递的,我只是没有显式地调用ARGV。哦,是的,你是对的,我的实际脚本使用puts,但是重写抽象我排除了它…但是你击中了主要问题,那就是$schedule.join…所以我认为你解决了我的问题让我试试!对这很有效,谢谢。我希望这已经在示例或文档中了。我不知道如何使用$schedule和join
…您是如何根据文档了解到这一点的?文档中包含了join:(quickstart.rb top first code example)还有,作为程序员,您应该了解,当程序退出时,其线程也随之死亡。我写了rufus scheduler,所以我知道它的文档。为了让程序员熟悉,“连接”只是遵循线程连接模式。因此,在抱怨“我希望这是在文件中”之前,请阅读文档。不要把责任推到别人身上。