diff options
| -rw-r--r-- | app/models/line_referential.rb | 2 | ||||
| -rw-r--r-- | app/models/line_referential_sync.rb | 12 | ||||
| -rw-r--r-- | app/workers/line_referential_sync_worker.rb | 11 | ||||
| -rw-r--r-- | lib/stif/codif_line_synchronization.rb | 13 | ||||
| -rw-r--r-- | spec/factories/line_referential_syncs.rb | 1 | ||||
| -rw-r--r-- | spec/factories/line_referentials.rb | 1 | ||||
| -rw-r--r-- | spec/models/line_referential_sync_spec.rb | 11 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 5 | ||||
| -rw-r--r-- | spec/workers/line_referential_sync_worker_spec.rb | 17 | 
9 files changed, 53 insertions, 20 deletions
| diff --git a/app/models/line_referential.rb b/app/models/line_referential.rb index ccab5bf8a..f33934dfc 100644 --- a/app/models/line_referential.rb +++ b/app/models/line_referential.rb @@ -7,7 +7,7 @@ class LineReferential < ActiveRecord::Base    has_many :companies, class_name: 'Chouette::Company'    has_many :networks, class_name: 'Chouette::Network' -  has_one :line_referential_sync +  has_many :line_referential_syncs    def add_member(organisation, options = {})      attributes = options.merge organisation: organisation diff --git a/app/models/line_referential_sync.rb b/app/models/line_referential_sync.rb index 35e074d4e..c0af54ac1 100644 --- a/app/models/line_referential_sync.rb +++ b/app/models/line_referential_sync.rb @@ -1,9 +1,17 @@  class LineReferentialSync < ActiveRecord::Base    belongs_to :line_referential -  after_create :synchronize +  after_create :perform_sync +  validate :multiple_process_validation, :on => :create    private -  def synchronize +  def perform_sync      LineReferentialSyncWorker.perform_async(self.id)    end + +  # 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 +      errors.add(:base, :multiple_process) +    end +  end  end diff --git a/app/workers/line_referential_sync_worker.rb b/app/workers/line_referential_sync_worker.rb index 719a10190..e63bf97cc 100644 --- a/app/workers/line_referential_sync_worker.rb +++ b/app/workers/line_referential_sync_worker.rb @@ -1,19 +1,24 @@  class LineReferentialSyncWorker    include Sidekiq::Worker +  sidekiq_options :retry => false    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)      lref_sync = LineReferentialSync.find lref_sync_id - +    update_started_at lref_sync      begin        Stif::CodifLineSynchronization.synchronize -      lref_sync.update_attribute(:ended_at, Time.now)      rescue Exception => e -      ap 'call to rescue Exception' +      ap "LineReferentialSyncWorker perform:rescue #{e.message}" +    ensure +      update_ended_at lref_sync      end    end  end diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb index 4bbc48946..fee785766 100644 --- a/lib/stif/codif_line_synchronization.rb +++ b/lib/stif/codif_line_synchronization.rb @@ -1,15 +1,8 @@  module Stif    module CodifLineSynchronization      class << self -      # Don't check last synchronizations if force_sync -      def synchronize force_sync = false -        # Check last synchronization and synchronization interval -        date = DateTime.now.to_date - LineReferential.first.sync_interval.days -        last_sync = LineReferential.first.line_referential_sync.line_sync_operations.where(status: :ok).last.try(:created_at) -        return if last_sync.present? && last_sync.to_date > date && !force_sync - +      def synchronize          start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) -        # TODO Check exceptions and status messages          begin            # Fetch Codifline data            client = Codifligne::API.new @@ -60,13 +53,9 @@ module Stif            total_codifligne_elements = operators.count + lines.count + networks.count + groups_of_lines.count            total_deleted = deleted_op + deleted_li + deleted_ne + deleted_gr            total_time = elapsed_time_since start_time - -          LineReferential.first.line_referential_sync.record_status :ok, I18n.t('synchronization.codifligne.message.success', time: total_time, imported: total_codifligne_elements, deleted: total_deleted)          rescue Exception => e            total_time = elapsed_time_since start_time -            Rails.logger.error "Codifligne:sync - Error: #{e}, ended after #{total_time} seconds" -          LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.codifligne.message.failure', time: total_time)          end        end diff --git a/spec/factories/line_referential_syncs.rb b/spec/factories/line_referential_syncs.rb index 8f0d1e6fb..27b7a9fb9 100644 --- a/spec/factories/line_referential_syncs.rb +++ b/spec/factories/line_referential_syncs.rb @@ -1,4 +1,5 @@  FactoryGirl.define do    factory :line_referential_sync do +    association :line_referential, :factory => :line_referential    end  end diff --git a/spec/factories/line_referentials.rb b/spec/factories/line_referentials.rb index 47d0727c5..cfce1399f 100644 --- a/spec/factories/line_referentials.rb +++ b/spec/factories/line_referentials.rb @@ -1,6 +1,5 @@  FactoryGirl.define do    factory :line_referential do      sequence(:name) { |n| "Line Referential #{n}" } -    association :line_referential_sync, :factory => :line_referential_sync    end  end diff --git a/spec/models/line_referential_sync_spec.rb b/spec/models/line_referential_sync_spec.rb index 0cab0389c..f5c738792 100644 --- a/spec/models/line_referential_sync_spec.rb +++ b/spec/models/line_referential_sync_spec.rb @@ -4,5 +4,16 @@ RSpec.describe LineReferentialSync, :type => :model do    it 'should have a valid factory' do      expect(FactoryGirl.build(:line_referential_sync)).to be_valid    end +    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 +  end + +  it 'should call LineReferentialSyncWorker on create' do +    expect(LineReferentialSyncWorker).to receive(:perform_async) +    create :line_referential_sync +  end  end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 93b148496..dd062bc84 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,8 @@ require 'will_paginate/array'  require 'fakeweb'  require 'webmock/rspec'  require 'simplecov' +require 'sidekiq/testing' +Sidekiq::Testing.fake!  if ENV['JOB_NAME']    require 'simplecov-rcov' @@ -43,6 +45,9 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}  ActiveRecord::Migration.maintain_test_schema!  RSpec.configure do |config| +  config.before(:each) do +    Sidekiq::Worker.clear_all +  end    #Capybara.exact = true    Capybara.javascript_driver = :poltergeist diff --git a/spec/workers/line_referential_sync_worker_spec.rb b/spec/workers/line_referential_sync_worker_spec.rb index 0a1172e41..f1a63c9db 100644 --- a/spec/workers/line_referential_sync_worker_spec.rb +++ b/spec/workers/line_referential_sync_worker_spec.rb @@ -1,4 +1,19 @@  require 'rails_helper'  RSpec.describe LineReferentialSyncWorker, type: :worker do -    pending "add some examples to (or delete) #{__FILE__}" +  let!(:line_referential_sync) { create :line_referential_sync } + +  it 'should call codifline synchronize on worker perform' do +    expect(Stif::CodifLineSynchronization).to receive(:synchronize) +    LineReferentialSyncWorker.new.perform(line_referential_sync.id) +  end + +  it 'should update line_referential_sync started_at on worker perform' do +    LineReferentialSyncWorker.new.perform(line_referential_sync.id) +    expect(line_referential_sync.reload.started_at).not_to be_nil +  end + +  it 'should update line_referential_sync ended_at on worker perform success' do +    LineReferentialSyncWorker.new.perform(line_referential_sync.id) +    expect(line_referential_sync.reload.started_at).not_to be_nil +  end  end | 
