Last active
December 14, 2015 04:38
-
-
Save pake007/5029192 to your computer and use it in GitHub Desktop.
Customize delayed_job for sending email, create association between the job and the object, so that can quickly find the object's jobs.
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
# db/migrate/add_owner_to_delayed_jobs.rb | |
class AddOwnerToDelayedJobs < ActiveRecord::Migration | |
def self.up | |
add_column :delayed_jobs, :owner_type, :string | |
add_column :delayed_jobs, :owner_id, :integer | |
add_index :delayed_jobs, [:owner_type, :owner_id] | |
end | |
def self.down | |
remove_index :delayed_jobs, [:owner_type, :owner_id] | |
remove_column :delayed_jobs, :owner_type | |
remove_column :delayed_jobs, :owner_id | |
end | |
end |
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
# db/migrate/migrate_delayed_job_data_to_add_owner.rb | |
class MigrateDelayedJobDataToAddOwner < ActiveRecord::Migration | |
def self.up | |
Delayed::Job.where(:owner_id => nil).each do |job| | |
begin | |
obj = YAML::load(job.handler).args.first | |
class_name = obj.class.name | |
obj_id = obj.id | |
# assign owner | |
job.owner_type = class_name | |
job.owner_id = obj_id | |
job.save | |
rescue | |
job.delete | |
end | |
end | |
end | |
def self.down | |
end | |
end |
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
# config/initializers/delayed_job.rb | |
class Delayed::Job < ActiveRecord::Base | |
belongs_to :owner, :polymorphic => true | |
attr_accessible :owner, :owner_type, :owner_id | |
end |
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
# config/initializers/active_record.rb | |
class ActiveRecord::Base | |
has_many :jobs, :class_name => "::Delayed::Job", :as => :owner | |
def send_mail_of(method, time=nil, queue=nil, *args) | |
Delayed::Job.transaction do | |
job = Delayed::Job.enqueue(Delayed::PerformableMailer.new(Notifier, | |
method.to_sym, args), :priority => 0, :run_at => time, :queue => queue) | |
job.owner = self | |
job.save | |
end | |
end | |
def self.jobs | |
# to address the STI scenario we use base_class.name. | |
# downside: you have to write extra code to filter STI class specific instance. | |
Delayed::Job.find_all_by_owner_type(self.base_class.name) | |
end | |
end |
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
# Usage, the :auto_special_offer_email is a email function defined in Notifier.rb | |
class User < ActiveRecord::Base | |
def send_auto_emails | |
send_mail_of(:auto_special_offer_email, 15.days.from_now, "pre_register", self) | |
end | |
end | |
# list all jobs of User | |
User.jobs | |
# list jobs of first user | |
User.first.jobs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment