sidekiq 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq might be problematic. Click here for more details.

Files changed (62) hide show
  1. data/.rvmrc +3 -0
  2. data/COMM-LICENSE +75 -0
  3. data/Changes.md +4 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +32 -0
  6. data/LICENSE +9 -0
  7. data/README.md +64 -0
  8. data/Rakefile +8 -0
  9. data/TODO.md +4 -0
  10. data/bin/client +7 -0
  11. data/bin/sidekiq +19 -0
  12. data/lib/sidekiq.rb +4 -0
  13. data/lib/sidekiq/cli.rb +100 -0
  14. data/lib/sidekiq/client.rb +47 -0
  15. data/lib/sidekiq/manager.rb +131 -0
  16. data/lib/sidekiq/middleware.rb +89 -0
  17. data/lib/sidekiq/processor.rb +37 -0
  18. data/lib/sidekiq/rails.rb +5 -0
  19. data/lib/sidekiq/util.rb +36 -0
  20. data/lib/sidekiq/version.rb +3 -0
  21. data/lib/sidekiq/worker.rb +42 -0
  22. data/myapp/.gitignore +15 -0
  23. data/myapp/Gemfile +5 -0
  24. data/myapp/Gemfile.lock +100 -0
  25. data/myapp/Rakefile +7 -0
  26. data/myapp/app/controllers/application_controller.rb +3 -0
  27. data/myapp/app/helpers/application_helper.rb +2 -0
  28. data/myapp/app/mailers/.gitkeep +0 -0
  29. data/myapp/app/models/.gitkeep +0 -0
  30. data/myapp/app/models/post.rb +2 -0
  31. data/myapp/app/views/layouts/application.html.erb +14 -0
  32. data/myapp/app/workers/hard_worker.rb +6 -0
  33. data/myapp/config.ru +4 -0
  34. data/myapp/config/application.rb +59 -0
  35. data/myapp/config/boot.rb +6 -0
  36. data/myapp/config/database.yml +25 -0
  37. data/myapp/config/environment.rb +5 -0
  38. data/myapp/config/environments/development.rb +37 -0
  39. data/myapp/config/environments/production.rb +67 -0
  40. data/myapp/config/environments/test.rb +37 -0
  41. data/myapp/config/initializers/backtrace_silencers.rb +7 -0
  42. data/myapp/config/initializers/inflections.rb +15 -0
  43. data/myapp/config/initializers/mime_types.rb +5 -0
  44. data/myapp/config/initializers/secret_token.rb +7 -0
  45. data/myapp/config/initializers/session_store.rb +8 -0
  46. data/myapp/config/initializers/wrap_parameters.rb +14 -0
  47. data/myapp/config/locales/en.yml +5 -0
  48. data/myapp/config/routes.rb +58 -0
  49. data/myapp/db/migrate/20120123214055_create_posts.rb +10 -0
  50. data/myapp/db/seeds.rb +7 -0
  51. data/myapp/lib/assets/.gitkeep +0 -0
  52. data/myapp/lib/tasks/.gitkeep +0 -0
  53. data/myapp/log/.gitkeep +0 -0
  54. data/myapp/script/rails +6 -0
  55. data/sidekiq.gemspec +22 -0
  56. data/test/helper.rb +9 -0
  57. data/test/test_client.rb +50 -0
  58. data/test/test_manager.rb +38 -0
  59. data/test/test_middleware.rb +62 -0
  60. data/test/test_processor.rb +52 -0
  61. data/test/timed_queue.rb +42 -0
  62. metadata +179 -0
@@ -0,0 +1,89 @@
1
+ module Sidekiq
2
+ # Middleware is code configured to run before/after
3
+ # a message is processed. It is patterned after Rack
4
+ # middleware. The default middleware chain:
5
+ #
6
+ # Sidekiq::Middleware::Chain.register do
7
+ # use Sidekiq::Airbrake
8
+ # use Sidekiq::ActiveRecord
9
+ # end
10
+ #
11
+ # This is an example of a minimal middleware:
12
+ #
13
+ # class MyHook
14
+ # def initialize(options=nil)
15
+ # end
16
+ # def call(worker, msg)
17
+ # puts "Before work"
18
+ # yield
19
+ # puts "After work"
20
+ # end
21
+ # end
22
+ #
23
+ module Middleware
24
+ class Chain
25
+ def self.register(&block)
26
+ instance_exec(&block)
27
+ end
28
+
29
+ def self.default
30
+ @default ||= [Entry.new(Airbrake), Entry.new(ActiveRecord)]
31
+ end
32
+
33
+ def self.use(klass, *args)
34
+ chain << Entry.new(klass, args)
35
+ end
36
+
37
+ def self.chain
38
+ @chain ||= default
39
+ end
40
+
41
+ def self.retrieve
42
+ Thread.current[:sidekiq_chain] ||= chain.map { |entry| entry.make_new }
43
+ end
44
+ end
45
+
46
+ class Entry
47
+ attr_reader :klass
48
+ def initialize(klass, args = [])
49
+ @klass = klass
50
+ @args = args
51
+ end
52
+
53
+ def make_new
54
+ @klass.new(*@args)
55
+ end
56
+ end
57
+ end
58
+
59
+ class Airbrake
60
+ def initialize(options=nil)
61
+ end
62
+
63
+ def call(worker, msg)
64
+ yield
65
+ rescue => ex
66
+ send_to_airbrake(msg, ex) if defined?(::Airbrake)
67
+ raise
68
+ end
69
+
70
+ private
71
+
72
+ def send_to_airbrake(msg, ex)
73
+ ::Airbrake.notify(:error_class => ex.class.name,
74
+ :error_message => "#{ex.class.name}: #{ex.message}",
75
+ :parameters => msg)
76
+ end
77
+ end
78
+
79
+ class ActiveRecord
80
+ def initialize(options=nil)
81
+ end
82
+
83
+ def call(*)
84
+ yield
85
+ ensure
86
+ ActiveRecord::Base.clear_active_connections! if defined?(::ActiveRecord)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,37 @@
1
+ require 'sidekiq/util'
2
+ require 'sidekiq/middleware'
3
+ require 'celluloid'
4
+
5
+ module Sidekiq
6
+ class Processor
7
+ include Util
8
+ include Celluloid
9
+
10
+ def initialize(boss)
11
+ @boss = boss
12
+ end
13
+
14
+ def process(msg)
15
+ klass = constantize(msg['class'])
16
+ invoke_chain(klass.new, msg)
17
+ @boss.processor_done!(current_actor)
18
+ end
19
+
20
+ def invoke_chain(worker, msg)
21
+ chain = Sidekiq::Middleware::Chain.retrieve.dup
22
+ traverse_chain = lambda do
23
+ if chain.empty?
24
+ worker.perform(*msg['args'])
25
+ else
26
+ chain.shift.call(worker, msg, &traverse_chain)
27
+ end
28
+ end
29
+ traverse_chain.call
30
+ end
31
+
32
+ # See http://github.com/tarcieri/celluloid/issues/22
33
+ def inspect
34
+ "Sidekiq::Processor<#{object_id}>"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ class Rails < ::Rails::Engine
3
+ config.autoload_paths << File.expand_path("#{config.root}/app/workers") if File.exist?("#{config.root}/app/workers")
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ module Sidekiq
2
+ module Util
3
+
4
+ def constantize(camel_cased_word)
5
+ names = camel_cased_word.split('::')
6
+ names.shift if names.empty? || names.first.empty?
7
+
8
+ constant = Object
9
+ names.each do |name|
10
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
11
+ end
12
+ constant
13
+ end
14
+
15
+ def watchdog(last_words)
16
+ yield
17
+ rescue => ex
18
+ STDERR.puts last_words
19
+ STDERR.puts ex
20
+ STDERR.puts ex.backtrace.join("\n")
21
+ end
22
+
23
+ def err(msg)
24
+ STDERR.puts(msg)
25
+ end
26
+
27
+ def log(msg)
28
+ STDOUT.puts(msg) unless $TESTING
29
+ end
30
+
31
+ def verbose(msg)
32
+ STDOUT.puts(msg) if $DEBUG
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Sidekiq
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'sidekiq/client'
2
+
3
+ module Sidekiq
4
+
5
+ ##
6
+ # Include this module in your worker class and you can easily create
7
+ # asynchronous jobs:
8
+ #
9
+ # class HardWorker
10
+ # include Sidekiq::Worker
11
+ #
12
+ # def perform(*args)
13
+ # # do some work
14
+ # end
15
+ # end
16
+ #
17
+ # Then in your Rails app, you can do this:
18
+ #
19
+ # HardWorker.perform_async(1, 2, 3)
20
+ #
21
+ # Note that perform_async is a class method, perform is an instance method.
22
+ module Worker
23
+ def self.included(base)
24
+ base.extend(ClassMethods)
25
+ end
26
+
27
+ def info(msg)
28
+ print "#{msg}\n"
29
+ end
30
+ alias_method :log, :info
31
+
32
+ def debug(msg)
33
+ print "#{msg}\n" if $DEBUG
34
+ end
35
+
36
+ module ClassMethods
37
+ def perform_async(*args)
38
+ Sidekiq::Client.push('class' => self.name, 'args' => args)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '3.2.0'
4
+ gem 'sqlite3'
5
+ gem 'sidekiq', :path => '..'
@@ -0,0 +1,100 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ sidekiq (0.1.0)
5
+ celluloid
6
+ connection_pool
7
+ multi_json
8
+ redis
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actionmailer (3.2.0)
14
+ actionpack (= 3.2.0)
15
+ mail (~> 2.4.0)
16
+ actionpack (3.2.0)
17
+ activemodel (= 3.2.0)
18
+ activesupport (= 3.2.0)
19
+ builder (~> 3.0.0)
20
+ erubis (~> 2.7.0)
21
+ journey (~> 1.0.0)
22
+ rack (~> 1.4.0)
23
+ rack-cache (~> 1.1)
24
+ rack-test (~> 0.6.1)
25
+ sprockets (~> 2.1.2)
26
+ activemodel (3.2.0)
27
+ activesupport (= 3.2.0)
28
+ builder (~> 3.0.0)
29
+ activerecord (3.2.0)
30
+ activemodel (= 3.2.0)
31
+ activesupport (= 3.2.0)
32
+ arel (~> 3.0.0)
33
+ tzinfo (~> 0.3.29)
34
+ activeresource (3.2.0)
35
+ activemodel (= 3.2.0)
36
+ activesupport (= 3.2.0)
37
+ activesupport (3.2.0)
38
+ i18n (~> 0.6)
39
+ multi_json (~> 1.0)
40
+ arel (3.0.0)
41
+ builder (3.0.0)
42
+ celluloid (0.7.2)
43
+ connection_pool (0.1.0)
44
+ erubis (2.7.0)
45
+ hike (1.2.1)
46
+ i18n (0.6.0)
47
+ journey (1.0.0)
48
+ json (1.6.5)
49
+ mail (2.4.1)
50
+ i18n (>= 0.4.0)
51
+ mime-types (~> 1.16)
52
+ treetop (~> 1.4.8)
53
+ mime-types (1.17.2)
54
+ multi_json (1.0.4)
55
+ polyglot (0.3.3)
56
+ rack (1.4.1)
57
+ rack-cache (1.1)
58
+ rack (>= 0.4)
59
+ rack-ssl (1.3.2)
60
+ rack
61
+ rack-test (0.6.1)
62
+ rack (>= 1.0)
63
+ rails (3.2.0)
64
+ actionmailer (= 3.2.0)
65
+ actionpack (= 3.2.0)
66
+ activerecord (= 3.2.0)
67
+ activeresource (= 3.2.0)
68
+ activesupport (= 3.2.0)
69
+ bundler (~> 1.0)
70
+ railties (= 3.2.0)
71
+ railties (3.2.0)
72
+ actionpack (= 3.2.0)
73
+ activesupport (= 3.2.0)
74
+ rack-ssl (~> 1.3.2)
75
+ rake (>= 0.8.7)
76
+ rdoc (~> 3.4)
77
+ thor (~> 0.14.6)
78
+ rake (0.9.2.2)
79
+ rdoc (3.12)
80
+ json (~> 1.4)
81
+ redis (2.2.2)
82
+ sprockets (2.1.2)
83
+ hike (~> 1.2)
84
+ rack (~> 1.0)
85
+ tilt (~> 1.1, != 1.3.0)
86
+ sqlite3 (1.3.5)
87
+ thor (0.14.6)
88
+ tilt (1.3.3)
89
+ treetop (1.4.10)
90
+ polyglot
91
+ polyglot (>= 0.3.1)
92
+ tzinfo (0.3.31)
93
+
94
+ PLATFORMS
95
+ ruby
96
+
97
+ DEPENDENCIES
98
+ rails (= 3.2.0)
99
+ sidekiq!
100
+ sqlite3
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Myapp::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Myapp</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,6 @@
1
+ class HardWorker
2
+ def perform(name, count)
3
+ sleep 0.01
4
+ puts 'done'
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Myapp::Application
@@ -0,0 +1,59 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ if defined?(Bundler)
6
+ # If you precompile assets before deploying to production, use this line
7
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
8
+ # If you want your assets lazily compiled in production, use this line
9
+ # Bundler.require(:default, :assets, Rails.env)
10
+ end
11
+
12
+ module Myapp
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+
18
+ # Custom directories with classes and modules you want to be autoloadable.
19
+ # config.autoload_paths += %W(#{config.root}/extras)
20
+
21
+ # Only load the plugins named here, in the order given (default is alphabetical).
22
+ # :all can be used as a placeholder for all plugins not explicitly named.
23
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24
+
25
+ # Activate observers that should always be running.
26
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27
+
28
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30
+ # config.time_zone = 'Central Time (US & Canada)'
31
+
32
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34
+ # config.i18n.default_locale = :de
35
+
36
+ # Configure the default encoding used in templates for Ruby 1.9.
37
+ config.encoding = "utf-8"
38
+
39
+ # Configure sensitive parameters which will be filtered from the log file.
40
+ config.filter_parameters += [:password]
41
+
42
+ # Use SQL instead of Active Record's schema dumper when creating the database.
43
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
44
+ # like if you have constraints or database-specific column types
45
+ # config.active_record.schema_format = :sql
46
+
47
+ # Enforce whitelist mode for mass assignment.
48
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
49
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
50
+ # parameters by using an attr_accessible or attr_protected declaration.
51
+ # config.active_record.whitelist_attributes = true
52
+
53
+ # Enable the asset pipeline
54
+ config.assets.enabled = true
55
+
56
+ # Version of your assets, change this if you want to expire all your assets
57
+ config.assets.version = '1.0'
58
+ end
59
+ end