feat(task): task enqueueing a maintenance task runnable on deploy
This commit is contained in:
parent
2127f8cef1
commit
cae5d8afed
4 changed files with 92 additions and 0 deletions
24
app/tasks/maintenance/concerns/runnable_on_deploy_concern.rb
Normal file
24
app/tasks/maintenance/concerns/runnable_on_deploy_concern.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Maintenance
|
||||||
|
module RunnableOnDeployConcern
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def run_on_first_deploy
|
||||||
|
@run_on_first_deploy = true
|
||||||
|
end
|
||||||
|
|
||||||
|
def run_on_deploy?
|
||||||
|
return false unless @run_on_first_deploy
|
||||||
|
|
||||||
|
task = MaintenanceTasks::TaskDataShow.new(name)
|
||||||
|
|
||||||
|
return false if task.completed_runs.not_errored.any?
|
||||||
|
return false if task.active_runs.any?
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,4 +1,14 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
namespace :deploy do
|
||||||
|
task maintenance_tasks: :environment do
|
||||||
|
tasks = MaintenanceTasks::Task
|
||||||
|
.load_all
|
||||||
|
.filter { _1.respond_to?(:run_on_deploy?) && _1.run_on_deploy? }
|
||||||
|
|
||||||
|
tasks.each do |task|
|
||||||
|
Rails.logger.info { "MaintenanceTask run on deploy #{task.name}" }
|
||||||
|
MaintenanceTasks::Runner.run(name: task.name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,8 +5,13 @@ module <%= tasks_module %>
|
||||||
class <%= class_name %>Task < MaintenanceTasks::Task
|
class <%= class_name %>Task < MaintenanceTasks::Task
|
||||||
# Documentation: cette tâche modifie les données pour…
|
# Documentation: cette tâche modifie les données pour…
|
||||||
|
|
||||||
|
include RunnableOnDeployConcern
|
||||||
include StatementsHelpersConcern
|
include StatementsHelpersConcern
|
||||||
|
|
||||||
|
# Uncomment only if this task MUST run imperatively on its first deployment.
|
||||||
|
# If possible, leave commented for manual execution later.
|
||||||
|
# run_on_first_deploy
|
||||||
|
|
||||||
def collection
|
def collection
|
||||||
# Collection to be iterated over
|
# Collection to be iterated over
|
||||||
# Must be Active Record Relation or Array
|
# Must be Active Record Relation or Array
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Maintenance::RunnableOnDeployConcern do
|
||||||
|
let(:test_class) do
|
||||||
|
Class.new do
|
||||||
|
include Maintenance::RunnableOnDeployConcern
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.run_on_deploy?' do
|
||||||
|
context 'when run_on_first_deploy is not set' do
|
||||||
|
it 'returns false' do
|
||||||
|
expect(test_class.run_on_deploy?).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when run_on_first_deploy is set' do
|
||||||
|
before do
|
||||||
|
test_class.run_on_first_deploy
|
||||||
|
allow(MaintenanceTasks::TaskDataShow).to receive(:new).and_return(task_data_show)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:task_data_show) { instance_double(MaintenanceTasks::TaskDataShow, completed_runs: completed_runs, active_runs: active_runs) }
|
||||||
|
let(:completed_runs) { double(ActiveRecord::Relation, not_errored: not_errored_runs) }
|
||||||
|
let(:active_runs) { [] }
|
||||||
|
let(:not_errored_runs) { [] }
|
||||||
|
|
||||||
|
context 'when there are no run yet' do
|
||||||
|
it 'returns true' do
|
||||||
|
expect(test_class.run_on_deploy?).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are completed runs without errors' do
|
||||||
|
let(:not_errored_runs) { [instance_double(MaintenanceTasks::Run)] }
|
||||||
|
|
||||||
|
it 'returns false' do
|
||||||
|
expect(test_class.run_on_deploy?).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are active runs' do
|
||||||
|
let(:active_runs) { [instance_double(MaintenanceTasks::Run)] }
|
||||||
|
|
||||||
|
it 'returns false' do
|
||||||
|
expect(test_class.run_on_deploy?).to be false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue