Skip to content

Commit

Permalink
RakeJob: Load the rake task if it wasn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Sep 3, 2024
1 parent 822b67c commit b192b97
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions app/workers/rake_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
require "rake"

##
# Invoke a rake task while safely loading the tasks only once
# Invoke a rake task while loading the tasks on demand
# to ensure they are only loaded once.
module RakeJob
attr_reader :task_name, :args
Expand All @@ -44,29 +44,26 @@ def perform(task_name, *args)
protected

def invoke
load_tasks!
task.invoke *args
ensure
task&.reenable
if (task = load_task)
task.reenable
task.invoke *args
else
OpenProject.logger.error { "Rake task #{task_name} not found for background job." }
end
end

##
# Load tasks if there are none. This should only be run once in an environment
def load_tasks!
Rails.application.load_rake_tasks unless tasks_loaded?
end
# Load tasks if we don't find our task
def load_task
Rails.application.load_rake_tasks unless task_loaded?

##
# Reference to the task name.
# Will raise NameError or NoMethodError depending on what of rake is (not) loaded
def task
Rake::Task[task_name]
task_loaded? && Rake::Task[task_name]
end

##
# Returns whether any task is loaded
# Will raise NameError or NoMethodError depending on what of rake is (not) loaded
def tasks_loaded?
!Rake::Task.tasks.empty?
def task_loaded?
Rake::Task.task_defined?(task_name)
end
end

0 comments on commit b192b97

Please sign in to comment.