Как вы можете ожидать
def loop
"hey I am i the loop"
end
выводить что-либо на стандартный вывод (не ЗППП)? Он просто возвращает строку, не вызывая 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) живут и выполняют свою работу. Ваш первоначальный скрипт просто вышел, освободив все его ресурсы, как и ожидалось.
Я считаю, что это опечатка, вам не нужен последний «конец». Почему у вас есть метод 'url'' count '' params, если вы не используете? – MurifoX
те передаются в командной строке, я просто не вызывал явно ARGV. – Angela