creeper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -1,29 +1,79 @@
1
1
  # Creeper
2
2
 
3
- TODO: Write a gem description
3
+ Can be used as an in place drop in for stalker but it is multi threaded so you can easily do more without using more memory
4
4
 
5
- ## Installation
5
+ Creeper - an improvement on Stalker
6
+ ==========================================
6
7
 
7
- Add this line to your application's Gemfile:
8
+ The big difference is how you "work" jobs
8
9
 
9
- gem 'creeper'
10
+ all you need is a thread count arguement :)
10
11
 
11
- And then execute:
12
+ Creeper.work(<jobs>, <thread_count>)
12
13
 
13
- $ bundle
14
+ [Beanstalkd](http://kr.github.com/beanstalkd/) is a fast, lightweight queueing backend inspired by mmemcached.
14
15
 
15
- Or install it yourself as:
16
+ Queueing jobs
17
+ -------------
16
18
 
17
- $ gem install creeper
19
+ From anywhere in your app:
18
20
 
19
- ## Usage
21
+ require 'creeper'
20
22
 
21
- TODO: Write usage instructions here
23
+ Creeper.enqueue('email.send', :to => 'joe@example.com')
24
+ Creeper.enqueue('post.cleanup.all')
25
+ Creeper.enqueue('post.cleanup', :id => post.id)
22
26
 
23
- ## Contributing
27
+ Working jobs
28
+ ------------
24
29
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Added some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
30
+ In a standalone file, typically jobs.rb or worker.rb:
31
+
32
+ require 'creeper'
33
+ include Creeper
34
+
35
+ job 'email.send' do |args|
36
+ Pony.send(:to => args['to'], :subject => "Hello there")
37
+ end
38
+
39
+ job 'post.cleanup.all' do |args|
40
+ Post.all.each do |post|
41
+ enqueue('post.cleanup', :id => post.id)
42
+ end
43
+ end
44
+
45
+ job 'post.cleanup' do |args|
46
+ Post.find(args['id']).cleanup
47
+ end
48
+
49
+ Creeper.work(<jobs>, <thread_count>)
50
+
51
+ Running
52
+ -------
53
+
54
+ First, make sure you have Beanstalkd installed and running:
55
+
56
+ $ sudo brew install beanstalkd
57
+ $ beanstalkd
58
+
59
+ Creeper:
60
+
61
+ $ sudo gem install creeper
62
+
63
+ Error Handling
64
+ -------------
65
+
66
+ If you include an `error` block in your jobs definition, that block will be invoked when a worker encounters an error. You might use this to report errors to an external monitoring service:
67
+
68
+ error do |e, job, args|
69
+ Exceptional.handle(e)
70
+ end
71
+
72
+ Before filter
73
+ -------------
74
+
75
+ If you wish to run a block of code prior to any job:
76
+
77
+ before do |job|
78
+ puts "About to work #{job}"
79
+ end
@@ -1,3 +1,3 @@
1
1
  module Creeper
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ describe Creeper do
4
+
5
+ after :each do
6
+ Creeper.clear!
7
+ end
8
+
9
+ it "work a job and do it up" do
10
+ val = rand(999999)
11
+ Creeper.job('my.job') { |args| $result = args['val'] }
12
+ Creeper.enqueue('my.job', :val => val)
13
+ w = Creeper::Worker.new
14
+ w.stub(:exception_message)
15
+ w.stub(:log)
16
+ w.prep
17
+ w.work_one_job
18
+ val.should == $result
19
+ end
20
+
21
+ it "invoke error handler when defined" do
22
+ with_an_error_handler
23
+ Creeper.job('my.job') { |args| fail }
24
+ Creeper.enqueue('my.job', :foo => 123)
25
+ w = Creeper::Worker.new
26
+ w.stub(:exception_message)
27
+ w.stub(:log)
28
+ w.prep
29
+ w.work_one_job
30
+ $handled.should_not == nil
31
+ 'my.job'.should == $job_name
32
+ {'foo' => 123}.should == $job_args
33
+ end
34
+
35
+ it "should be compatible with legacy error handlers" do
36
+ exception = StandardError.new("Oh my, the job has failed!")
37
+ Creeper.error { |e| $handled = e }
38
+ Creeper.job('my.job') { |args| raise exception }
39
+ Creeper.enqueue('my.job')
40
+ w = Creeper::Worker.new
41
+ w.stub(:exception_message)
42
+ w.stub(:log)
43
+ w.prep
44
+ w.work_one_job
45
+ exception.should == $handled
46
+ end
47
+
48
+ it "continue working when error handler not defined" do
49
+ Creeper.error { |e| $handled = false }
50
+ Creeper.job('my.job') { fail }
51
+ Creeper.enqueue('my.job')
52
+ w = Creeper::Worker.new
53
+ w.stub(:exception_message)
54
+ w.stub(:log)
55
+ w.prep
56
+ w.work_one_job
57
+ false.should == $handled
58
+ end
59
+
60
+ it "exception raised one second before beanstalk ttr reached" do
61
+ with_an_error_handler
62
+ Creeper.job('my.job') { sleep(3); $handled = "didn't time out" }
63
+ Creeper.enqueue('my.job', {}, :ttr => 2)
64
+ w = Creeper::Worker.new
65
+ w.stub(:exception_message)
66
+ w.stub(:log)
67
+ w.prep
68
+ w.work_one_job
69
+ $handled.should == "didn't time out"
70
+ end
71
+
72
+ it "before filter gets run first" do
73
+ Creeper.before { |name| $flag = "i_was_here" }
74
+ Creeper.job('my.job') { |args| $handled = ($flag == 'i_was_here') }
75
+ Creeper.enqueue('my.job')
76
+ w = Creeper::Worker.new
77
+ w.stub(:exception_message)
78
+ w.stub(:log)
79
+ w.prep
80
+ w.work_one_job
81
+ true.should == $handled
82
+ end
83
+
84
+ it "before filter passes the name of the job" do
85
+ Creeper.before { |name| $jobname = name }
86
+ Creeper.job('my.job') { true }
87
+ Creeper.enqueue('my.job')
88
+ w = Creeper::Worker.new
89
+ w.stub(:exception_message)
90
+ w.stub(:log)
91
+ w.prep
92
+ w.work_one_job
93
+ 'my.job'.should == $jobname
94
+ end
95
+
96
+ it "before filter can pass an instance var" do
97
+ Creeper.before { |name| @foo = "hello" }
98
+ Creeper.job('my.job') { |args| $handled = (@foo == "hello") }
99
+ Creeper.enqueue('my.job')
100
+ w = Creeper::Worker.new
101
+ w.stub(:exception_message)
102
+ w.stub(:log)
103
+ w.prep
104
+ w.work_one_job
105
+ true.should == $handled
106
+ end
107
+
108
+ it "before filter invokes error handler when defined" do
109
+ with_an_error_handler
110
+ Creeper.before { |name| fail }
111
+ Creeper.job('my.job') { }
112
+ Creeper.enqueue('my.job', :foo => 123)
113
+ w = Creeper::Worker.new
114
+ w.stub(:exception_message)
115
+ w.stub(:log)
116
+ w.prep
117
+ w.work_one_job
118
+ $handled.should_not == nil
119
+ 'my.job'.should == $job_name
120
+ {'foo' => 123}.should == $job_args
121
+ end
122
+
123
+ it "parse BEANSTALK_URL" do
124
+ ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300"
125
+ Creeper.beanstalk_addresses.should == ["localhost:12300"]
126
+ ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300/, beanstalk://localhost:12301/"
127
+ Creeper.beanstalk_addresses.should == ["localhost:12300","localhost:12301"]
128
+ ENV['BEANSTALK_URL'] = "beanstalk://localhost:12300 beanstalk://localhost:12301"
129
+ Creeper.beanstalk_addresses.should == ["localhost:12300","localhost:12301"]
130
+ end
131
+
132
+ def with_an_error_handler
133
+ Creeper.error do |e, job_name, args|
134
+ $handled = e.class
135
+ $job_name = job_name
136
+ $job_args = args
137
+ end
138
+ end
139
+
140
+
141
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require 'creeper'
5
+ require 'pry'
6
+
7
+
8
+ RSpec.configure do |config|
9
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Creeper::Worker do
4
+ # before :each do
5
+ # @base = Monitor::Base.new
6
+ # end
7
+
8
+ # after :each do
9
+ # @base = nil
10
+ # end
11
+
12
+ it "shoud not create a vz object" do
13
+ 2.should == 2
14
+ end
15
+
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -67,6 +67,7 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
+ - .rspec
70
71
  - Gemfile
71
72
  - LICENSE
72
73
  - README.md
@@ -75,6 +76,9 @@ files:
75
76
  - lib/creeper.rb
76
77
  - lib/creeper/version.rb
77
78
  - lib/creeper/worker.rb
79
+ - spec/creeper_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/worker_spec.rb
78
82
  homepage: https://github.com/lyondhill/creeper
79
83
  licenses: []
80
84
  post_install_message:
@@ -100,5 +104,8 @@ signing_key:
100
104
  specification_version: 3
101
105
  summary: A better solution for io bound jobs, same as stalker in functionality but
102
106
  more threadie.
103
- test_files: []
107
+ test_files:
108
+ - spec/creeper_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/worker_spec.rb
104
111
  has_rdoc: