Merge pull request #2988 from aignatov-bio/ai-sci-5260-add-direction-condition-for-status-consequences

Add direction condition for status consequences [SCI-5260]
This commit is contained in:
aignatov-bio 2020-12-01 16:14:31 +01:00 committed by GitHub
commit a27c78e445
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 7 deletions

View file

@ -3,11 +3,11 @@
class MyModuleStatusConsequencesJob < ApplicationJob class MyModuleStatusConsequencesJob < ApplicationJob
queue_as :high_priority queue_as :high_priority
def perform(my_module, my_module_status_consequences) def perform(my_module, my_module_status_consequences, status_changing_direction)
error_raised = false error_raised = false
my_module.transaction do my_module.transaction do
my_module_status_consequences.each do |consequence| my_module_status_consequences.each do |consequence|
consequence.call(my_module) consequence.public_send(status_changing_direction, my_module)
end end
my_module.update!(status_changing: false) my_module.update!(status_changing: false)
rescue StandardError => e rescue StandardError => e

View file

@ -506,13 +506,19 @@ class MyModule < ApplicationRecord
self.changing_from_my_module_status_id = my_module_status_id_was if my_module_status_id_was.present? self.changing_from_my_module_status_id = my_module_status_id_was if my_module_status_id_was.present?
self.status_changing = true self.status_changing = true
status_changing_direction = my_module_status.previous_status_id == my_module_status_id_was ? :forward : :backward
yield yield
if my_module_status.my_module_status_consequences.any?(&:runs_in_background?) if my_module_status.my_module_status_consequences.any?(&:runs_in_background?)
MyModuleStatusConsequencesJob.perform_later(self, my_module_status.my_module_status_consequences.to_a) MyModuleStatusConsequencesJob.perform_later(
self,
my_module_status.my_module_status_consequences.to_a,
status_changing_direction
)
else else
my_module_status.my_module_status_consequences.each do |consequence| my_module_status.my_module_status_consequences.each do |consequence|
consequence.call(self) consequence.public_send(status_changing_direction, self)
end end
update!(status_changing: false) update!(status_changing: false)
end end

View file

@ -3,6 +3,10 @@
class MyModuleStatusConsequence < ApplicationRecord class MyModuleStatusConsequence < ApplicationRecord
belongs_to :my_module_status belongs_to :my_module_status
def forward(my_module); end
def backward(my_module); end
def runs_in_background? def runs_in_background?
false false
end end

View file

@ -3,7 +3,7 @@
# Just an example, to be replaced with an actual implementation # Just an example, to be replaced with an actual implementation
module MyModuleStatusConsequences module MyModuleStatusConsequences
class Completion < MyModuleStatusConsequence class Completion < MyModuleStatusConsequence
def call(my_module) def forward(my_module)
my_module.state = 'completed' my_module.state = 'completed'
my_module.completed_on = DateTime.now my_module.completed_on = DateTime.now
my_module.save! my_module.save!

View file

@ -6,7 +6,7 @@ module MyModuleStatusConsequences
true true
end end
def call(my_module) def forward(my_module)
my_module.assigned_repositories.each do |repository| my_module.assigned_repositories.each do |repository|
repository_snapshot = ::RepositorySnapshot.create_preliminary(repository, my_module) repository_snapshot = ::RepositorySnapshot.create_preliminary(repository, my_module)
service = Repositories::SnapshotProvisioningService.call(repository_snapshot: repository_snapshot) service = Repositories::SnapshotProvisioningService.call(repository_snapshot: repository_snapshot)

View file

@ -3,7 +3,7 @@
# Just an example, to be replaced with an actual implementation # Just an example, to be replaced with an actual implementation
module MyModuleStatusConsequences module MyModuleStatusConsequences
class Uncompletion < MyModuleStatusConsequence class Uncompletion < MyModuleStatusConsequence
def call(my_module) def backward(my_module)
return unless my_module.state == 'completed' return unless my_module.state == 'completed'
my_module.state = 'uncompleted' my_module.state = 'uncompleted'