Created
December 26, 2020 14:33
-
-
Save mryoshio/f3c00471d0a7344bb5032358e2734625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sample task model | |
# | |
# > rails c | |
# irb(main):004:0> t.finish | |
# "-------> notify_done with 0" | |
# "-------> notify_done with 1" | |
# "-------> notify_done with 2" | |
# "-------> notify_done end" | |
# => true | |
class Task < ActiveRecord::Base | |
state_machine initial: :todo do | |
state :todo | |
state :doing | |
state :done | |
event :start do | |
transition from: :todo, to: :doing | |
end | |
event :finish do | |
transition from: :doing, to: :done | |
end | |
event :redo do | |
transition from: :done, to: :todo | |
end | |
after_transition to: :todo, do: :notify_todo | |
after_transition to: :doing, do: :notify_doing | |
after_transition to: :done do |task, transition| | |
task.notify_done | |
end | |
end | |
def notify_todo | |
p "-------> #{__method__}" | |
end | |
def notify_doing | |
p "-------> #{__method__}" | |
end | |
def notify_done(cnt: 0) | |
p "-------> #{__method__} with #{cnt}" | |
raise | |
rescue => e | |
sleep Math.exp(cnt) | |
cnt += 1 | |
retry if cnt < 3 | |
ensure | |
p "-------> #{__method__} end" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment