首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将rufus-scheduler作业排队

如何将rufus-scheduler作业排队
EN

Stack Overflow用户
提问于 2017-02-08 03:06:29
回答 1查看 428关注 0票数 1

我有一些类似以下的东西:

代码语言:javascript
复制
# 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)

我的期望是当我运行时:

代码语言:javascript
复制
bundle exec ruby myScript.rb url, count, method, interval

我看到STD的输出是基于间隔的一堆“嘿,我在循环中”。

发生的情况是,我退出到命令行提示符,并且再也看不到循环运行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 05:04:31

你怎么能期望

代码语言:javascript
复制
def loop
  "hey I am i the loop"
end 

输出任何东西到stdout (不是STD)?它只是返回一个字符串,而不是调用printputs...

代码语言:javascript
复制
# 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

所以它是这样的:

代码语言:javascript
复制
"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调度器实例中的线程)存活并执行它们的工作。正如预期的那样,您的初始脚本只是退出,释放了它的所有资源。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42097927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档