Merge pull request #8254 from opf/feature/delayed-cron-jobs
Add cron job functionality to delayed_jobpull/8258/head
commit
400646d258
@ -0,0 +1,42 @@ |
||||
#-- 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 ClearOldSessionsJob < CronJob |
||||
include ::RakeJob |
||||
|
||||
# runs at 1:15 nightly |
||||
self.cron_expression = '15 1 * * *' |
||||
|
||||
def perform |
||||
super 'db:sessions:expire', 7 |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,42 @@ |
||||
#-- 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 ClearTmpCacheJob < CronJob |
||||
include ::RakeJob |
||||
|
||||
# runs at 02:45 sundays |
||||
self.cron_expression = '45 2 * * 7' |
||||
|
||||
def perform |
||||
super 'tmp:cache:clear' |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,42 @@ |
||||
#-- 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 ClearUploadedFilesJob < CronJob |
||||
include ::RakeJob |
||||
|
||||
# Runs 23pm fridays |
||||
self.cron_expression = '0 23 * * 5' |
||||
|
||||
def perform |
||||
super 'attachments:clear' |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,78 @@ |
||||
#-- 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 CronJob < ApplicationJob |
||||
class_attribute :cron_expression |
||||
|
||||
# List of registered jobs, requires eager load in dev(!) |
||||
class_attribute :registered_jobs, default: [] |
||||
|
||||
class << self |
||||
## |
||||
# Register new job class(es) |
||||
def register!(*job_classes) |
||||
Array(job_classes).each do |clz| |
||||
raise ArgumentError, "Needs to be subclass of ::Cron::CronJob" unless clz.ancestors.include?(self) |
||||
|
||||
registered_jobs << clz |
||||
end |
||||
end |
||||
|
||||
## |
||||
# Ensure the job is scheduled unless it is already |
||||
def ensure_scheduled! |
||||
# Ensure scheduled only onced |
||||
return if scheduled? |
||||
|
||||
Rails.logger.info { "Scheduling #{name} recurrent background job." } |
||||
set(cron: cron_expression).perform_later |
||||
end |
||||
|
||||
## |
||||
# Remove the scheduled job, if any |
||||
def remove |
||||
delayed_job&.destroy |
||||
end |
||||
|
||||
## |
||||
# Is there a job scheduled? |
||||
def scheduled? |
||||
delayed_job.present? |
||||
end |
||||
|
||||
def delayed_job |
||||
Delayed::Job |
||||
.where('handler LIKE ?', "%job_class: #{name}%") |
||||
.first |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,9 @@ |
||||
# Register "Cron-like jobs" |
||||
|
||||
OpenProject::Application.configure do |application| |
||||
application.config.to_prepare do |
||||
::Cron::CronJob.register! ::Cron::ClearOldSessionsJob, |
||||
::Cron::ClearTmpCacheJob, |
||||
::Cron::ClearUploadedFilesJob |
||||
end |
||||
end |
@ -0,0 +1,9 @@ |
||||
class AddCronToDelayedJobs < ActiveRecord::Migration[6.0] |
||||
def self.up |
||||
add_column :delayed_jobs, :cron, :string |
||||
end |
||||
|
||||
def self.down |
||||
remove_column :delayed_jobs, :cron |
||||
end |
||||
end |
@ -0,0 +1,50 @@ |
||||
#-- 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 LdapGroups |
||||
class SynchronizationJob < ::Cron::CronJob |
||||
# Run every 30 minutes |
||||
self.cron_expression = '*/30 * * * *' |
||||
|
||||
def perform |
||||
return unless EnterpriseToken.allows_to?(:ldap_groups) |
||||
|
||||
begin |
||||
LdapAuthSource.find_each do |ldap| |
||||
Rails.logger.info { "[LDAP groups] Start synchronization for ldap auth source #{ldap.name}" } |
||||
OpenProject::LdapGroups::Synchronization.new(ldap) |
||||
end |
||||
rescue StandardError => e |
||||
msg = "[LDAP groups] Failed to run LDAP group synchronization. #{e.class.name}: #{e.message}" |
||||
Rails.logger.error msg |
||||
end |
||||
end |
||||
end |
||||
end |
@ -1,3 +0,0 @@ |
||||
APP_NAME="_APP_NAME_" |
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
||||
15 1 * * * root ${APP_NAME} run rake -s db:sessions:expire[7] >> /var/log/${APP_NAME}/cron-clear-old-sessions.log 2>&1 |
@ -1,3 +0,0 @@ |
||||
APP_NAME="_APP_NAME_" |
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
||||
45 2 * * 7 root ${APP_NAME} run rake -s tmp:cache:clear >> /var/log/${APP_NAME}/cron-clear-tmp-files.log 2>&1 |
@ -1,3 +0,0 @@ |
||||
APP_NAME="_APP_NAME_" |
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
||||
0 23 * * 5 root ${APP_NAME} run rake -s attachments:clear >> /var/log/${APP_NAME}/cron-clear-uploaded-files.log 2>&1 |
@ -1,3 +0,0 @@ |
||||
APP_NAME="_APP_NAME_" |
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
||||
55 * * * * root ${APP_NAME} run rake -s openproject:cron:hourly >> /var/log/${APP_NAME}/cron-hourly.log 2>&1 |
Loading…
Reference in new issue