cyclop 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cyclop CHANGED
@@ -61,6 +61,10 @@ else
61
61
  # Limit this worker to job queued by this host
62
62
  # use "localhost" to let Cyclop set it to the host running the worker
63
63
  limit_to_host: "server1.mydomain.tld"
64
+ # Exit worker after having processed x jobs
65
+ # nil : no limit
66
+ # [1-9]+: limit to this number
67
+ die_after: 100
64
68
  # Load actions in this directory (default to ./actions)
65
69
  actions: "/app/actions"
66
70
  sleep_interval: 0.5 # in seconds
@@ -1,3 +1,3 @@
1
1
  module Cyclop
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/cyclop/worker.rb CHANGED
@@ -8,6 +8,10 @@ module Cyclop
8
8
  attr_accessor :sleep_interval
9
9
  # Path to actions directory
10
10
  attr_accessor :actions
11
+ # Number of jobs to process before exiting
12
+ attr_accessor :die_after
13
+ # Number of jobs processed by this worker
14
+ attr_accessor :processed_jobs
11
15
  # Options passed to Cyclop.next to get next job
12
16
  attr_accessor :job_opts
13
17
 
@@ -18,6 +22,8 @@ module Cyclop
18
22
  self.logger = Logger.new(config["log_file"] || $stdout)
19
23
  self.sleep_interval = config["sleep_interval"] || 1
20
24
  self.actions = config["actions"] || "./actions"
25
+ self.processed_jobs = 0
26
+ self.die_after = config["die_after"]
21
27
  @job_opts = {}
22
28
  if config["limit_to_host"]
23
29
  @job_opts[:host] = config["limit_to_host"]
@@ -44,7 +50,7 @@ module Cyclop
44
50
  def run
45
51
  register_signal_handlers
46
52
  loop do
47
- if @stop
53
+ if stop?
48
54
  log "Shutting down..."
49
55
  break
50
56
  end
@@ -56,6 +62,7 @@ module Cyclop
56
62
  procline msg
57
63
  Process.wait
58
64
  log "Child process #{@pid} ended with status: #{$?}"
65
+ self.processed_jobs += 1
59
66
  if $?.exitstatus==0
60
67
  job.complete!
61
68
  else
@@ -131,5 +138,9 @@ module Cyclop
131
138
  def load_actions
132
139
  Dir["#{actions}/*.rb"].each{|action| require action}
133
140
  end
141
+
142
+ def stop?
143
+ @stop || (die_after && processed_jobs >= die_after.to_i)
144
+ end
134
145
  end
135
146
  end
@@ -7,6 +7,7 @@ describe Cyclop::Worker do
7
7
  its(:logger){ should_not be_nil }
8
8
  its(:sleep_interval){ should == 1 }
9
9
  its(:actions){ should == "./actions" }
10
+ its(:processed_jobs){ should == 0 }
10
11
 
11
12
  it "raise ArgumentError without mongo['database']" do
12
13
  lambda {
@@ -43,13 +44,18 @@ describe Cyclop::Worker do
43
44
  })
44
45
  end
45
46
  context "with successful action" do
46
- it "remove the job" do
47
- job = Cyclop.push queue: "slow", job_params: ["tony@starkenterprises.com", :welcome]
47
+ before do
48
+ @job = Cyclop.push queue: "slow", job_params: ["tony@starkenterprises.com", :welcome]
48
49
  t = Thread.new { worker.run }
49
50
  sleep 1
50
51
  worker.stop
51
52
  t.join
52
- Cyclop::Job.find(job._id).should be_nil
53
+ end
54
+ it "remove the job" do
55
+ Cyclop::Job.find(@job._id).should be_nil
56
+ end
57
+ it "increments the number of processed jobs" do
58
+ worker.processed_jobs.should == 1
53
59
  end
54
60
  end
55
61
 
@@ -73,6 +79,15 @@ describe Cyclop::Worker do
73
79
  job.failed.should be_true
74
80
  job.attempts.should == 2
75
81
  end
82
+
83
+ it "increments the number of processed jobs" do
84
+ job = Cyclop.push queue: "slow", job_params: ["tony@starkenterprises.com"]
85
+ t = Thread.new { worker.run }
86
+ sleep 1
87
+ worker.stop
88
+ t.join
89
+ worker.processed_jobs.should == 1
90
+ end
76
91
  end
77
92
 
78
93
  context "limiting to jobs queued by a given host" do
@@ -99,5 +114,28 @@ describe Cyclop::Worker do
99
114
  Cyclop::Job.find(job_local._id).should be_nil
100
115
  end
101
116
  end
117
+
118
+ context "limiting the number of jobs to process" do
119
+ let(:worker) do
120
+ Cyclop::Worker.new({
121
+ "log_file" => File.expand_path("../../../test.log", __FILE__),
122
+ "mongo" => {"database" => "cyclop_test"},
123
+ "actions" => File.expand_path("../../fixtures/actions", __FILE__),
124
+ "die_after" => 1,
125
+ })
126
+ end
127
+ it "only processes specified number jobs" do
128
+ job1 = Cyclop.push queue: "slow", job_params: ["tony@starkenterprises.com", :welcome]
129
+ job2 = Cyclop.push queue: "slow", job_params: ["tony@starkinternationals.com", :welcome]
130
+ 2.times do
131
+ t = Thread.new { worker.run }
132
+ sleep 1
133
+ worker.stop
134
+ t.join
135
+ end
136
+ Cyclop::Job.find(job1._id).should be_nil
137
+ Cyclop::Job.find(job2._id).should == job2
138
+ end
139
+ end
102
140
  end
103
141
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyclop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,12 +10,12 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-06-22 00:00:00.000000000 +02:00
13
+ date: 2011-06-28 00:00:00.000000000 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bson_ext
18
- requirement: &2165763360 !ruby/object:Gem::Requirement
18
+ requirement: &2153128540 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: 1.3.1
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2165763360
26
+ version_requirements: *2153128540
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mongo
29
- requirement: &2165762840 !ruby/object:Gem::Requirement
29
+ requirement: &2153125580 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: 1.3.1
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *2165762840
37
+ version_requirements: *2153125580
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: posix-spawn
40
- requirement: &2165762360 !ruby/object:Gem::Requirement
40
+ requirement: &2153125100 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: 0.3.6
46
46
  type: :runtime
47
47
  prerelease: false
48
- version_requirements: *2165762360
48
+ version_requirements: *2153125100
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: rake
51
- requirement: &2165761880 !ruby/object:Gem::Requirement
51
+ requirement: &2153124220 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: 0.8.7
57
57
  type: :development
58
58
  prerelease: false
59
- version_requirements: *2165761880
59
+ version_requirements: *2153124220
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rspec
62
- requirement: &2165761380 !ruby/object:Gem::Requirement
62
+ requirement: &2153121880 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ~>
@@ -67,10 +67,10 @@ dependencies:
67
67
  version: 2.6.0
68
68
  type: :development
69
69
  prerelease: false
70
- version_requirements: *2165761380
70
+ version_requirements: *2153121880
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rocco
73
- requirement: &2165760900 !ruby/object:Gem::Requirement
73
+ requirement: &2153118180 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
76
  - - ~>
@@ -78,7 +78,7 @@ dependencies:
78
78
  version: '0.7'
79
79
  type: :development
80
80
  prerelease: false
81
- version_requirements: *2165760900
81
+ version_requirements: *2153118180
82
82
  description: Job queue with MongoDB with emphasis on never losing any task even if
83
83
  worker fails hard (segfault).
84
84
  email: