Last active
December 15, 2015 20:59
-
-
Save sasha-id/5322732 to your computer and use it in GitHub Desktop.
Run rake task from delayed_job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Stick an Object in the queue. | |
# Delayed::Job.enqueue(DelayedRake.new("util:mongrel_init:name")) | |
require 'rake' | |
require 'fileutils' | |
class DelayedRake | |
def initialize(task, options = {}) | |
@task = task | |
@options = options | |
end | |
## | |
# Called by Delayed::Job. | |
def perform | |
FileUtils.cd Rails.root | |
@rake = Rake::Application.new | |
Rake.application = @rake | |
# Load specified task | |
task_name = /^([^:]+):.*?/.match(@task)[1] | |
Dir[ "./lib/tasks/**/#{task_name}.rake" ].each { |ext| load ext } | |
ENV['RAILS_ENV'] = Rails.env | |
begin | |
Rails.logger.info "Rake Task (#{Rails.env}): \"#{@task}\" #{@options.inspect} }" | |
@rake[@task].execute @options | |
rescue => e | |
Rails.logger.error "[ERROR]: task \"#{@task}\" failed. #{e}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment