resque-kawai 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ log
2
+ TODO
3
+ locks
4
+ *.gem
5
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Will automatically pull in this gem and all its
4
+ # dependencies specified in the gemspec
5
+ gem "resque-kawai", :path => File.expand_path("..", __FILE__)
6
+
7
+ gem 'activesupport'
8
+ # These are development dependencies
9
+ gem "rspec"
10
+ gem 'rake'
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resque-kawai (0.1)
5
+ activesupport
6
+ resque
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.2.3)
12
+ i18n (~> 0.6)
13
+ multi_json (~> 1.0)
14
+ diff-lcs (1.1.3)
15
+ i18n (0.6.0)
16
+ multi_json (1.3.2)
17
+ rack (1.4.1)
18
+ rack-protection (1.2.0)
19
+ rack
20
+ rake (0.9.2.2)
21
+ redis (2.2.2)
22
+ redis-namespace (1.0.3)
23
+ redis (< 3.0.0)
24
+ resque (1.20.0)
25
+ multi_json (~> 1.0)
26
+ redis-namespace (~> 1.0.2)
27
+ sinatra (>= 0.9.2)
28
+ vegas (~> 0.1.2)
29
+ rspec (2.9.0)
30
+ rspec-core (~> 2.9.0)
31
+ rspec-expectations (~> 2.9.0)
32
+ rspec-mocks (~> 2.9.0)
33
+ rspec-core (2.9.0)
34
+ rspec-expectations (2.9.1)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.9.0)
37
+ sinatra (1.3.2)
38
+ rack (~> 1.3, >= 1.3.6)
39
+ rack-protection (~> 1.2)
40
+ tilt (~> 1.3, >= 1.3.3)
41
+ tilt (1.3.3)
42
+ vegas (0.1.11)
43
+ rack (>= 1.0.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ activesupport
50
+ rake
51
+ resque-kawai!
52
+ rspec
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ Resque Kawai
2
+ ============
3
+
4
+ Syntax sugar for Resque consumers. Each consumer is a class, with clean interface, and custom logger.
5
+
6
+ ``` ruby
7
+ gem 'resque-kawai'
8
+ ```
9
+
10
+ rails generate rq:add bla
11
+
12
+ And add to config/application.rb
13
+
14
+ config.autoload_paths += %W( #{config.root}/app/models/resque )
15
+
16
+ Consumer
17
+ --------
18
+ app/models/resque/rq_bla.rb
19
+
20
+ ``` ruby
21
+ class RqBla < RqQueue
22
+
23
+ def some_method1(a, b, c)
24
+ logger.info "async called some_method1 with #{[a, b, c].inspect}"
25
+ end
26
+
27
+ def some_method2(x)
28
+ logger.info "async called some_method2 with #{x.inspect}"
29
+ end
30
+
31
+ end
32
+ ```
33
+
34
+ Insert event into queue like this:
35
+
36
+ RqBla.some_method1(1, 2, 3)
37
+
38
+ or
39
+
40
+ RqBla.add_event(:some_method2, some_x)
41
+
42
+
43
+ Logger for this consumer: Rails.root/log/resque/bla.log
44
+
45
+
46
+
47
+ ### Options
48
+
49
+ ``` ruby
50
+ class RqBla < RqQueue
51
+
52
+ # specify custom logger
53
+ self.logger_path = "#{Rails.root}/log/bla.log"
54
+
55
+ # enables benchmark for each event (into logger)
56
+ self.benchmark = true
57
+
58
+ end
59
+ ```
60
+
61
+
62
+ ### Proxy method to consumer
63
+ Usefull in specs
64
+
65
+ ``` ruby
66
+ RqBla.proxy(:some_method1)
67
+ ```
68
+
69
+ When code call RqBla.some_method1(a,b,c) this would be convert into RqBla.new.some_method1(a,b,c)
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task :default => :spec
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ require 'rubygems/package_task'
10
+ require 'rubygems/specification'
11
+
12
+ spec = eval(File.read('resque-kawai.gemspec'))
13
+
14
+ Gem::PackageTask.new(spec) do |pkg|
15
+ pkg.gem_spec = spec
16
+ end
@@ -0,0 +1,29 @@
1
+ # for Rails 3
2
+ if Rails::VERSION::MAJOR >= 3
3
+
4
+ module Rq
5
+ class AddGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def add_files
9
+ template "consumer.rb", "app/models/resque/rq_#{file_path}.rb"
10
+ template "spec.rb", "spec/models/resque/rq_#{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 RqAddGenerator < Rails::Generator::NamedBase
21
+ def manifest
22
+ record do |m|
23
+ m.template "consumer.rb", "app/models/bin/rq_#{file_path}.rb"
24
+ m.template "spec.rb", "spec/models/bin/rq_#{file_path}_spec.rb"
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,9 @@
1
+ class Rq<%= class_name %> < RqQueue
2
+
3
+ # insert event like: Rg<%= 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 Rq<%= class_name %> do
4
+ before :each do
5
+ @rq = Rq<%= class_name %>.instance
6
+ end
7
+
8
+ it "should not raise" do
9
+ @rq.some_event(1)
10
+ end
11
+
12
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/rq_queue'
data/lib/rq_queue.rb ADDED
@@ -0,0 +1,74 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_support/inflector'
3
+ require 'resque'
4
+ require 'logger'
5
+
6
+ class RqQueue
7
+ @queue = :default
8
+
9
+ attr_accessor :logger
10
+
11
+ def self.inherited(subclass)
12
+ subclass.instance_variable_set('@queue', subclass.extract_queue_name)
13
+ end
14
+
15
+ def self.extract_queue_name
16
+ name.gsub(/Rq/, '').underscore.to_sym
17
+ end
18
+
19
+ def self.add_event(method_name, *args)
20
+ Resque.enqueue(self, method_name.to_s, args)
21
+ end
22
+
23
+ def self.method_missing(method, *args)
24
+ add_event(method, *args)
25
+ end
26
+
27
+ def self.logger
28
+ @logger ||= Logger.new(logger_path).tap do |logger|
29
+ logger.formatter = lambda { |s, d, p, m| "#{d.strftime("%d.%m.%Y %H:%M:%S")} #{m}\n" }
30
+ end
31
+ end
32
+
33
+ def self.instance
34
+ @instance ||= self.new
35
+ end
36
+
37
+ def self.perform(method_name, args)
38
+ start_time = benchmark ? Time.now : nil
39
+
40
+ instance.send(method_name, *args)
41
+
42
+ logger.info "done #{method_name}, #{"%.6f" % (Time.now - start_time)} s" if benchmark
43
+
44
+ rescue => ex
45
+ logger.error "Failed event: #{ex.message}"
46
+ raise ex
47
+ end
48
+
49
+ # proxing method for tests
50
+ def self.proxy(method_name)
51
+ self.should_receive(method_name) do |*data|
52
+ self.instance.send(method_name, *data)
53
+ end.any_number_of_times
54
+ end
55
+
56
+ class << self
57
+ def logger_path=(_logger_path)
58
+ @logger_path = _logger_path
59
+ end
60
+
61
+ def logger_path
62
+ @logger_path ||= begin
63
+ "#{Rails.root}/log/resque/#{extract_queue_name}.log"
64
+ end
65
+ end
66
+
67
+ attr_accessor :benchmark
68
+ end
69
+
70
+ def initialize
71
+ self.logger = self.class.logger
72
+ end
73
+
74
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Rq
3
+ VERSION = "0.1"
4
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.dirname(__FILE__) + "/lib/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = %q{resque-kawai}
6
+ s.version = Rq::VERSION
7
+
8
+ s.authors = ["Makarchev Konstantin"]
9
+ s.autorequire = %q{resque-kawai.rb}
10
+
11
+ s.description = %q{Syntax sugar for Resque consumers. Each consumer is a class, with clean interface, and custom logger.}
12
+ s.summary = %q{Syntax sugar for Resque consumers. Each consumer is a class, with clean interface, and custom logger.}
13
+
14
+ s.email = %q{kostya27@gmail.com}
15
+ s.homepage = %q{http://github.com/kostya/resque-kawai}
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activesupport'
23
+ s.add_dependency 'resque'
24
+ s.add_development_dependency "rspec"
25
+
26
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/test_class'
3
+
4
+ describe RqQueue do
5
+ it "should enqueue defined event" do
6
+ Resque.should_receive(:enqueue).with(RqTest, 'bla', [1, 'a', []])
7
+ RqTest.bla(1, 'a', [])
8
+ end
9
+
10
+ it "insert empty event" do
11
+ Resque.should_receive(:enqueue).with(RqTest, 'bl', [])
12
+ RqTest.bl
13
+ end
14
+
15
+ it "should enqueue undefined event" do
16
+ Resque.should_receive(:enqueue).with(RqTest, 'bl', [1])
17
+ RqTest.bl(1)
18
+ end
19
+
20
+ it "should enqueue undefined event" do
21
+ Resque.should_receive(:enqueue).with(RqTest, 'bl2', [{}])
22
+ RqTest.bl2({})
23
+ end
24
+
25
+ it "should insert event with custom method" do
26
+ Resque.should_receive(:enqueue).with(RqTest, 'super', [1, 2, 3])
27
+ RqTest.add_event(:super, 1, 2, 3)
28
+ end
29
+
30
+ it "should insert event with custom method" do
31
+ Resque.should_receive(:enqueue).with(RqTest, 'super', [[1, 2, 3]])
32
+ RqTest.add_event(:super, [1, 2, 3])
33
+ end
34
+
35
+ describe "consume" do
36
+ before :each do
37
+ @bla = RqTest.new
38
+ RqTest.stub!(:instance).and_return(@bla)
39
+ end
40
+
41
+ it "should call our event" do
42
+ @bla.should_receive(:bla).with(1, 'a', [])
43
+ RqTest.perform('bla', [1, 'a', []])
44
+ end
45
+
46
+ it "should call our another event" do
47
+ @bla.should_receive(:bl).with(1)
48
+ RqTest.perform('bl', [1])
49
+ end
50
+
51
+ it "should call our another event" do
52
+ @bla.should_receive(:bl2).with({})
53
+ RqTest.perform('bl2', [{}])
54
+ end
55
+
56
+ it "should call our another event" do
57
+ @bla.should_receive(:bl2).with([1,2,3])
58
+ RqTest.perform('bl2', [[1,2,3]])
59
+ end
60
+
61
+ it "raised when method undefined" do
62
+ lambda do
63
+ RqTest.perform('blasdfds', [1])
64
+ end.should raise_error
65
+ end
66
+ end
67
+
68
+ it "should proxy consumer" do
69
+ RqTest.proxy(:ptest)
70
+ RqTest.ptest(111)
71
+ $a.should == 111
72
+ end
73
+
74
+ 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 'rq_queue'
@@ -0,0 +1,15 @@
1
+ class RqTest < RqQueue
2
+
3
+ self.logger_path = "test.log"
4
+ self.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)
12
+ $a = a
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-kawai
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Makarchev Konstantin
9
+ autorequire: resque-kawai.rb
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &75846280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *75846280
25
+ - !ruby/object:Gem::Dependency
26
+ name: resque
27
+ requirement: &75846060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *75846060
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &75845850 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *75845850
47
+ description: Syntax sugar for Resque consumers. Each consumer is a class, with clean
48
+ interface, and custom logger.
49
+ email: kostya27@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - README.md
58
+ - Rakefile
59
+ - lib/generators/rq/add_generator.rb
60
+ - lib/generators/rq/templates/consumer.rb
61
+ - lib/generators/rq/templates/spec.rb
62
+ - lib/resque-kawai.rb
63
+ - lib/rq_queue.rb
64
+ - lib/version.rb
65
+ - resque-kawai.gemspec
66
+ - spec/rq_queue_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/test_class.rb
69
+ homepage: http://github.com/kostya/resque-kawai
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.16
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Syntax sugar for Resque consumers. Each consumer is a class, with clean interface,
93
+ and custom logger.
94
+ test_files: []