introduce a presisted status for delayed jobs (#8299)
This is currently only employed for work package export jobs but is intended to be used more broadly, e.g. to signal the status of copying a project. [ci skip]pull/8302/head
parent
30289bd71e
commit
1e6c1e7d72
@ -0,0 +1,20 @@ |
||||
module Delayed |
||||
class Job |
||||
class Status < ApplicationRecord |
||||
self.table_name = 'delayed_job_statuses' |
||||
|
||||
belongs_to :reference, polymorphic: true |
||||
belongs_to :job, class_name: '::Delayed::Job' |
||||
|
||||
enum status: { in_queue: 'in_queue', |
||||
error: 'error', |
||||
in_process: 'in_process', |
||||
success: 'success', |
||||
failure: 'failure' } |
||||
|
||||
def self.of_reference(reference) |
||||
where(reference: reference) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,44 @@ |
||||
#-- encoding: UTF-8 |
||||
|
||||
#-- copyright |
||||
# OpenProject is an open source project management software. |
||||
# Copyright (C) 2012-2020 the OpenProject GmbH |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2017 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See docs/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module Cron |
||||
class ClearOldJobStatusJob < CronJob |
||||
# runs at 4:15 nightly |
||||
self.cron_expression = '15 4 * * *' |
||||
|
||||
RETENTION_PERIOD = 2.days.freeze |
||||
|
||||
def perform |
||||
Delayed::Job::Status |
||||
.where(Delayed::Job::Status.arel_table[:updated_at].lteq(Time.now - RETENTION_PERIOD)) |
||||
.destroy_all |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,77 @@ |
||||
#-- encoding: UTF-8 |
||||
|
||||
#-- copyright |
||||
# OpenProject is an open source project management software. |
||||
# Copyright (C) 2012-2020 the OpenProject GmbH |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2017 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See docs/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
# Extends the ActiveJob adapter in use (DelayedJob) by a Status which lives |
||||
# indenpendently from the job itself (which is deleted once successful or after max attempts). |
||||
# That way, the result of a background job is available even after the original job is gone. |
||||
|
||||
ActiveSupport::Notifications.subscribe "perform.active_job" do |job:, exception_object: nil, **_args| |
||||
next unless job.status_reference |
||||
|
||||
# job.provider_job_id is not filled at this point as |
||||
# the ActiveJob adapter for DelayedJob is only setting it |
||||
# on enqueue and enqueue_at. |
||||
if exception_object |
||||
dj_job_attempts = Delayed::Job.where(id: Delayed::Job::Status.of_reference(job.status_reference).select(:job_id)) |
||||
.pluck(:attempts) |
||||
.first || 1 |
||||
|
||||
new_status = if dj_job_attempts + 1 >= Delayed::Worker.max_attempts |
||||
:failure |
||||
else |
||||
:error |
||||
end |
||||
|
||||
Delayed::Job::Status |
||||
.of_reference(job.status_reference) |
||||
.update(status: new_status, |
||||
message: exception_object) |
||||
else |
||||
Delayed::Job::Status |
||||
.of_reference(job.status_reference) |
||||
.update(status: :success) |
||||
end |
||||
end |
||||
|
||||
ActiveSupport::Notifications.subscribe "enqueue.active_job" do |job:, **_args| |
||||
if job.status_reference |
||||
Delayed::Job::Status.create(status: :in_queue, |
||||
reference: job.status_reference, |
||||
job_id: job.provider_job_id) |
||||
end |
||||
end |
||||
|
||||
ActiveSupport::Notifications.subscribe "enqueue_at.active_job" do |job:, **_args| |
||||
if job.status_reference |
||||
Delayed::Job::Status.create(status: :in_queue, |
||||
reference: job.status_reference, |
||||
job_id: job.provider_job_id) |
||||
end |
||||
end |
@ -0,0 +1,25 @@ |
||||
class AddJobStatus < ActiveRecord::Migration[6.0] |
||||
def up |
||||
execute <<-SQL |
||||
CREATE TYPE delayed_job_status AS ENUM ('in_queue', 'error', 'in_process', 'success', 'failure'); |
||||
SQL |
||||
|
||||
create_table :delayed_job_statuses do |t| |
||||
t.references :job |
||||
t.references :reference, polymorphic: true, index: { unique: true } |
||||
t.string :message |
||||
|
||||
t.timestamps |
||||
end |
||||
|
||||
add_column :delayed_job_statuses, :status, :delayed_job_status, default: 'in_queue' |
||||
end |
||||
|
||||
def down |
||||
drop_table :delayed_job_statuses |
||||
|
||||
execute <<-SQL |
||||
DROP TYPE delayed_job_status; |
||||
SQL |
||||
end |
||||
end |
@ -0,0 +1,33 @@ |
||||
#-- copyright |
||||
# OpenProject is an open source project management software. |
||||
# Copyright (C) 2012-2020 the OpenProject GmbH |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2017 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See docs/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
FactoryBot.define do |
||||
factory :delayed_job_status, class: Delayed::Job::Status do |
||||
|
||||
end |
||||
end |
Loading…
Reference in new issue