forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ManageIQ#13560 from chrisarcand/try-to-fix-job
Normalize Job::StateMachine
- Loading branch information
Showing
2 changed files
with
53 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
class Job | ||
module StateMachine | ||
def transitions | ||
@transitions ||= load_transitions | ||
end | ||
module Job::StateMachine | ||
extend ActiveSupport::Concern | ||
|
||
def state=(next_state) | ||
# '*' refers to any state; if next_state is '*', remain in current state. | ||
super unless next_state.nil? || next_state == '*' | ||
end | ||
def transitions | ||
@transitions ||= load_transitions | ||
end | ||
|
||
def state=(next_state) | ||
# '*' refers to any state; if next_state is '*', remain in current state. | ||
super unless next_state.nil? || next_state == '*' | ||
end | ||
|
||
# test whether the transition is allowed; if yes, transit to next state | ||
def transit_state(signal) | ||
permitted_transitions = transitions[signal.to_sym] | ||
unless permitted_transitions.nil? | ||
next_state = permitted_transitions[state] | ||
# test whether the transition is allowed; if yes, transit to next state | ||
def transit_state(signal) | ||
permitted_transitions = transitions[signal.to_sym] | ||
unless permitted_transitions.nil? | ||
next_state = permitted_transitions[state] | ||
|
||
# if current state is not explicitly permitted, is any state (referred by '*') permitted? | ||
next_state = permitted_transitions['*'] unless next_state | ||
self.state = next_state | ||
end | ||
!!next_state | ||
# if current state is not explicitly permitted, is any state (referred by '*') permitted? | ||
next_state = permitted_transitions['*'] unless next_state | ||
self.state = next_state | ||
end | ||
!!next_state | ||
end | ||
|
||
def signal_abort(*args) | ||
signal(:abort, *args) | ||
end | ||
def signal_abort(*args) | ||
signal(:abort, *args) | ||
end | ||
|
||
def signal(signal, *args) | ||
signal = :abort_job if signal == :abort | ||
if transit_state(signal) | ||
save | ||
send(signal, *args) if respond_to?(signal) | ||
else | ||
raise _("%{signal} is not permitted at state %{state}") % {:signal => signal, :state => state} | ||
end | ||
def signal(signal, *args) | ||
signal = :abort_job if signal == :abort | ||
if transit_state(signal) | ||
save | ||
send(signal, *args) if respond_to?(signal) | ||
else | ||
raise _("%{signal} is not permitted at state %{state}") % {:signal => signal, :state => state} | ||
end | ||
end | ||
end |
This file contains 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