Skip to content

Commit

Permalink
Revert "Merge pull request opf#12998 from opf/task/48717-replace-dela…
Browse files Browse the repository at this point in the history
…yedjob"
  • Loading branch information
Eric-Guo committed Mar 27, 2024
1 parent 0c12b51 commit d111edb
Show file tree
Hide file tree
Showing 91 changed files with 1,202 additions and 1,007 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ gem "multi_json", "~> 1.15.0"
gem "oj", "~> 3.16.0"

gem "daemons"
gem "good_job", "= 3.26.2" # update should be done manually in sync with saas-openproject version.
gem "delayed_cron_job", "~> 0.9.0"
gem "delayed_job_active_record", "~> 4.1.5"

gem "rack-protection", "~> 3.2.0"

Expand Down
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: bundle exec rails server -p 3000 -b ${HOST:="127.0.0.1"} --environment ${RAILS_ENV:="development"}
angular: yarn run serve
worker: bundle exec good_job start
worker: bundle exec rake jobs:work
4 changes: 1 addition & 3 deletions app/contracts/settings/working_days_params_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def working_days_are_present
end

def unique_job
if GoodJob::Job
.where(finished_at: nil)
.exists?(job_class: WorkPackages::ApplyWorkingDaysChangeJob.name)
if WorkPackages::ApplyWorkingDaysChangeJob.scheduled?
errors.add :base, :previous_working_day_changes_unprocessed
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/seeders/root_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ def prepare_seed!
ActionMailer::Base.perform_deliveries = false

# Avoid asynchronous DeliverWorkPackageCreatedJob
previous_execution_mode = Rails.configuration.good_job.execution_mode
Rails.configuration.good_job.execution_mode = :inline
previous_delay_jobs = Delayed::Worker.delay_jobs
Delayed::Worker.delay_jobs = false

yield
ensure
ActionMailer::Base.perform_deliveries = previous_perform_deliveries
Rails.configuration.good_job.execution_mode = previous_execution_mode
Delayed::Worker.delay_jobs = previous_delay_jobs
end

def seed_basic_data
Expand Down
4 changes: 0 additions & 4 deletions app/workers/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ def reload_mailer_settings!
Setting.reload_mailer_settings!
end

def job_scheduled_at
GoodJob::Job.where(id: job_id).pick(:scheduled_at)
end

private

def prepare_job_context
Expand Down
7 changes: 5 additions & 2 deletions app/workers/attachments/cleanup_uncontainered_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
# See COPYRIGHT and LICENSE files for more details.
#++

class Attachments::CleanupUncontaineredJob < ApplicationJob
class Attachments::CleanupUncontaineredJob < Cron::CronJob
queue_with_priority :low

# runs at 10:03 pm
self.cron_expression = "03 22 * * *"

def perform
Attachment
.where(container: nil)
Expand All @@ -47,7 +50,7 @@ def too_old
attachment_table = Attachment.arel_table

attachment_table[:created_at]
.lteq(Time.zone.now - OpenProject::Configuration.attachments_grace_period.minutes)
.lteq(Time.now - OpenProject::Configuration.attachments_grace_period.minutes)
.to_sql
end
end
41 changes: 41 additions & 0 deletions app/workers/concerns/scheduled_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# OpenProject is an open source project management software.
# Copyright (C) 2010-2022 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-2013 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 COPYRIGHT and LICENSE files for more details.

module ScheduledJob
extend ActiveSupport::Concern

class_methods do
##
# Is there a job scheduled?
def scheduled?
delayed_job_query.exists?
end

def delayed_job_query
Delayed::Job.where("handler LIKE ?", "%job_class: #{name}%")
end
end
end
5 changes: 4 additions & 1 deletion app/workers/cron/clear_old_sessions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#++

module Cron
class ClearOldSessionsJob < ApplicationJob
class ClearOldSessionsJob < CronJob
include ::RakeJob

# runs at 1:15 nightly
self.cron_expression = "15 1 * * *"

def perform
super("db:sessions:expire", 7)
end
Expand Down
5 changes: 4 additions & 1 deletion app/workers/cron/clear_tmp_cache_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#++

module Cron
class ClearTmpCacheJob < ApplicationJob
class ClearTmpCacheJob < CronJob
include ::RakeJob

# runs at 02:45 sundays
self.cron_expression = "45 2 * * 7"

def perform
super("tmp:cache:clear")
end
Expand Down
5 changes: 4 additions & 1 deletion app/workers/cron/clear_uploaded_files_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#++

module Cron
class ClearUploadedFilesJob < ApplicationJob
class ClearUploadedFilesJob < CronJob
include ::RakeJob

# Runs 23pm fridays
self.cron_expression = "0 23 * * 5"

def perform
super("attachments:clear")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,53 @@
# 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.
# See COPYRIGHT and LICENSE files for more details.
#++
module OpenProject
module HealthChecks
class PumaCheck < OkComputer::Check
attr_reader :threshold

def initialize(threshold = OpenProject::Configuration.health_checks_backlog_threshold)
@threshold = threshold.to_i
@applicable = Object.const_defined?("Puma::Server") && !Puma::Server.current.nil?
super()
end
module Cron
class CronJob < ApplicationJob
class_attribute :cron_expression

# List of registered jobs, requires eager load in dev(!)
class_attribute :registered_jobs, default: []

def check
stats = self.stats
include ScheduledJob

return mark_message "N/A as Puma is not used." if stats.nil?
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)

if stats[:running] > 0
mark_message "Puma is running"
else
mark_failure
mark_message "Puma is not running"
registered_jobs << clz
end
end

if stats[:backlog] < threshold
mark_message "Backlog ok"
else
mark_failure
mark_message "Backlog congested"
def schedule_registered_jobs!
registered_jobs.each do |job_class|
job_class.ensure_scheduled!
end
end

def stats
return nil unless applicable?
##
# Ensure the job is scheduled unless it is already
def ensure_scheduled!
# Ensure scheduled only once
return if scheduled?

server = Puma::Server.current
return nil if server.nil?
Rails.logger.info { "Scheduling #{name} recurrent background job." }
set(cron: cron_expression).perform_later
end

{
backlog: server.backlog || 0,
running: server.running || 0,
pool_capacity: server.pool_capacity || 0,
max_threads: server.max_threads || 0
}
##
# Remove the scheduled job, if any
def remove
delayed_job&.destroy
end

def applicable?
!!@applicable
def delayed_job
delayed_job_query.first
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/workers/ldap/synchronization_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
#++

module Ldap
class SynchronizationJob < ApplicationJob
class SynchronizationJob < ::Cron::CronJob
# Run once per night at 11:30pm
self.cron_expression = "30 23 * * *"

def perform
run_user_sync
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@

module Notifications
# Creates date alert jobs for users whose local time is 1:00 am.
class ScheduleDateAlertsNotificationsJob < ApplicationJob
class ScheduleDateAlertsNotificationsJob < Cron::CronJob
# runs every quarter of an hour, so 00:00, 00:15,..., 15:30, 15:45, 16:00, ...
self.cron_expression = "*/15 * * * *"

def perform
return unless EnterpriseToken.allows_to?(:date_alerts)

Service.new(times_from_scheduled_to_execution).call
service = Service.new(times_from_scheduled_to_execution)
service.call
end

# Returns times from scheduled execution time to current time in 15 minutes
Expand All @@ -53,7 +57,7 @@ def times_from_scheduled_to_execution
end

def scheduled_time
job_scheduled_at.then { |t| t.change(min: t.min / 15 * 15) }
self.class.delayed_job.run_at.then { |t| t.change(min: t.min / 15 * 15) }
end
end
end
13 changes: 9 additions & 4 deletions app/workers/notifications/schedule_reminder_mails_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@
#++

module Notifications
class ScheduleReminderMailsJob < ApplicationJob
class ScheduleReminderMailsJob < Cron::CronJob
# runs every quarter of an hour, so 00:00, 00:15...
self.cron_expression = "*/15 * * * *"

def perform
User.having_reminder_mail_to_send(job_scheduled_at)
.pluck(:id)
.each do |user_id|
User.having_reminder_mail_to_send(run_at).pluck(:id).each do |user_id|
Mails::ReminderJob.perform_later(user_id)
end
end

def run_at
self.class.delayed_job.run_at
end
end
end
5 changes: 4 additions & 1 deletion app/workers/oauth/cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#++

module OAuth
class CleanupJob < ApplicationJob
class CleanupJob < ::Cron::CronJob
include ::RakeJob

# runs at 1:52 nightly
self.cron_expression = "52 1 * * *"

queue_with_priority :low

def perform
Expand Down
5 changes: 4 additions & 1 deletion app/workers/paper_trail_audits/cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
#++

module PaperTrailAudits
class CleanupJob < ApplicationJob
class CleanupJob < ::Cron::CronJob
# runs at 4:03 on Saturday
self.cron_expression = "3 4 * * 6"

# Clean any paper trails older than 60 days
def perform
::PaperTrailAudit
Expand Down
Loading

0 comments on commit d111edb

Please sign in to comment.