aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorRobert Dober2017-08-03 11:19:47 +0200
committerGitHub2017-08-03 11:19:47 +0200
commit494fdfab76e6557018de77dac6861513ffa0502b (patch)
tree7488393cfe925334893e78a7a3983ded78167267 /app
parent8aba37303c2c2bd95df8c5117173579b3399c2c7 (diff)
parent517495a48e2a05c301159fc48573a9994fbff47b (diff)
downloadchouette-core-494fdfab76e6557018de77dac6861513ffa0502b.tar.bz2
Merge pull request #50 from af83/3511-cron-job-to-notify-parent-WorkbenchImport-of-sub-import-status--rb201708011853
3511 cron job to notify parent workbench import of sub import status rb201708011853
Diffstat (limited to 'app')
-rw-r--r--app/models/import.rb39
-rw-r--r--app/services/parent_import_notifier.rb15
2 files changed, 52 insertions, 2 deletions
diff --git a/app/models/import.rb b/app/models/import.rb
index c932ecdd9..f8702c115 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -3,7 +3,7 @@ class Import < ActiveRecord::Base
belongs_to :workbench
belongs_to :referential
- belongs_to :parent, class_name: to_s
+ belongs_to :parent, polymorphic: true
extend Enumerize
enumerize :status, in: %i(new pending successful failed running aborted canceled)
@@ -11,8 +11,43 @@ class Import < ActiveRecord::Base
validates :file, presence: true
validates_presence_of :referential, :workbench
- before_create do
+ before_create :initialize_fields
+
+ def self.failing_statuses
+ symbols_with_indifferent_access(%i(failed aborted canceled))
+ end
+
+ def self.finished_statuses
+ symbols_with_indifferent_access(%i(successful failed aborted canceled))
+ end
+
+ def notify_parent
+ parent.child_change(self)
+ update(notified_parent_at: DateTime.now)
+ end
+
+ def child_change(child)
+ return if self.class.finished_statuses.include?(status)
+
+ if self.class.failing_statuses.include?(child.status)
+ return update(status: 'failed')
+ end
+
+ update(status: 'successful') if ready?
+ end
+
+ def ready?
+ current_step == total_steps
+ end
+
+ private
+
+ def initialize_fields
self.token_download = SecureRandom.urlsafe_base64
self.status = Import.status.new
end
+
+ def self.symbols_with_indifferent_access(array)
+ array.flat_map { |symbol| [symbol, symbol.to_s] }
+ end
end
diff --git a/app/services/parent_import_notifier.rb b/app/services/parent_import_notifier.rb
new file mode 100644
index 000000000..47e6755e4
--- /dev/null
+++ b/app/services/parent_import_notifier.rb
@@ -0,0 +1,15 @@
+class ParentImportNotifier
+ def self.notify_when_finished(imports = nil)
+ imports ||= imports_pending_notification
+ imports.each(&:notify_parent)
+ end
+
+ def self.imports_pending_notification
+ Import
+ .where(
+ notified_parent_at: nil,
+ status: Import.finished_statuses
+ )
+ .where.not(parent: nil)
+ end
+end