From 042928b20574184777361d226c174a5fc9aeb006 Mon Sep 17 00:00:00 2001 From: Axel Kelting Date: Tue, 17 Jan 2023 00:05:50 -0800 Subject: [PATCH 1/4] Instrument callbacks with active-support --- lib/statesman/callback.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/statesman/callback.rb b/lib/statesman/callback.rb index 9c0623ea..85a06d2e 100644 --- a/lib/statesman/callback.rb +++ b/lib/statesman/callback.rb @@ -19,7 +19,16 @@ def initialize(options = { from: nil, to: nil, callback: nil }) end def call(*args) - callback.call(*args) + ActiveSupport::Notifications.instrument "callback.statesman", { + subject: args.first.class.name, + subject_id: args.first.id, + to_state: to, + from_state: from, + callback: callback.to_s, + resource: self.class.name + } do + callback.call(*args) + end end def applies_to?(options = { from: nil, to: nil }) From 6b61f149493369dc888624dcc462ab0aa973ee48 Mon Sep 17 00:00:00 2001 From: Axel Kelting Date: Tue, 17 Jan 2023 00:06:13 -0800 Subject: [PATCH 2/4] Instrument guards with active-support --- lib/statesman/guard.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/statesman/guard.rb b/lib/statesman/guard.rb index 475f04b3..8e6a18fc 100644 --- a/lib/statesman/guard.rb +++ b/lib/statesman/guard.rb @@ -6,7 +6,16 @@ module Statesman class Guard < Callback def call(*args) - raise GuardFailedError.new(from, to) unless super(*args) + ActiveSupport::Notifications.instrument "guard.statesman", { + subject: args.first.class.name, + subject_id: args.first.id, + to_state: to, + from_state: from, + callback: callback.to_s, + resource: self.class.name + } do + raise GuardFailedError.new(from, to) unless super(*args) + end end end end From 777b18f5a381e1a67991b84a151144f724af97bd Mon Sep 17 00:00:00 2001 From: Axel Kelting Date: Wed, 18 Jan 2023 00:23:27 -0800 Subject: [PATCH 3/4] Fix code styles --- lib/statesman/callback.rb | 4 ++-- lib/statesman/guard.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/statesman/callback.rb b/lib/statesman/callback.rb index 85a06d2e..1ca1e2d0 100644 --- a/lib/statesman/callback.rb +++ b/lib/statesman/callback.rb @@ -19,13 +19,13 @@ def initialize(options = { from: nil, to: nil, callback: nil }) end def call(*args) - ActiveSupport::Notifications.instrument "callback.statesman", { + ActiveSupport::Notifications.instrument "callback.statesman", { subject: args.first.class.name, subject_id: args.first.id, to_state: to, from_state: from, callback: callback.to_s, - resource: self.class.name + resource: self.class.name, } do callback.call(*args) end diff --git a/lib/statesman/guard.rb b/lib/statesman/guard.rb index 8e6a18fc..bb106cbe 100644 --- a/lib/statesman/guard.rb +++ b/lib/statesman/guard.rb @@ -6,13 +6,13 @@ module Statesman class Guard < Callback def call(*args) - ActiveSupport::Notifications.instrument "guard.statesman", { + ActiveSupport::Notifications.instrument "guard.statesman", { subject: args.first.class.name, subject_id: args.first.id, to_state: to, from_state: from, callback: callback.to_s, - resource: self.class.name + resource: self.class.name, } do raise GuardFailedError.new(from, to) unless super(*args) end From d80cb92be2e151f4ad4622cda81414123f85c72d Mon Sep 17 00:00:00 2001 From: Axel Kelting Date: Wed, 18 Jan 2023 00:24:07 -0800 Subject: [PATCH 4/4] Instrument transitions with active-support --- lib/statesman/machine.rb | 42 ++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/statesman/machine.rb b/lib/statesman/machine.rb index 153059a6..c6e80b41 100644 --- a/lib/statesman/machine.rb +++ b/lib/statesman/machine.rb @@ -279,14 +279,20 @@ def history end def transition_to!(new_state, metadata = {}) - initial_state = current_state - new_state = new_state.to_s - - validate_transition(from: initial_state, - to: new_state, - metadata: metadata) - - @storage_adapter.create(initial_state, new_state, metadata) + ActiveSupport::Notifications.instrument "transition.statesman", { + to_state: new_state, + from_state: current_state, + resource: self.class.name, + } do + initial_state = current_state + new_state = new_state.to_s + + validate_transition(from: initial_state, + to: new_state, + metadata: metadata) + + @storage_adapter.create(initial_state, new_state, metadata) + end true rescue TransitionFailedError => e @@ -298,13 +304,25 @@ def transition_to!(new_state, metadata = {}) end def execute_on_failure(phase, initial_state, new_state, exception) - callbacks = callbacks_for(phase, from: initial_state, to: new_state) - callbacks.each { |cb| cb.call(@object, exception) } + ActiveSupport::Notifications.instrument "execute_on_failure.statesman", { + to_state: new_state, + from_state: initial_state, + resource: self.class.name, + } do + callbacks = callbacks_for(phase, from: initial_state, to: new_state) + callbacks.each { |cb| cb.call(@object, exception) } + end end def execute(phase, initial_state, new_state, transition) - callbacks = callbacks_for(phase, from: initial_state, to: new_state) - callbacks.each { |cb| cb.call(@object, transition) } + ActiveSupport::Notifications.instrument "execute.statesman", { + to_state: new_state, + from_state: initial_state, + resource: self.class.name, + } do + callbacks = callbacks_for(phase, from: initial_state, to: new_state) + callbacks.each { |cb| cb.call(@object, transition) } + end end def transition_to(new_state, metadata = {})