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
rails s
## Lancement des workers
## Programmation des jobs
Delayed::Job.enqueue(AutoArchiveProcedureWorker.new, cron: "* * * * *")
Delayed::Job.enqueue(WeeklyOverviewWorker.new, cron: "0 8 * * 0")
AutoArchiveProcedureJob.set(cron: "* * * * *").perform_later
WeeklyOverviewJob.set(cron: "0 8 * * 0").perform_later
## Exécution des tests (RSpec)

View file

@ -1,6 +1,8 @@
class AutoArchiveProcedureWorker
class AutoArchiveProcedureJob < ApplicationJob
queue_as :cron
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.dossiers.state_en_construction.each do |dossier|
dossier.received!
@ -8,12 +10,6 @@ class AutoArchiveProcedureWorker
procedure.archive
end
Rails.logger.info("AutoArchiveProcedureWorker ended at #{Time.now}")
Rails.logger.info("AutoArchiveProcedureJob ended at #{Time.now}")
end
def queue_name
"cron"
end
handle_asynchronously :perform
end

View file

@ -1,6 +1,8 @@
class WeeklyOverviewWorker
class WeeklyOverviewJob < ApplicationJob
queue_as :cron
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
if Features.weekly_overview
Gestionnaire.all
@ -8,12 +10,6 @@ class WeeklyOverviewWorker
.reject { |_, overview| overview.nil? }
.each { |gestionnaire, overview| GestionnaireMailer.last_week_overview(gestionnaire, overview).deliver_now }
end
Rails.logger.info("WeeklyOverviewWorker ended at #{Time.now}")
Rails.logger.info("WeeklyOverviewJob ended at #{Time.now}")
end
def queue_name
"cron"
end
handle_asynchronously :perform
end

View file

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

View file

@ -1,15 +1,12 @@
require 'rails_helper'
RSpec.describe AutoArchiveProcedureWorker, type: :worker do
before { Delayed::Worker.delay_jobs = false }
after { Delayed::Worker.delay_jobs = true }
RSpec.describe AutoArchiveProcedureJob, type: :job do
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_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 )}
subject { AutoArchiveProcedureWorker.new.perform }
subject { AutoArchiveProcedureJob.new.perform }
context "when procedures have no auto_archive_on" do
before do

View file

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