ruby rails 后台应用程序在用户动态定义的时间自动执行 运行 作业?
ruby on rails background application to run jobs automaically at a time dynamically defined by users?
我有一个用例,其中用户从 Web 界面安排 'command'。用户还指定需要触发命令的日期和时间。
这是步骤顺序:
1.User 安排命令 'Restart Device' 在 5 月 31 日下午 3 点。
2.This 保存在名为 Command 的数据库 table 中。
3.Now 需要有一个需要在指定时间触发的后台作业来做某事(拨打 api 电话、发送电子邮件等)
4.Once 作业被执行,它被删除或标记为完成,直到发出新命令。
可能有多个用户同时执行上述步骤序列。
delayed_job 是上面的一个不错的选择吗?我找不到有关如何使用延迟作业实现上述内容的示例。
编辑:我查看 delayed_job 的原因是因为最终我需要利用现有的关系数据库
我建议使用 Sidekiq. With it you can use scheduled jobs 告诉 sidekiq 何时执行作业。
示例:
MyWorker.perform_at(3.hours.from_now, 'mike', 1)
编辑:工人示例
#app/workers/restart_device_worker.rb
class RestartDeviceWorker
include Sidekiq::Worker
def perform(params)
# Do the job
# ...
# update in DB
end
end
见文档:https://blog.codeship.com/how-to-use-rails-active-job/
https://guides.rubyonrails.org/active_job_basics.html
如果您使用的是 Rails 5,那么您最好选择 ActiveJob(内置功能)
Use ActiveJob
"Active Job – Make work happen later. Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really."
Active Job 具有用于多个队列后端(Sidekiq、Resque、Delayed Job 等)的内置适配器。你只需要告诉他们。
场景:我想在 24 小时(1 天)后删除我的故事。然后我们创建一个名为 "StoriesCleanupJob" 的作业。在创建故事时调用此作业,如下所示
StoriesCleanupJob.set(wait: 1.day).perform_later(story)
它会在 1 天后调用 Job。
class StoriesCleanupJob < ApplicationJob
queue_as :default
def perform(story)
if story.destroy
#put your own conditions like update the status and all, whatever you want to perform.
end
end
end
我有一个用例,其中用户从 Web 界面安排 'command'。用户还指定需要触发命令的日期和时间。
这是步骤顺序:
1.User 安排命令 'Restart Device' 在 5 月 31 日下午 3 点。
2.This 保存在名为 Command 的数据库 table 中。
3.Now 需要有一个需要在指定时间触发的后台作业来做某事(拨打 api 电话、发送电子邮件等)
4.Once 作业被执行,它被删除或标记为完成,直到发出新命令。
可能有多个用户同时执行上述步骤序列。
delayed_job 是上面的一个不错的选择吗?我找不到有关如何使用延迟作业实现上述内容的示例。
编辑:我查看 delayed_job 的原因是因为最终我需要利用现有的关系数据库
我建议使用 Sidekiq. With it you can use scheduled jobs 告诉 sidekiq 何时执行作业。
示例:
MyWorker.perform_at(3.hours.from_now, 'mike', 1)
编辑:工人示例
#app/workers/restart_device_worker.rb
class RestartDeviceWorker
include Sidekiq::Worker
def perform(params)
# Do the job
# ...
# update in DB
end
end
见文档:https://blog.codeship.com/how-to-use-rails-active-job/ https://guides.rubyonrails.org/active_job_basics.html
如果您使用的是 Rails 5,那么您最好选择 ActiveJob(内置功能)
Use ActiveJob
"Active Job – Make work happen later. Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really."
Active Job 具有用于多个队列后端(Sidekiq、Resque、Delayed Job 等)的内置适配器。你只需要告诉他们。
场景:我想在 24 小时(1 天)后删除我的故事。然后我们创建一个名为 "StoriesCleanupJob" 的作业。在创建故事时调用此作业,如下所示
StoriesCleanupJob.set(wait: 1.day).perform_later(story)
它会在 1 天后调用 Job。
class StoriesCleanupJob < ApplicationJob
queue_as :default
def perform(story)
if story.destroy
#put your own conditions like update the status and all, whatever you want to perform.
end
end
end