我试图设置一个Sidekiq调度程序来运行定期工作。
我可以在Rails控制台或Rails应用程序中成功运行Sidekiq任务。我可以让Whenever触发命令。然而,我无法让Whenever触发Sidekiq任务。
我在不同的机器上复制了我的工作,并且在一些机器上我使其正常工作。在大多数其他机器上,它无法正常工作。
这是我的设置:
# app/workers/create_random_product.rb
class CreateRandomProduct
include Sidekiq::Worker
def perform
new_product = Product.new
new_product.name = "Product #{Time.now}"
new_product.price = 5.5
new_product.save
end
end
# config/schedule.rb
every 1.minute do
runner "CreateRandomProduct.perform_async"
command "echo 'hello' >> /home/vagrant/output.txt"
end
如您在schedule.rb中所见,我可以运行第二个命令,因为我能看到输出已更新,但第一个runner命令似乎没有任何操作。Sidekiq仪表板也没有显示任何活动。
运行“whenever”会返回以下cron作业:
* * * * * /bin/bash -l -c 'cd /home/vagrant/sidekiqdemo && bin/rails runner -e production '\''CreateRandomProduct.perform_async'\'''
* * * * * /bin/bash -l -c 'echo '\''hello'\'' >> /home/vagrant/output.txt'
我已经手动运行了第一条命令,并且它确实触发了Sidekiq作业。
我执行了“whenever -i”命令来更新crontab文件。我检查了crontab日志,发现它尝试触发Sidekiq作业:
Oct 16 16:15:01 vagrant-ubuntu-trusty-64 CRON[13062]: (vagrant) CMD (/bin/bash -l -c 'cd /home/vagrant/sidekiqdemo && bin/rails runner -e production '\''CreateRandomProduct.perform_async'\''')
Oct 16 16:15:01 vagrant-ubuntu-trusty-64 CRON[13063]: (vagrant) CMD (/bin/bash -l -c 'echo '\''hello'\'' >> /home/vagrant/output.txt')
我有没有漏掉什么重要的内容?
crontab -r
,然后使用whenever --update-crontab --set environment='development'
在本地测试。 - luzny