Use ActiveJob

This commit is contained in:
gregoirenovel 2017-10-03 16:19:25 +02:00
parent 23c278aa65
commit 51f57d983e
6 changed files with 21 additions and 33 deletions

View file

@ -64,10 +64,10 @@ Afin de générer la BDD de l'application, il est nécessaire d'éxécuter les c
mailcatcher -f mailcatcher -f
rails s rails s
## Lancement des workers ## Programmation des jobs
Delayed::Job.enqueue(AutoArchiveProcedureWorker.new, cron: "* * * * *") AutoArchiveProcedureJob.set(cron: "* * * * *").perform_later
Delayed::Job.enqueue(WeeklyOverviewWorker.new, cron: "0 8 * * 0") WeeklyOverviewJob.set(cron: "0 8 * * 0").perform_later
## Exécution des tests (RSpec) ## Exécution des tests (RSpec)

View file

@ -1,6 +1,8 @@
class AutoArchiveProcedureWorker class AutoArchiveProcedureJob < ApplicationJob
queue_as :cron
def perform(*args) def perform(*args)
Rails.logger.info("AutoArchiveProcedureWorker started at #{Time.now}") Rails.logger.info("AutoArchiveProcedureJob started at #{Time.now}")
Procedure.publiees.where("auto_archive_on <= ?", Date.today).each do |procedure| Procedure.publiees.where("auto_archive_on <= ?", Date.today).each do |procedure|
procedure.dossiers.state_en_construction.each do |dossier| procedure.dossiers.state_en_construction.each do |dossier|
dossier.received! dossier.received!
@ -8,12 +10,6 @@ class AutoArchiveProcedureWorker
procedure.archive procedure.archive
end end
Rails.logger.info("AutoArchiveProcedureWorker ended at #{Time.now}") Rails.logger.info("AutoArchiveProcedureJob ended at #{Time.now}")
end end
def queue_name
"cron"
end
handle_asynchronously :perform
end end

View file

@ -1,6 +1,8 @@
class WeeklyOverviewWorker class WeeklyOverviewJob < ApplicationJob
queue_as :cron
def perform(*args) def perform(*args)
Rails.logger.info("WeeklyOverviewWorker started at #{Time.now}") Rails.logger.info("WeeklyOverviewJob started at #{Time.now}")
# Feature flipped to avoid mails in staging due to unprocessed dossier # Feature flipped to avoid mails in staging due to unprocessed dossier
if Features.weekly_overview if Features.weekly_overview
Gestionnaire.all Gestionnaire.all
@ -8,12 +10,6 @@ class WeeklyOverviewWorker
.reject { |_, overview| overview.nil? } .reject { |_, overview| overview.nil? }
.each { |gestionnaire, overview| GestionnaireMailer.last_week_overview(gestionnaire, overview).deliver_now } .each { |gestionnaire, overview| GestionnaireMailer.last_week_overview(gestionnaire, overview).deliver_now }
end end
Rails.logger.info("WeeklyOverviewWorker ended at #{Time.now}") Rails.logger.info("WeeklyOverviewJob ended at #{Time.now}")
end end
def queue_name
"cron"
end
handle_asynchronously :perform
end end

View file

@ -33,5 +33,7 @@ module TPS
else else
URL = "http://localhost:3000/" URL = "http://localhost:3000/"
end end
config.active_job.queue_adapter = :delayed_job
end end
end end

View file

@ -1,15 +1,12 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe AutoArchiveProcedureWorker, type: :worker do RSpec.describe AutoArchiveProcedureJob, type: :job do
before { Delayed::Worker.delay_jobs = false }
after { Delayed::Worker.delay_jobs = true }
let!(:procedure) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: nil )} let!(:procedure) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: nil )}
let!(:procedure_hier) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.ago )} let!(:procedure_hier) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.ago )}
let!(:procedure_aujourdhui) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: Date.today )} let!(:procedure_aujourdhui) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: Date.today )}
let!(:procedure_demain) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.from_now )} let!(:procedure_demain) { create(:procedure, published_at: Time.now, archived_at: nil, auto_archive_on: 1.day.from_now )}
subject { AutoArchiveProcedureWorker.new.perform } subject { AutoArchiveProcedureJob.new.perform }
context "when procedures have no auto_archive_on" do context "when procedures have no auto_archive_on" do
before do before do

View file

@ -1,9 +1,6 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe WeeklyOverviewWorker, type: :worker do RSpec.describe WeeklyOverviewJob, type: :job do
before { Delayed::Worker.delay_jobs = false }
after { Delayed::Worker.delay_jobs = true }
describe 'perform' do describe 'perform' do
let!(:gestionnaire) { create(:gestionnaire) } let!(:gestionnaire) { create(:gestionnaire) }
let(:overview) { double('overview') } let(:overview) { double('overview') }
@ -16,7 +13,7 @@ RSpec.describe WeeklyOverviewWorker, type: :worker do
before :each do before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(overview) expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(overview)
allow(GestionnaireMailer).to receive(:last_week_overview).and_return(mailer_double) allow(GestionnaireMailer).to receive(:last_week_overview).and_return(mailer_double)
WeeklyOverviewWorker.new.perform WeeklyOverviewJob.new.perform
end end
it { expect(GestionnaireMailer).to have_received(:last_week_overview).with(gestionnaire, overview) } it { expect(GestionnaireMailer).to have_received(:last_week_overview).with(gestionnaire, overview) }
@ -27,7 +24,7 @@ RSpec.describe WeeklyOverviewWorker, type: :worker do
before :each do before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(nil) expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(nil)
allow(GestionnaireMailer).to receive(:last_week_overview) allow(GestionnaireMailer).to receive(:last_week_overview)
WeeklyOverviewWorker.new.perform WeeklyOverviewJob.new.perform
end end
it { expect(GestionnaireMailer).not_to have_received(:last_week_overview) } it { expect(GestionnaireMailer).not_to have_received(:last_week_overview) }
@ -38,7 +35,7 @@ RSpec.describe WeeklyOverviewWorker, type: :worker do
before { allow(Features).to receive(:weekly_overview).and_return(false) } before { allow(Features).to receive(:weekly_overview).and_return(false) }
before :each do before :each do
allow(Gestionnaire).to receive(:all) allow(Gestionnaire).to receive(:all)
WeeklyOverviewWorker.new.perform WeeklyOverviewJob.new.perform
end end
it { expect(Gestionnaire).not_to receive(:all) } it { expect(Gestionnaire).not_to receive(:all) }