mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Add support for new delay extensions for ActiveRecord and ActionMailer, with example tests in myapp.
This commit is contained in:
parent
4fedfdf049
commit
f214c10af9
8 changed files with 40 additions and 3 deletions
lib
myapp
app
config
|
@ -3,6 +3,9 @@ require 'sidekiq/client'
|
|||
require 'sidekiq/worker'
|
||||
require 'sidekiq/rails' if defined?(::Rails)
|
||||
|
||||
require 'sidekiq/extensions/action_mailer'
|
||||
require 'sidekiq/extensions/active_record'
|
||||
|
||||
module Sidekiq
|
||||
def self.redis
|
||||
@redis ||= Sidekiq::RedisConnection.create
|
||||
|
|
|
@ -24,6 +24,6 @@ module Sidekiq
|
|||
end
|
||||
|
||||
::ActiveRecord::Base.extend(ActiveRecord)
|
||||
::ActiveRecord::Base.include(ActiveRecord)
|
||||
::ActiveRecord::Base.send(:include, ActiveRecord)
|
||||
end
|
||||
end if defined?(::ActiveRecord)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Sidekiq
|
||||
module Extensions
|
||||
class Proxy < BasicObject
|
||||
class Proxy < ::BasicObject
|
||||
def initialize(performable, target)
|
||||
@performable = performable
|
||||
@target = target
|
||||
|
@ -13,7 +13,7 @@ module Sidekiq
|
|||
# to JSON and then deserialized on the other side back into a
|
||||
# Ruby object.
|
||||
obj = [@target, name, args]
|
||||
Sidekiq::Client.push('class' => @performable.name, 'args' => [YAML.dump(obj)])
|
||||
::Sidekiq::Client.push('class' => @performable.name, 'args' => [::YAML.dump(obj)])
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -6,4 +6,21 @@ class WorkController < ApplicationController
|
|||
HardWorker.perform_async('bubba', x)
|
||||
end
|
||||
end
|
||||
|
||||
def email
|
||||
UserMailer.delay.greetings(Time.now)
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
def delayed_post
|
||||
p = Post.first
|
||||
unless p
|
||||
p = Post.create!(:title => "Title!", :body => 'Body!')
|
||||
p2 = Post.create!(:title => "Other!", :body => 'Second Body!')
|
||||
else
|
||||
p2 = Post.second
|
||||
end
|
||||
p.delay.long_method(p2)
|
||||
render :nothing => true
|
||||
end
|
||||
end
|
||||
|
|
9
myapp/app/mailers/user_mailer.rb
Normal file
9
myapp/app/mailers/user_mailer.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class UserMailer < ActionMailer::Base
|
||||
default from: "sidekiq@example.com"
|
||||
|
||||
def greetings(now)
|
||||
@now = now
|
||||
@hostname = `hostname`.strip
|
||||
mail(:to => 'mperham@gmail.com', :subject => 'Ahoy Matey!')
|
||||
end
|
||||
end
|
|
@ -1,2 +1,5 @@
|
|||
class Post < ActiveRecord::Base
|
||||
def long_method(other_post)
|
||||
puts "Running long method with #{self.id} and #{other_post.id}"
|
||||
end
|
||||
end
|
||||
|
|
3
myapp/app/views/user_mailer/greetings.html.erb
Normal file
3
myapp/app/views/user_mailer/greetings.html.erb
Normal file
|
@ -0,0 +1,3 @@
|
|||
<p>
|
||||
Hi Mike, it's <%= @now %> and I'm at <%= @hostname %>.
|
||||
</p>
|
|
@ -3,4 +3,6 @@ require 'resque/server'
|
|||
Myapp::Application.routes.draw do
|
||||
mount Resque::Server.new, :at => '/resque'
|
||||
get "work" => "work#index"
|
||||
get "work/email" => "work#email"
|
||||
get "work/post" => "work#delayed_post"
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue