resque-kalashnikov 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.gitignore +18 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +99 -0
  7. data/Rakefile +7 -0
  8. data/demo/.gitignore +15 -0
  9. data/demo/Gemfile +31 -0
  10. data/demo/README.rdoc +9 -0
  11. data/demo/Rakefile +7 -0
  12. data/demo/app/assets/images/rails.png +0 -0
  13. data/demo/app/assets/javascripts/application.js +13 -0
  14. data/demo/app/assets/stylesheets/application.css +13 -0
  15. data/demo/app/controllers/application_controller.rb +3 -0
  16. data/demo/app/controllers/test_controller.rb +15 -0
  17. data/demo/app/helpers/application_helper.rb +2 -0
  18. data/demo/app/mailers/.gitkeep +0 -0
  19. data/demo/app/models/.gitkeep +0 -0
  20. data/demo/app/workers/slow_http_request.rb +3 -0
  21. data/demo/config.ru +4 -0
  22. data/demo/config/application.rb +65 -0
  23. data/demo/config/boot.rb +6 -0
  24. data/demo/config/environment.rb +5 -0
  25. data/demo/config/environments/development.rb +26 -0
  26. data/demo/config/environments/production.rb +51 -0
  27. data/demo/config/environments/test.rb +35 -0
  28. data/demo/config/initializers/backtrace_silencers.rb +7 -0
  29. data/demo/config/initializers/inflections.rb +15 -0
  30. data/demo/config/initializers/mime_types.rb +5 -0
  31. data/demo/config/initializers/secret_token.rb +7 -0
  32. data/demo/config/initializers/session_store.rb +8 -0
  33. data/demo/config/initializers/wrap_parameters.rb +10 -0
  34. data/demo/config/locales/en.yml +5 -0
  35. data/demo/config/routes.rb +11 -0
  36. data/demo/db/seeds.rb +7 -0
  37. data/demo/lib/assets/.gitkeep +0 -0
  38. data/demo/lib/tasks/.gitkeep +0 -0
  39. data/demo/log/.gitkeep +0 -0
  40. data/demo/public/404.html +26 -0
  41. data/demo/public/422.html +26 -0
  42. data/demo/public/500.html +25 -0
  43. data/demo/public/favicon.ico +0 -0
  44. data/demo/public/index.html +12 -0
  45. data/demo/public/robots.txt +5 -0
  46. data/demo/script/rails +6 -0
  47. data/demo/script/resque_async.rb +29 -0
  48. data/demo/vendor/assets/javascripts/.gitkeep +0 -0
  49. data/demo/vendor/assets/stylesheets/.gitkeep +0 -0
  50. data/demo/vendor/plugins/.gitkeep +0 -0
  51. data/demo/zeus.json +22 -0
  52. data/lib/event_machine/forced_stop.rb +3 -0
  53. data/lib/resque/catridge.rb +87 -0
  54. data/lib/resque/plugins/resque_kalashnikov/resque_kalashnikov.rb +130 -0
  55. data/lib/resque_kalashnikov.rb +17 -0
  56. data/lib/resque_kalashnikov/delegation.rb +153 -0
  57. data/lib/resque_kalashnikov/http_request.rb +64 -0
  58. data/lib/resque_kalashnikov/railtie.rb +10 -0
  59. data/lib/resque_kalashnikov/server.rb +50 -0
  60. data/lib/resque_kalashnikov/server/views/catridges.erb +57 -0
  61. data/lib/resque_kalashnikov/version.rb +3 -0
  62. data/lib/tasks.rb +34 -0
  63. data/resque-kalashnikov.gemspec +30 -0
  64. data/screenshot.png +0 -0
  65. data/spec/catridge_spec.rb +42 -0
  66. data/spec/http_request_spec.rb +84 -0
  67. data/spec/server_spec.rb +46 -0
  68. data/spec/spec_helper.rb +56 -0
  69. data/spec/support/stub_server.rb +46 -0
  70. data/spec/worker_spec.rb +113 -0
  71. data/tasks/resque_kalashnikov.rake +2 -0
  72. metadata +267 -0
@@ -0,0 +1,35 @@
1
+ ResqueKalashnikovDemo::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Configure static asset server for tests with Cache-Control for performance
11
+ config.serve_static_assets = true
12
+ config.static_cache_control = "public, max-age=3600"
13
+
14
+ # Log error messages when you accidentally call methods on nil
15
+ config.whiny_nils = true
16
+
17
+ # Show full error reports and disable caching
18
+ config.consider_all_requests_local = true
19
+ config.action_controller.perform_caching = false
20
+
21
+ # Raise exceptions instead of rendering exception templates
22
+ config.action_dispatch.show_exceptions = false
23
+
24
+ # Disable request forgery protection in test environment
25
+ config.action_controller.allow_forgery_protection = false
26
+
27
+ # Tell Action Mailer not to deliver emails to the real world.
28
+ # The :test delivery method accumulates sent emails in the
29
+ # ActionMailer::Base.deliveries array.
30
+ config.action_mailer.delivery_method = :test
31
+
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ ResqueKalashnikovDemo::Application.config.secret_token = '99d46a272754fe656bf667aee4b24ae1d3ae48ce1db44b1d4fa3d9eed079e009903a7b2c95517f4c44db074592423168e6815e3b742ce5cb905ee2ffbfeda32a'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ ResqueKalashnikovDemo::Application.config.session_store :cookie_store, key: '_resque-kalashnikov-demo_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # ResqueKalashnikovDemo::Application.config.session_store :active_record_store
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,11 @@
1
+ ResqueKalashnikovDemo::Application.routes.draw do
2
+
3
+ mount Resque::Server, at: '/resque'
4
+
5
+ match "test/home"
6
+
7
+ match "test/slow"
8
+
9
+ match "test/unreliable"
10
+
11
+ end
data/demo/db/seeds.rb ADDED
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
File without changes
File without changes
data/demo/log/.gitkeep ADDED
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ </head>
6
+ <body>
7
+ <h1>Test#home</h1>
8
+ <form method='POST' action='/test/home'>
9
+ <input type='submit' value='Begin!' />
10
+ </form>
11
+ </body>
12
+ </html>
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
data/demo/script/rails ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,29 @@
1
+ RAILS_ENV = ENV['RAILS_ENV'] || 'development_async'
2
+ RAILS_ROOT = Dir.pwd
3
+
4
+ require 'rubygems'
5
+ require 'yaml'
6
+ require 'uri'
7
+ require 'em-resque'
8
+ require 'em-resque/worker_machine'
9
+ require 'em-resque/task_helper'
10
+
11
+ #require 'resque-kalashnikov'
12
+ #require 'resque/plugins/kalashnikov'
13
+ require '/www/resque-kalashnikov/lib/resque/plugins/kalashnikov'
14
+
15
+ #require 'resque-retry'
16
+ #require 'em-synchrony'
17
+ #require 'em-synchrony/connection_pool'
18
+ #require 'em-synchrony/mysql2'
19
+
20
+ require 'debugger'
21
+
22
+ #Dir.glob(File.join(RAILS_ROOT, 'lib', 'async_worker', '**', '*.rb')).sort.each{|f| require File.expand_path(f)}
23
+
24
+ #resque_config = YAML.load_file("#{RAILS_ROOT}/config/resque.yml")
25
+ #proxy_config = YAML.load_file("#{RAILS_ROOT}/config/proxy.yml")
26
+ #PROXY = proxy_config ? proxy_config[RAILS_ENV] : nil
27
+
28
+ opts = TaskHelper.parse_opts_from_env #.merge(:redis => resque_config[RAILS_ENV])
29
+ EM::Resque::WorkerMachine.new(opts).start
File without changes
File without changes
File without changes
data/demo/zeus.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "command": "ruby -rubygems -rzeus/rails -eZeus.go",
3
+
4
+ "plan": {
5
+ "boot": {
6
+ "default_bundle": {
7
+ "development_environment": {
8
+ "prerake": {"rake": []},
9
+ "runner": ["r"],
10
+ "console": ["c"],
11
+ "server": ["s"],
12
+ "generate": ["g"],
13
+ "dbconsole": []
14
+ },
15
+ "test_environment": {
16
+ "cucumber_environment": {"cucumber": []},
17
+ "test_helper": {"test": ["rspec", "testrb"]}
18
+ }
19
+ }
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,3 @@
1
+ module EventMachine
2
+ class ForcedStop < Exception; end
3
+ end
@@ -0,0 +1,87 @@
1
+ module Resque
2
+ class Catridge
3
+ attr_reader :request, :response
4
+
5
+ def initialize(request, response)
6
+ @request = request
7
+ @response = response
8
+ log if misfire?
9
+ inc_stat_counter
10
+ end
11
+
12
+ def reload?
13
+ misfire? && !ran_out_of_ammo?
14
+ end
15
+
16
+ def retries
17
+ self.class.redis.hget("#{self.class.ns}:misfires:#{status}", serialized_request).to_i
18
+ end
19
+
20
+ private
21
+
22
+ def serialized_request
23
+ Resque.encode([request.class.to_s, [request.url, request.http_method, request.reload_opts]])
24
+ end
25
+
26
+ def log
27
+ self.class.redis.hincrby "#{self.class.ns}:misfires:#{status}", serialized_request, 1
28
+ end
29
+
30
+ def inc_stat_counter
31
+ self.class.redis.hincrby "#{self.class.ns}:stat", status, 1
32
+ end
33
+
34
+ def status
35
+ @status ||= response.response_header.status
36
+ end
37
+
38
+ def misfire?
39
+ case status
40
+ when 300..600 then true
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ def ran_out_of_ammo?
47
+ # do something meaningfull
48
+ false
49
+ end
50
+
51
+ class << self
52
+ def ns
53
+ 'kalashnikov'
54
+ end
55
+
56
+ # DI here, please
57
+ def redis
58
+ Resque.redis
59
+ end
60
+
61
+ def stats
62
+ redis.hgetall "#{ns}:stat"
63
+ end
64
+
65
+ def misfire_codes
66
+ redis.keys "#{ns}:misfires:*"
67
+ end
68
+
69
+ def misfire_stats(status)
70
+ redis.hgetall "#{status}"
71
+ #redis.hgetall "#{ns}:misfires:#{status}"
72
+ end
73
+
74
+ def misfire_stats_reset(status, request)
75
+ redis.hdel "#{status}", "#{request}"
76
+ #redis.hgetall "#{ns}:misfires:#{status}"
77
+ end
78
+
79
+ def reset_stats
80
+ stats.keys.each do |http_code|
81
+ redis.del "#{ns}:misfires:#{http_code}"
82
+ end
83
+ redis.del "#{ns}:stat"
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,130 @@
1
+ module Resque::Plugins
2
+ module ResqueKalashnikov
3
+
4
+ def work_with_kalashnikov(interval=5.0, &block)
5
+ interval = Float(interval)
6
+ @fibers = []
7
+ startup
8
+
9
+ loop do
10
+ break if shutdown?
11
+ job = reserve
12
+
13
+ log "got job in worker fiber: #{job.inspect}"
14
+ job.worker = self
15
+
16
+ working_on job
17
+
18
+ if can_do_job_async? job
19
+ @fibers << work_async_on(job, &block)
20
+ else
21
+ work_sync_on(job, &block)
22
+ @child = nil
23
+ end
24
+ monitor(interval)
25
+ done_working
26
+ end
27
+ unregister_worker
28
+
29
+ rescue EM::ForcedStop => e
30
+ # happens in fiber-mode
31
+ # EM has stopped but we need
32
+ # to reconnect to report it
33
+ Resque.redis = Redis.connect
34
+ unregister_worker
35
+ rescue Resque::Helpers::DecodeException => e
36
+ # agian, happens in fork-mode
37
+ raise e unless e.to_s['Redis disconnected']
38
+ Resque.redis = Redis.connect
39
+ unregister_worker
40
+
41
+ rescue Exception => exception
42
+ log exception.to_s
43
+ log exception.backtrace.to_s
44
+ unregister_worker(exception)
45
+ end
46
+
47
+ def inspect_with_kalashnikov
48
+ "#<KalashnikovWorker #{to_s}>"
49
+ end
50
+
51
+ def work_sync_on(job, &block)
52
+ log 'work sync'
53
+ if @child = fork(job)
54
+ srand # Reseeding
55
+ procline "Forked #{@child} at #{Time.now.to_i}"
56
+ begin
57
+ Process.waitpid(@child)
58
+ rescue SystemCallError
59
+ nil
60
+ end
61
+ job.fail(DirtyExit.new($?.to_s)) if $?.signaled?
62
+ else
63
+ unregister_signal_handlers if will_fork? && term_child
64
+ procline "Processing #{job.queue} since #{Time.now.to_i}"
65
+ #reconnect # cannot do it with hiredis
66
+ perform(job, &block)
67
+ exit!(true) if will_fork?
68
+ end
69
+ end
70
+
71
+ def work_async_on(job, &block)
72
+ log "work async"
73
+ Fiber.new do
74
+ perform(job, &block)
75
+ end.tap &:resume
76
+ end
77
+
78
+ # if resque worker gonna to stop - stop EM
79
+ # essentially, fiber-singleton
80
+ def monitor(interval)
81
+ @monitor ||= Fiber.new do
82
+ EM.add_periodic_timer(interval) do
83
+ # monitor itself doesnt count in @fibers
84
+ if (@fibers = @fibers.select(&:alive?)).empty?
85
+ EM.stop if shutdown?
86
+ else
87
+ log! "Big brother says: #{@fibers.size} fibers alive"
88
+ log! ObjectSpace.count_objects.to_s
89
+ end
90
+ end
91
+ end.tap &:resume
92
+ end
93
+
94
+ # test whenether we can do job async
95
+ # based on its name
96
+ def can_do_job_async?(job)
97
+ !! job.queue['async']
98
+ end
99
+
100
+ def reserve_with_kalashnikov
101
+ queues = Resque.queues.map { |q| "queue:#{q}" }
102
+
103
+ # NO block for EM since using hiredis + em-synchrony
104
+ GC.enable
105
+ queue, value = redis.blpop(*queues, 0)
106
+ GC.disable
107
+
108
+ # shit happens if monitor fiber stops EM
109
+ # it should happen only in tests
110
+ raise EM::ForcedStop.new(queue) if queue['Redis disconnected']
111
+
112
+ log "popped: q=#{queue} v=#{value}"
113
+ payload = decode value
114
+ Resque::Job.new queue, payload
115
+ end
116
+
117
+ def self.included(receiver)
118
+ receiver.class_eval do
119
+ alias work_without_kalashnikov work
120
+ alias work work_with_kalashnikov
121
+
122
+ alias inspect_without_kalashnikov inspect
123
+ alias inspect inspect_with_kalashnikov
124
+
125
+ alias reserve_without_kalashnikov reserve
126
+ alias reserve reserve_with_kalashnikov
127
+ end
128
+ end
129
+ end # ResqueKalashnikov
130
+ end # Resque::Plugins