aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2016-09-27 17:16:05 +0200
committerXinhui2016-09-27 17:53:51 +0200
commita2ac534ec3e9bbb34a392dff5ed1a11b7e270461 (patch)
treeca584d03ce72ee15dad4c5bbefd3709212738042
parent2d6fa24975ac5a3d28e657a96a781db1cb17d56c (diff)
downloadchouette-core-a2ac534ec3e9bbb34a392dff5ed1a11b7e270461.tar.bz2
LineReferentialSync status aasm
Refs #1707
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock2
-rw-r--r--app/models/line_referential_sync.rb36
-rw-r--r--app/views/line_referentials/show.html.slim12
-rw-r--r--app/workers/line_referential_sync_worker.rb20
-rw-r--r--spec/models/line_referential_sync_spec.rb30
6 files changed, 76 insertions, 25 deletions
diff --git a/Gemfile b/Gemfile
index 342e97ca8..0c4be1e94 100644
--- a/Gemfile
+++ b/Gemfile
@@ -109,6 +109,7 @@ gem 'whenever', github: 'af83/whenever', require: false # '~> 0.9'
gem 'rake'
gem 'devise-async'
gem 'apartment', '~> 1.0.0'
+gem 'aasm'
gem 'newrelic_rpm'
gem 'letter_opener'
diff --git a/Gemfile.lock b/Gemfile.lock
index 08efe99f3..31950483e 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -26,6 +26,7 @@ GEM
RedCloth (4.2.9)
RedCloth (4.2.9-java)
SyslogLogger (2.0)
+ aasm (4.5.2)
actionmailer (4.1.10)
actionpack (= 4.1.10)
actionview (= 4.1.10)
@@ -540,6 +541,7 @@ PLATFORMS
DEPENDENCIES
RedCloth
SyslogLogger
+ aasm
activerecord-postgis-adapter
acts-as-taggable-on (>= 3)
acts_as_list (~> 0.6.0)
diff --git a/app/models/line_referential_sync.rb b/app/models/line_referential_sync.rb
index 783fceaba..5b97010b1 100644
--- a/app/models/line_referential_sync.rb
+++ b/app/models/line_referential_sync.rb
@@ -1,4 +1,5 @@
class LineReferentialSync < ActiveRecord::Base
+ include AASM
belongs_to :line_referential
after_commit :perform_sync, :on => :create
validate :multiple_process_validation, :on => :create
@@ -10,8 +11,41 @@ class LineReferentialSync < ActiveRecord::Base
# There can be only one instance running
def multiple_process_validation
- if self.class.where(ended_at: nil, line_referential_id: line_referential_id).count > 0
+ if self.class.where(status: [:new, :pending], line_referential_id: line_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 log_pending
+ logger.debug "#{self.class.name} sync - pending"
+ update_attribute(:started_at, Time.now)
+ end
+
+ def log_successful
+ logger.debug "#{self.class.name} sync - done"
+ end
+
+ def log_failed error
+ logger.debug e.message
+ logger.debug "#{self.class.name} sync - failed"
+ end
end
diff --git a/app/views/line_referentials/show.html.slim b/app/views/line_referentials/show.html.slim
index 929eda7dd..df0b2ab36 100644
--- a/app/views/line_referentials/show.html.slim
+++ b/app/views/line_referentials/show.html.slim
@@ -21,13 +21,13 @@
span.badge = @line_referential.lines.size
= link_to Referential.human_attribute_name("lines"), line_referential_lines_path(@line_referential)
-- unless @line_referential.line_referential_sync.line_sync_operations.empty?
- h3 Historique des synchronisations
+/ - unless @line_referential.line_referential_sync.line_sync_operations.empty?
+/ h3 Historique des synchronisations
- ul.list-group width="75%"
- - @line_referential.line_referential_sync.line_sync_operations.each do |sync|
- li = "#{sync.created_at.to_formatted_s(:short)} - #{sync.message}"
+/ ul.list-group width="75%"
+/ - @line_referential.line_referential_sync.line_sync_operations.each do |sync|
+/ li = "#{sync.created_at.to_formatted_s(:short)} - #{sync.message}"
- content_for :sidebar do
ul.actions
- = link_to t('line_referentials.actions.edit'), edit_line_referential_path(@line_referential), class: 'edit' \ No newline at end of file
+ = link_to t('line_referentials.actions.edit'), edit_line_referential_path(@line_referential), class: 'edit'
diff --git a/app/workers/line_referential_sync_worker.rb b/app/workers/line_referential_sync_worker.rb
index b39fae5e5..1dbb36ad2 100644
--- a/app/workers/line_referential_sync_worker.rb
+++ b/app/workers/line_referential_sync_worker.rb
@@ -1,26 +1,14 @@
class LineReferentialSyncWorker
include Sidekiq::Worker
- def update_started_at lref_sync
- lref_sync.update_attribute(:started_at, Time.now)
- end
-
- def update_ended_at lref_sync
- lref_sync.update_attribute(:ended_at, Time.now)
- end
-
def perform(lref_sync_id)
- logger.info "worker call to perfom"
lref_sync = LineReferentialSync.find lref_sync_id
- update_started_at lref_sync
+ lref_sync.run
begin
Stif::CodifLineSynchronization.synchronize
- logger.info "worker done CodifLineSynchronization"
- rescue Exception => e
- ap "LineReferentialSyncWorker perform:rescue #{e.message}"
- ensure
- logger.info "worker ensure ended_at"
- update_ended_at lref_sync
+ lref_sync.successful
+ rescue Exception => error
+ lref_sync.failed error
end
end
end
diff --git a/spec/models/line_referential_sync_spec.rb b/spec/models/line_referential_sync_spec.rb
index bcd58f0c9..dd68b099d 100644
--- a/spec/models/line_referential_sync_spec.rb
+++ b/spec/models/line_referential_sync_spec.rb
@@ -8,12 +8,38 @@ RSpec.describe LineReferentialSync, :type => :model do
it { is_expected.to belong_to(:line_referential) }
it 'should validate multiple sync instance' do
- lref_sync = build(:line_referential_sync, line_referential: create(:line_referential_sync).line_referential)
- expect(lref_sync).to be_invalid
+ pending = create(:line_referential_sync)
+ multiple = build(:line_referential_sync, line_referential: pending.line_referential)
+ expect(multiple).to be_invalid
end
it 'should call LineReferentialSyncWorker on create' do
expect(LineReferentialSyncWorker).to receive(:perform_async)
create(:line_referential_sync).run_callbacks(:commit)
end
+
+ describe 'states' do
+ let(:line_referential_sync) { create(:line_referential_sync) }
+
+ it 'should initialize with new state' do
+ expect(line_referential_sync.new?).to be_truthy
+ end
+
+ it 'should pending state change' do
+ expect(line_referential_sync).to receive(:log_pending)
+ line_referential_sync.run
+ end
+
+ it 'should successful state change' do
+ expect(line_referential_sync).to receive(:log_successful)
+ line_referential_sync.run
+ line_referential_sync.successful
+ end
+
+ it 'should failed state change' do
+ expect(line_referential_sync).to receive(:log_failed)
+ line_referential_sync.run
+ line_referential_sync.failed
+ end
+ end
end