aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorXinhui2016-09-29 16:17:13 +0200
committerXinhui2016-09-29 16:17:13 +0200
commita1fffa57564825303671c20e596f4fc944fbdd12 (patch)
treeefbfe652a11c48d4818a26b3686ca2f355e612d7 /app
parentcecd26452ce1cb2486d926421c1ae6360c973d1b (diff)
downloadchouette-core-a1fffa57564825303671c20e596f4fc944fbdd12.tar.bz2
Refactoring StopAreaReferentialSync
Refs #1710
Diffstat (limited to 'app')
-rw-r--r--app/models/stop_area_referential_sync.rb60
-rw-r--r--app/workers/stop_area_referential_sync_worker.rb23
2 files changed, 83 insertions, 0 deletions
diff --git a/app/models/stop_area_referential_sync.rb b/app/models/stop_area_referential_sync.rb
index 54a3fce46..ac5f09e93 100644
--- a/app/models/stop_area_referential_sync.rb
+++ b/app/models/stop_area_referential_sync.rb
@@ -1,3 +1,63 @@
class StopAreaReferentialSync < ActiveRecord::Base
+ include AASM
belongs_to :stop_area_referential
+ has_many :stop_area_referential_sync_messages, -> { order(created_at: :desc) }, :dependent => :destroy
+
+ after_commit :perform_sync, :on => :create
+ validate :multiple_process_validation, :on => :create
+
+ private
+ def perform_sync
+ StopAreaReferentialSyncWorker.perform_async(self.id)
+ end
+
+ # There can be only one instance running
+ def multiple_process_validation
+ if self.class.where(status: [:new, :pending], stop_area_referential_id: stop_area_referential_id).count > 0
+ errors.add(:base, :multiple_process)
+ end
+ end
+
+ aasm column: :status do
+ state :new, :initial => true
+ state :pending
+ state :successful
+ state :failed
+
+ event :run, after: :log_pending do
+ transitions :from => [:new, :failed], :to => :pending
+ end
+
+ event :successful, after: :log_successful do
+ transitions :from => :pending, :to => :successful
+ end
+
+ event :failed, after: :log_failed do
+ transitions :from => :pending, :to => :failed
+ end
+ end
+
+ def create_sync_message criticity, key, message_attributs = {}
+ params = {
+ criticity: criticity,
+ message_key: key,
+ message_attributs: message_attributs
+ }
+ stop_area_referential_sync_messages.create params
+ end
+
+ def log_pending
+ update_attribute(:started_at, Time.now)
+ create_sync_message :info, :pending
+ end
+
+ def log_successful message_attributs
+ update_attribute(:ended_at, Time.now)
+ create_sync_message :info, :successful, message_attributs
+ end
+
+ def log_failed message_attributs
+ update_attribute(:ended_at, Time.now)
+ create_sync_message :error, :failed, message_attributs
+ end
end
diff --git a/app/workers/stop_area_referential_sync_worker.rb b/app/workers/stop_area_referential_sync_worker.rb
new file mode 100644
index 000000000..662652831
--- /dev/null
+++ b/app/workers/stop_area_referential_sync_worker.rb
@@ -0,0 +1,23 @@
+class StopAreaReferentialSyncWorker
+ include Sidekiq::Worker
+ sidekiq_options :retry => false
+
+ def process_time
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
+ end
+
+ def perform(stop_area_ref_sync_id)
+ start_time = process_time
+ stop_ref_sync = StopAreaReferentialSync.find stop_area_ref_sync_id
+ stop_ref_sync.run
+ begin
+ info = Stif::ReflexSynchronization.synchronize
+ stop_ref_sync.successful info.merge({processing_time: process_time - start_time})
+ rescue Exception => e
+ stop_ref_sync.failed({
+ error: e.message,
+ processing_time: process_time - start_time
+ })
+ end
+ end
+end