sidekiq-kawai 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ log
2
+ TODO
3
+ locks
4
+ *.gem
5
+ *.log
6
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq-kawai (0.1.6)
5
+ activesupport
6
+ sidekiq (>= 2.1.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.2.8)
12
+ i18n (~> 0.6)
13
+ multi_json (~> 1.0)
14
+ celluloid (0.11.1)
15
+ timers (>= 1.0.0)
16
+ connection_pool (0.9.2)
17
+ diff-lcs (1.1.3)
18
+ i18n (0.6.0)
19
+ multi_json (1.3.6)
20
+ rake (0.9.2.2)
21
+ redis (3.0.1)
22
+ redis-namespace (1.2.1)
23
+ redis (~> 3.0.0)
24
+ rspec (2.11.0)
25
+ rspec-core (~> 2.11.0)
26
+ rspec-expectations (~> 2.11.0)
27
+ rspec-mocks (~> 2.11.0)
28
+ rspec-core (2.11.1)
29
+ rspec-expectations (2.11.2)
30
+ diff-lcs (~> 1.1.3)
31
+ rspec-mocks (2.11.2)
32
+ sidekiq (2.2.0)
33
+ celluloid (~> 0.11.1)
34
+ connection_pool (~> 0.9.2)
35
+ multi_json (~> 1)
36
+ redis (~> 3)
37
+ redis-namespace
38
+ timers (1.0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ rake
45
+ rspec
46
+ sidekiq-kawai!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Makarchev K
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ Sidekiq Kawai
2
+ ============
3
+
4
+ Syntax sugar for Sidekiq consumers. Each consumer is a class, with clean interface, and custom logger.
5
+ Usefull when count of different events ~100 and more.
6
+
7
+ ``` ruby
8
+ gem 'sidekiq-kawai'
9
+ ```
10
+
11
+ rails generate sk:add bla
12
+
13
+ And add to config/application.rb
14
+
15
+ config.autoload_paths += %W( #{config.root}/app/models/sidekiq )
16
+
17
+ Consumer
18
+ --------
19
+ app/workers/sk_bla.rb
20
+
21
+ ``` ruby
22
+ class SkBla < SkQueue
23
+
24
+ def some_method1(a, b, c)
25
+ logger.info "async called some_method1 with #{[a, b, c].inspect}"
26
+ end
27
+
28
+ def some_method2(x)
29
+ logger.info "async called some_method2 with #{x.inspect}"
30
+ end
31
+
32
+ end
33
+ ```
34
+
35
+ Insert event into queue like this:
36
+
37
+ SkBla.some_method1(1, 2, 3)
38
+
39
+ or
40
+
41
+ SkBla.add_event(:some_method2, some_x)
42
+
43
+
44
+ Logger for this consumer: Rails.root/log/workers/bla.log
45
+
46
+
47
+
48
+ ### Options
49
+
50
+ ``` ruby
51
+ class SkBla < RkQueue
52
+
53
+ # specify custom logger
54
+ sidekiq_options :logger_path => "#{Rails.root}/log/bla.log"
55
+
56
+ # enables benchmark for each event (into logger)
57
+ sidekiq_options :benchmark => true
58
+
59
+ end
60
+ ```
61
+
62
+
63
+ ### Proxy method to consumer
64
+ Usefull in specs
65
+
66
+ ``` ruby
67
+ SkBla.proxy(:some_method1)
68
+ ```
69
+
70
+ When code call SkBla.some_method1(a,b,c) this would be convert into SkBla.new.some_method1(a,b,c)
71
+
72
+
73
+
74
+ ### Insert events with scheduler
75
+
76
+ ``` ruby
77
+ SkBla.add_event_in(10.seconds, :some_method1, 1, 2, 3)
78
+
79
+ SkBla.enqueue_in(10.seconds, :some_method1, 1, 2, 3)
80
+
81
+ SkBla.some_method2_in(2.minutes.from_now, "call instance method on Worker sheduled async")
82
+
83
+ SkBla.some_method2_at(2.minutes.from_now, "call instance method on Worker sheduled async")
84
+ ```
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ task :default => :spec
9
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,29 @@
1
+ # for Rails 3
2
+ if Rails::VERSION::MAJOR >= 3
3
+
4
+ module Sk
5
+ class AddGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def add_files
9
+ template "consumer.rb", "app/workers/sk_#{file_path}.rb"
10
+ template "spec.rb", "spec/workers/sk_#{file_path}_spec.rb"
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ # for Rails 2.3
18
+ if Rails::VERSION::MAJOR == 2
19
+
20
+ class SkAddGenerator < Rails::Generator::NamedBase
21
+ def manifest
22
+ record do |m|
23
+ m.template "consumer.rb", "app/workers/sk_#{file_path}.rb"
24
+ m.template "spec.rb", "spec/workers/sk_#{file_path}_spec.rb"
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,9 @@
1
+ class Sk<%= class_name %> < SkQueue
2
+
3
+ # insert event like: Sk<%= class_name %>.some_event("haha")
4
+
5
+ def some_event(h)
6
+ logger.info "async called event with #{h.inspect}"
7
+ end
8
+
9
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Sk<%= class_name %> do
4
+ before :each do
5
+ @sk = Sk<%= class_name %>.new
6
+ end
7
+
8
+ it "should not raise" do
9
+ @sk.some_event(1)
10
+ end
11
+
12
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/sk_queue'
@@ -0,0 +1,118 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_support'
3
+ require 'active_support/inflector' unless ''.respond_to?(:underscore)
4
+ require 'sidekiq'
5
+
6
+ class SkQueue
7
+ include Sidekiq::Worker
8
+
9
+ sidekiq_options :retry => true, :benchmark => false
10
+
11
+ def self.extract_queue_name
12
+ name.gsub(/^Sk/, '').underscore.gsub('/', '-').to_sym rescue :default
13
+ end
14
+
15
+ def self.inherited(subclass)
16
+ subclass.class_eval do
17
+ sidekiq_options :queue => extract_queue_name
18
+ sidekiq_options :logger_path => File.expand_path("log/workers/#{queue_name}.log")
19
+ end
20
+ end
21
+
22
+ def perform(method_name, args)
23
+ start_time = benchmark ? Time.now : nil
24
+
25
+ self.send(method_name, *args)
26
+
27
+ logger.info "done #{method_name}, #{"%.6f" % (Time.now - start_time)} s" if benchmark
28
+
29
+ rescue => ex
30
+ logger.error "!Failed event: #{method_name} => #{ex.message}, #{args.inspect}"
31
+ self.class.notify_about_error(ex)
32
+ raise ex
33
+ end
34
+
35
+ def self.benchmark
36
+ get_sidekiq_options['benchmark']
37
+ end
38
+
39
+ def self.benchmark=(val)
40
+ sidekiq_options :benchmark => val
41
+ end
42
+
43
+ def benchmark
44
+ self.class.benchmark
45
+ end
46
+
47
+ def self.queue_name
48
+ get_sidekiq_options['queue']
49
+ end
50
+
51
+ def queue_name
52
+ self.class.queue_name
53
+ end
54
+
55
+ def self.set_queue_name(val)
56
+ sidekiq_options :queue => val
57
+ end
58
+
59
+
60
+ def self.logger_path
61
+ get_sidekiq_options['logger_path']
62
+ end
63
+
64
+ def logger_path
65
+ self.class.logger_path
66
+ end
67
+
68
+
69
+
70
+
71
+ def self.add_event(method_name, *args)
72
+ client_push('class' => self, 'args' => [method_name, args])
73
+ end
74
+
75
+ def self.enqueue(method_name, *args)
76
+ add_event method_name, *args
77
+ end
78
+
79
+ def self.add_event_in(interval, method_name, *args)
80
+ perform_in(interval, method_name, args)
81
+ end
82
+
83
+ def self.enqueue_in(interval, method_name, *args)
84
+ add_event_in(interval, method_name, *args)
85
+ end
86
+
87
+ # Worker.some_method("call new method on Worker async")
88
+ # Worker.some_method_in(2.minutes.from_now,"call new method on Worker sheduled async")
89
+ # Worker.some_method_at(2.minutes.from_now,"call new method on Worker sheduled async")
90
+ def self.method_missing(method_name, *args)
91
+ if method_name.to_s[/\A(\w*)_((at)|(in))\z/]
92
+ add_event_in(args.shift, $1.to_s.to_sym, *args)
93
+ else
94
+ add_event(method_name, *args)
95
+ end
96
+ end
97
+
98
+
99
+
100
+
101
+ def logger
102
+ @logger ||= Logger.new(logger_path).tap do |logger|
103
+ logger.formatter = lambda { |s, d, p, m| "#{d.strftime("%d.%m.%Y %H:%M:%S")} #{m}\n" }
104
+ end
105
+ end
106
+
107
+ def self.proxy(method_name)
108
+ self.should_receive(method_name) do |*data|
109
+ x = Sidekiq.load_json(Sidekiq.dump_json(data))
110
+ self.new.send(method_name, *x)
111
+ end.any_number_of_times
112
+ end
113
+
114
+ def self.notify_about_error(exception)
115
+ # stub
116
+ end
117
+
118
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Sk
3
+ VERSION = "0.1.6"
4
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.dirname(__FILE__) + "/lib/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = %q{sidekiq-kawai}
6
+ s.version = Sk::VERSION
7
+
8
+ s.authors = ["Makarchev Konstantin", "Damir Sharipov"]
9
+
10
+ s.description = %q{Syntax sugar for Sidekiq workers. Each consumer is a class, with clean interface, and custom logger. Usefull when count of different events ~100 and more.}
11
+ s.summary = %q{Syntax sugar for Sidekiq consumers. Each consumer is a class, with clean interface, and custom logger. Usefull when count of different events ~100 and more.}
12
+ s.email = %q{dammer2k@gmail.com}
13
+ s.homepage = %q{https://github.com/kostya/sidekiq-kawai}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activesupport'
21
+ s.add_dependency 'sidekiq', '>= 2.1.0'
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rake"
24
+
25
+ end
@@ -0,0 +1,127 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/test_class'
3
+
4
+ describe "main queue" do
5
+
6
+ it "queue" do
7
+ SkTest.queue_name.should == :test
8
+ end
9
+
10
+ it "queue name A::B::C" do
11
+ A::B::C.queue_name.should == :'a-b-c'
12
+ end
13
+
14
+ it "aliases for queues" do
15
+ SkTest.queue_name.should == :test
16
+ SkTest.new.queue_name.should == :test
17
+ end
18
+
19
+ it "set queue_name" do
20
+ SkTest.set_queue_name :haha
21
+ SkTest.queue_name.should == :haha
22
+ SkTest.new.queue_name.should == :haha
23
+ end
24
+
25
+ it "set queue name inside class" do
26
+ class Sk2 < SkTest
27
+ set_queue_name :jopa
28
+ end
29
+
30
+ Sk2.queue_name.should == :jopa
31
+ end
32
+
33
+ it "should enqueue defined event" do
34
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:bla, [1, 'a', []]])
35
+ SkTest.bla(1, 'a', [])
36
+ end
37
+
38
+ it "insert empty event" do
39
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:bla, []])
40
+ SkTest.bla
41
+ end
42
+
43
+ it "should enqueue undefined event" do
44
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:bl, [1]])
45
+ SkTest.bl(1)
46
+ end
47
+
48
+ it "should enqueue undefined event" do
49
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:bl2, [{}]])
50
+ SkTest.bl2({})
51
+ end
52
+
53
+ it "should insert event with custom method" do
54
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:super, [1,2,3]])
55
+ SkTest.add_event(:super, 1, 2, 3)
56
+ end
57
+
58
+ it "should insert event with custom method" do
59
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:super, [[1,2,3]]])
60
+ SkTest.add_event(:super, [1, 2, 3])
61
+ end
62
+
63
+ it "should insert event with custom method enqueue" do
64
+ SkTest.should_receive(:client_push).with('class' => SkTest, 'args' => [:super, [1,2,3]])
65
+ SkTest.enqueue(:super, 1, 2, 3)
66
+ end
67
+
68
+ it "enqueue in" do
69
+ SkTest.should_receive(:perform_in).with(10, :super, [1, 2, 3])
70
+ SkTest.enqueue_in(10, :super, 1, 2, 3)
71
+ end
72
+
73
+ it "add event in" do
74
+ SkTest.should_receive(:perform_in).with(10, :super, [1, 2, 3])
75
+ SkTest.add_event_in(10, :super, 1, 2, 3)
76
+ end
77
+
78
+ it "abstract enqueue in" do
79
+ SkTest.should_receive(:perform_in).with(10, :bla, [1, 2, 3])
80
+ SkTest.bla_in(10, 1, 2, 3)
81
+ end
82
+
83
+ it "abstract enqueue at" do
84
+ SkTest.should_receive(:perform_in).with(10, :bla, [1, 2, 3])
85
+ SkTest.bla_at(10, 1, 2, 3)
86
+ end
87
+
88
+ describe "consume" do
89
+ before :each do
90
+ @bla = SkTest.new
91
+ end
92
+
93
+ it "should call our event" do
94
+ @bla.should_receive(:bla).with(1, 'a', [])
95
+ @bla.perform(:bla, [1, 'a', []])
96
+ end
97
+
98
+ it "should call our another event" do
99
+ @bla.should_receive(:bl).with(1)
100
+ @bla.perform('bl', [1])
101
+ end
102
+
103
+ it "should call our another event" do
104
+ @bla.should_receive(:bl2).with({})
105
+ @bla.perform('bl2', [{}])
106
+ end
107
+
108
+ it "should call our another event" do
109
+ @bla.should_receive(:bl2).with([1,2,3])
110
+ @bla.perform('bl2', [[1,2,3]])
111
+ end
112
+
113
+ it "raised when method undefined" do
114
+ lambda do
115
+ SkTest.new.perform('blasdfds', [1])
116
+ end.should raise_error
117
+ end
118
+ end
119
+
120
+ it "should proxy consumer" do
121
+ SkTest.proxy(:ptest)
122
+ SkTest.ptest(111, 'abc').should == 10
123
+ $a.should == 111
124
+ $b.should == 'abc'
125
+ end
126
+
127
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ ENV['RAILS_ENV'] ||= 'test'
5
+
6
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
7
+ require 'sk_queue'
@@ -0,0 +1,28 @@
1
+ class SkTest < SkQueue
2
+
3
+ sidekiq_options :logger_path => "test.log"
4
+ sidekiq_options :benchmark => true
5
+
6
+ def bla(a, b, c)
7
+ @hah = [a, b, c]
8
+ logger.info "bla #{@hah.inspect}"
9
+ end
10
+
11
+ def ptest(a, b)
12
+ $a = a
13
+ $b = b
14
+ 10
15
+ end
16
+
17
+ def self.to_ary
18
+ super
19
+ end
20
+
21
+ end
22
+
23
+ module A
24
+ module B
25
+ class C < SkQueue
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-kawai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Makarchev Konstantin
9
+ - Damir Sharipov
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-17 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: sidekiq
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 2.1.0
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 2.1.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: Syntax sugar for Sidekiq workers. Each consumer is a class, with clean
80
+ interface, and custom logger. Usefull when count of different events ~100 and more.
81
+ email: dammer2k@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - LICENSE
90
+ - README.md
91
+ - Rakefile
92
+ - lib/generators/sk/add_generator.rb
93
+ - lib/generators/sk/templates/consumer.rb
94
+ - lib/generators/sk/templates/spec.rb
95
+ - lib/sidekiq-kawai.rb
96
+ - lib/sk_queue.rb
97
+ - lib/version.rb
98
+ - sidekiq-kawai.gemspec
99
+ - spec/sk_queue_spec.rb
100
+ - spec/spec_helper.rb
101
+ - spec/test_class.rb
102
+ homepage: https://github.com/kostya/sidekiq-kawai
103
+ licenses: []
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 4705233
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 4705233
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.24
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Syntax sugar for Sidekiq consumers. Each consumer is a class, with clean
132
+ interface, and custom logger. Usefull when count of different events ~100 and more.
133
+ test_files: []