我一直在使用AASM在我当前的项目中创建状态机,并且想知道自动调用事件并进入下一个状态的最佳方法是什么?
我正在考虑两种方法:
设置后台作业以定期检查是否满足某些条件,因此调用该事件以进入下一个状态。
让before_save
调用一个连续尝试下一个事件的方法。如果有条件,它就不会成功,否则,状态发生变化,下次模型更新时,我们会检查新事件。
我倾向于第二个选项,因为设置一个background_job队列只是为了过渡事件看起来像是一种矫枉过正。我无法找到有关此问题的最佳做法,所以我很想知道最好的方法以及为什么会这样做?
实施例
例如,我们有start_onboarding
和complete_onboarding
个事件。我不想手动调用这些事件,但我想在 pending - >之间自动转换。 in_progress - >已完成事件。
enum status: {
pending: 1,
in_progress: 2,
completed: 3
}
aasm column: :status, enum: true, whiny_transitions: false do
state :pending, initial: true
state :in_progress
state :completed
event :start_onboarding do
transitions from: :pending, to: :in_progress
end
event :complete_onboarding do
transitions from: :in_progress,
to: :completed,
if: :onboarding_completed?
end
end
答案 0 :(得分:1)
在类似的任务中:
我们摆脱了:
我们来使用:
代码看起来像这样:
require 'active_record'
require 'aasm'
require 'sidekiq'
class Task < ActiveRecord::Base
include AASM
establish_connection adapter: 'sqlite3', database: 'todo.db'
connection.create_table table_name, force: true do |t|
t.string :name, null: false
t.string :aasm_state, null: false, index: true
t.datetime :expired_at, null: false
end
validates :name, :aasm_state, :expired_at, presence: true
aasm do
state :pending, initial: true
state :in_progress
state :completed
state :expired
event :run do
transitions to: :in_progress
end
event :complete do
transitions to: :completed
end
event :expire do
transitions to: :expired, unless: :completed?
end
end
end
class Task::ExpireJob
include Sidekiq::Worker
def perform task
task.expire!
end
end
class Task::CreationService
def self.create! params
task = Task.create! params
task.run!
Task::ExpireJob.perform_at task.expired_at, task
task
end
def self.complete! task
task.complete!
task
end
end
task = Task::CreationService.create! \
name: 'first',
expired_at: DateTime.now + 30.seconds
p task
p Task::CreationService.complete! task