Sidekiq worker to archive procedures automatically + tests

This commit is contained in:
Mathieu Magnin 2017-03-15 12:40:59 +01:00
parent 359807b4f7
commit 24d17dc0f3
11 changed files with 155 additions and 11 deletions

View file

@ -102,6 +102,10 @@ gem 'simple_form'
gem 'newrelic_rpm'
gem 'sidekiq'
gem 'sidekiq-cron', '~> 0.4.4'
gem 'sinatra', github: 'sinatra', require: false
group :test do
gem 'capybara'
gem 'launchy'

View file

@ -1,3 +1,15 @@
GIT
remote: git://github.com/sinatra/sinatra.git
revision: d0c4053fd459be9f2c207cfeec5c0606461c014b
specs:
rack-protection (2.0.0.rc1)
rack
sinatra (2.0.0.rc1)
mustermann (= 1.0.0)
rack (~> 2.0)
rack-protection (= 2.0.0.rc1)
tilt (~> 2.0)
GIT
remote: https://github.com/mina-deploy/mina.git
revision: 343a7ab672d8b4f0ddb84ec240cde7d94b46397a
@ -114,6 +126,7 @@ GEM
execjs
coffee-script-source (1.11.1)
concurrent-ruby (1.0.2)
connection_pool (2.2.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
database_cleaner (1.5.3)
@ -333,7 +346,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
json (1.8.6)
json-jwt (1.7.0)
activesupport
bindata
@ -382,6 +395,7 @@ GEM
minitest (5.10.1)
multi_json (1.12.1)
multipart-post (2.0.0)
mustermann (1.0.0)
nenv (0.3.0)
netrc (0.11.0)
newrelic_rpm (3.18.1.330)
@ -476,7 +490,9 @@ GEM
nokogiri (~> 1.5)
trollop (~> 2.1)
rdoc (4.3.0)
redis (3.3.0)
redis (3.3.3)
redis-namespace (1.5.3)
redis (~> 3.0, >= 3.0.4)
ref (2.0.0)
request_store (1.3.1)
responders (2.3.0)
@ -528,6 +544,8 @@ GEM
ruby_parser (3.8.3)
sexp_processor (~> 4.1)
rubyzip (1.0.0)
rufus-scheduler (3.3.4)
tzinfo
safe_yaml (1.0.4)
sass (3.4.22)
sass-rails (5.0.6)
@ -553,6 +571,15 @@ GEM
shellany (0.0.1)
shoulda-matchers (3.1.1)
activesupport (>= 4.0.0)
sidekiq (4.2.9)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (~> 3.2, >= 3.2.1)
sidekiq-cron (0.4.5)
redis-namespace (>= 1.5.2)
rufus-scheduler (>= 2.0.24)
sidekiq (>= 4.2.1)
simple_form (3.4.0)
actionpack (> 4, < 5.1)
activemodel (> 4, < 5.1)
@ -712,8 +739,11 @@ DEPENDENCIES
selenium-webdriver
sentry-raven
shoulda-matchers
sidekiq
sidekiq-cron (~> 0.4.4)
simple_form
simplecov
sinatra!
smart_listing
spreadsheet_architect
spring

View file

@ -181,9 +181,7 @@ class Dossier < ActiveRecord::Base
where(state: WAITING_FOR_USER, archived: false).order("updated_at #{order}")
end
def self.en_construction order = 'ASC'
where(state: EN_CONSTRUCTION, archived: false).order("updated_at #{order}")
end
scope :en_construction, -> { where(state: EN_CONSTRUCTION, archived: false).order(updated_at: :asc) }
def self.ouvert order = 'ASC'
where(state: OUVERT, archived: false).order("updated_at #{order}")

View file

@ -46,6 +46,8 @@ class Procedure < ActiveRecord::Base
alias_method_chain "#{name.underscore.to_sym}".to_s, :override
end
scope :not_archived, -> { where(archived: false) }
def path
procedure_path.path unless procedure_path.nil?
end
@ -66,10 +68,6 @@ class Procedure < ActiveRecord::Base
types_de_piece_justificative.order(:order_place)
end
def self.not_archived id
Procedure.where(archived: false).find(id)
end
def self.active id
Procedure.where(archived: false, published: true).find(id)
end

View file

@ -86,6 +86,9 @@
%label{ for: :auto_archive_on} Archivage automatique le
= f.text_field :auto_archive_on, id: 'auto_archive_on', value: @procedure.auto_archive_on.try{ |d| d.strftime("%d-%m-%Y") }, data: { provide: 'datepicker', 'date-format' => 'dd/mm/yyyy' }
(à 00h00 )
%p.help-block
%i.fa.fa-info-circle
L'archivage automatique de la procédure entrainera le passage en instruction de tous les dossiers en construction.

View file

@ -0,0 +1,14 @@
class AutoArchiveProcedureWorker
include Sidekiq::Worker
def perform(*args)
procedures_to_archive = Procedure.not_archived.where("auto_archive_on <= ?", Date.today)
procedures_to_archive.each do |p|
p.dossiers.en_construction.update_all(state: :received)
end
procedures_to_archive.update_all(archived: true, auto_archive_on: nil)
end
end

View file

@ -0,0 +1,7 @@
Sidekiq.configure_server do |config|
Sidekiq::Logging.logger = Rails.logger
schedule_file = "config/schedule.yml"
Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end

View file

@ -207,7 +207,9 @@ Rails.application.routes.draw do
get '/:procedure_path' => '/users/dossiers#commencer'
end
apipie
require 'sidekiq/web'
require 'sidekiq/cron/web'
mount Sidekiq::Web => '/sidekiq' # , constraints: lambda { |request| User::is_admin_from_request(request) }
mount ActionCable.server => '/cable'
apipie
end

3
config/schedule.yml Normal file
View file

@ -0,0 +1,3 @@
auto_archive_procedure:
cron: "* * * * *"
class: "AutoArchiveProcedureWorker"

7
config/sidekiq.yml Normal file
View file

@ -0,0 +1,7 @@
:concurrency: 5
staging:
:concurrency: 2
production:
:concurrency: 2
:queues:
- default

View file

@ -0,0 +1,78 @@
require 'rails_helper'
RSpec.describe AutoArchiveProcedureWorker, type: :worker do
let!(:procedure) { create(:procedure, archived: false, auto_archive_on: nil )}
let!(:procedure_hier) { create(:procedure, archived: false, auto_archive_on: 1.day.ago )}
let!(:procedure_aujourdhui) { create(:procedure, archived: false, auto_archive_on: Date.today )}
let!(:procedure_demain) { create(:procedure, archived: false, auto_archive_on: 1.day.from_now )}
subject { AutoArchiveProcedureWorker.new.perform }
context "when procedures have no auto_archive_on" do
before do
subject
procedure.reload
end
it { expect(procedure.archived).to eq false }
end
context "when procedures have auto_archive_on set on yesterday or today" do
describe "titi" do
before do
subject
procedure_hier.reload
procedure_aujourdhui.reload
end
it { expect(procedure_hier.archived).to eq true }
it { expect(procedure_aujourdhui.archived).to eq true }
end
context "with dossiers" do
let!(:dossier1) { create(:dossier, procedure: procedure_hier, state: 'draft', archived: false)}
let!(:dossier2) { create(:dossier, procedure: procedure_hier, state: 'initiated', archived: false)}
let!(:dossier3) { create(:dossier, procedure: procedure_hier, state: 'replied', archived: false)}
let!(:dossier4) { create(:dossier, procedure: procedure_hier, state: 'updated', archived: false)}
let!(:dossier5) { create(:dossier, procedure: procedure_hier, state: 'received', archived: false)}
let!(:dossier6) { create(:dossier, procedure: procedure_hier, state: 'closed', archived: false)}
let!(:dossier7) { create(:dossier, procedure: procedure_hier, state: 'refused', archived: false)}
let!(:dossier8) { create(:dossier, procedure: procedure_hier, state: 'without_continuation', archived: false)}
before do
subject
(1..8).each do |i|
eval "dossier#{i}.reload"
end
end
it { expect(dossier1.state).to eq 'draft' }
it { expect(dossier2.state).to eq 'received' }
it { expect(dossier3.state).to eq 'received' }
it { expect(dossier4.state).to eq 'received' }
it { expect(dossier5.state).to eq 'received' }
it { expect(dossier6.state).to eq 'closed' }
it { expect(dossier7.state).to eq 'refused' }
it { expect(dossier8.state).to eq 'without_continuation' }
end
end
context "when procedures have auto_archive_on set on future" do
before do
subject
end
it { expect(procedure_demain.archived).to eq false }
end
end