Skip to content

Instantly share code, notes, and snippets.

@sasha-id
Last active December 15, 2015 20:59
Show Gist options
  • Save sasha-id/5322732 to your computer and use it in GitHub Desktop.
Save sasha-id/5322732 to your computer and use it in GitHub Desktop.
Run rake task from delayed_job
##
# 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