aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/stop_area_referential_sync.rb60
-rw-r--r--app/workers/stop_area_referential_sync_worker.rb23
-rw-r--r--lib/stif/reflex_synchronization.rb2
-rw-r--r--spec/models/stop_area_referential_sync_spec.rb37
-rw-r--r--spec/tasks/reflex_rake_spec.rb5
-rw-r--r--spec/workers/stop_area_referential_sync_worker_spec.rb19
6 files changed, 139 insertions, 7 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
diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb
index 19f39e858..fae650945 100644
--- a/lib/stif/reflex_synchronization.rb
+++ b/lib/stif/reflex_synchronization.rb
@@ -41,10 +41,8 @@ module Stif
# Purge deleted stop_area
deleted = self.set_deleted_stop_area processed.uniq
- self.defaut_referential.stop_area_referential_sync.record_status :ok, I18n.t('synchronization.reflex.message.success', time: Time.now - tstart, imported: processed.uniq.size, deleted: deleted.size)
rescue Exception => e
Rails.logger.error "Reflex:sync - Error: #{e}, ended after #{Time.now - tstart} seconds"
- self.defaut_referential.stop_area_referential_sync.record_status :ko, I18n.t('synchronization.reflex.message.failure', time: Time.now - tstart)
end
end
diff --git a/spec/models/stop_area_referential_sync_spec.rb b/spec/models/stop_area_referential_sync_spec.rb
index 5f1e1e124..dd6855632 100644
--- a/spec/models/stop_area_referential_sync_spec.rb
+++ b/spec/models/stop_area_referential_sync_spec.rb
@@ -6,4 +6,41 @@ RSpec.describe StopAreaReferentialSync, :type => :model do
end
it { is_expected.to belong_to(:stop_area_referential) }
+ it { is_expected.to have_many(:stop_area_referential_sync_messages) }
+
+ it 'should validate multiple sync instance' do
+ pending = create(:stop_area_referential_sync)
+ multiple = build(:stop_area_referential_sync, stop_area_referential: pending.stop_area_referential)
+ expect(multiple).to be_invalid
+ end
+
+ it 'should call StopAreaReferentialSyncWorker on create' do
+ expect(StopAreaReferentialSyncWorker).to receive(:perform_async)
+ create(:stop_area_referential_sync).run_callbacks(:commit)
+ end
+
+ describe 'states' do
+ let(:stop_area_referential_sync) { create(:stop_area_referential_sync) }
+
+ it 'should initialize with new state' do
+ expect(stop_area_referential_sync.new?).to be_truthy
+ end
+
+ it 'should log pending state change' do
+ expect(stop_area_referential_sync).to receive(:log_pending)
+ stop_area_referential_sync.run
+ end
+
+ it 'should log successful state change' do
+ expect(stop_area_referential_sync).to receive(:log_successful)
+ stop_area_referential_sync.run
+ stop_area_referential_sync.successful
+ end
+
+ it 'should log failed state change' do
+ expect(stop_area_referential_sync).to receive(:log_failed)
+ stop_area_referential_sync.run
+ stop_area_referential_sync.failed
+ end
+ end
end
diff --git a/spec/tasks/reflex_rake_spec.rb b/spec/tasks/reflex_rake_spec.rb
index e5320b429..46910de72 100644
--- a/spec/tasks/reflex_rake_spec.rb
+++ b/spec/tasks/reflex_rake_spec.rb
@@ -44,11 +44,6 @@ describe 'reflex:sync' do
Stif::ReflexSynchronization.synchronize
end
- it 'should log sync operations' do
- expect(StopAreaSyncOperation.count).to eq 2
- expect(StopAreaSyncOperation.take.status).to eq "ok"
- end
-
it 'should not create duplicate stop_area' do
expect(Chouette::StopArea.count).to eq 6
expect(Chouette::AccessPoint.count).to eq 2
diff --git a/spec/workers/stop_area_referential_sync_worker_spec.rb b/spec/workers/stop_area_referential_sync_worker_spec.rb
new file mode 100644
index 000000000..48b64e55e
--- /dev/null
+++ b/spec/workers/stop_area_referential_sync_worker_spec.rb
@@ -0,0 +1,19 @@
+require 'rails_helper'
+RSpec.describe StopAreaReferentialSyncWorker, type: :worker do
+ let!(:stop_area_referential_sync) { create :stop_area_referential_sync }
+
+ it 'should call reflex synchronize on worker perform' do
+ expect(Stif::ReflexSynchronization).to receive(:synchronize)
+ StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id)
+ end
+
+ it 'should update stop_area_referential_sync started_at on worker perform' do
+ StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id)
+ expect(stop_area_referential_sync.reload.started_at).not_to be_nil
+ end
+
+ it 'should update stop_area_referential_sync ended_at on worker perform success' do
+ StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id)
+ expect(stop_area_referential_sync.reload.started_at).not_to be_nil
+ end
+end