Skip to content

Commit 4594b7b

Browse files
author
Adam Wiggins
committed
one tube per job instead of priority tubes
1 parent 2d41c82 commit 4594b7b

File tree

4 files changed

+28
-53
lines changed

4 files changed

+28
-53
lines changed

bin/stalk

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
require File.dirname(__FILE__) + '/../lib/stalker'
44

5-
usage = "stalk <jobs.rb> [<priority>[,<priority>,..]]"
5+
usage = "stalk <jobs.rb> [<job>[,<job>,..]]"
66
file = ARGV.shift or abort usage
7-
priorities = (ARGV.shift || 'all').split(',')
7+
jobs = ARGV.shift.split(',') rescue nil
88

99
require file
1010

11-
jobs = Stalker.jobs(priorities)
12-
puts "Working #{jobs.size} #{priorities.join(', ')} priority jobs: [ #{jobs.join(' ')} ]"
13-
1411
trap('INT') do
1512
puts "\rExiting"
1613
exit
1714
end
1815

19-
Stalker.work priorities
20-
16+
Stalker.work jobs

examples/enqueue.rb

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
$LOAD_PATH.unshift '../lib'
22
require 'stalker'
33

4-
require 'jobs'
5-
64
Stalker.enqueue('send.email', :email => '[email protected]')
75
Stalker.enqueue('cleanup.strays')
8-

examples/jobs.rb

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33

44
include Stalker
55

6-
priority :high do
7-
job 'send.email' do |args|
8-
puts "Sending email to args['email']"
9-
end
6+
job 'send.email' do |args|
7+
log "Sending email to #{args['email']}"
8+
end
109

11-
job 'transform.image' do |args|
12-
puts "Image transform"
13-
end
10+
job 'transform.image' do |args|
11+
log "Image transform"
1412
end
1513

16-
priority :low do
17-
job 'cleanup.strays' do |args|
18-
puts "Cleaning up"
19-
end
14+
job 'cleanup.strays' do |args|
15+
log "Cleaning up"
2016
end

lib/stalker.rb

+18-32
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,37 @@ module Stalker
66
extend self
77

88
def enqueue(job, args={})
9-
beanstalk.use find_priority(job)
9+
beanstalk.use job
1010
beanstalk.put [ job, args ].to_json
1111
end
1212

13-
def priority(p, &block)
14-
@@priority = p.to_s
15-
block.call
16-
@@priority = nil
17-
end
18-
1913
def job(j, &block)
20-
@@priority ||= 'default'
21-
@@priorities ||= {}
22-
@@priorities[j] = @@priority
23-
2414
@@handlers ||= {}
2515
@@handlers[j] = block
2616
end
2717

28-
def work(priorities=['all'])
29-
if Array(priorities) == [ 'all' ]
30-
priorities = @@priorities.values.uniq
18+
class NoJobsDefined < RuntimeError; end
19+
class NoSuchJob < RuntimeError; end
20+
21+
def work(jobs=nil)
22+
raise NoJobsDefined unless defined?(@@handlers)
23+
24+
jobs ||= all_jobs
25+
26+
jobs.each do |job|
27+
raise(NoSuchJob, job) unless @@handlers[job]
3128
end
3229

30+
log "Working #{jobs.size} jobs :: [ #{jobs.join(' ')} ]"
31+
3332
beanstalk.list_tubes_watched.each { |tube| beanstalk.ignore(tube) }
34-
priorities.each { |priority| beanstalk.watch(priority) }
33+
jobs.each { |job| beanstalk.watch(job) }
3534

3635
loop do
3736
work_one_job
3837
end
3938
end
4039

41-
class NoSuchJob < RuntimeError; end
42-
4340
def work_one_job
4441
job = beanstalk.reserve
4542
name, args = JSON.parse job.body
@@ -65,21 +62,6 @@ def log(msg)
6562
puts "[#{Time.now}] #{msg}"
6663
end
6764

68-
def jobs(priorities=['all'])
69-
jobs = []
70-
@@priorities.each do |job, priority|
71-
jobs << job if priorities == %w(all) or priorities.include? priority
72-
end
73-
jobs
74-
end
75-
76-
class NoJobsDefined < RuntimeError; end
77-
78-
def find_priority(job)
79-
raise NoJobsDefined unless defined?(@@priorities)
80-
@@priorities[job] or raise(NoSuchJob, job)
81-
end
82-
8365
def beanstalk
8466
@@beanstalk ||= Beanstalk::Pool.new([ beanstalk_host_and_port ])
8567
end
@@ -106,4 +88,8 @@ def exception_message(e)
10688

10789
msg.join("\n")
10890
end
91+
92+
def all_jobs
93+
@@handlers.keys
94+
end
10995
end

0 commit comments

Comments
 (0)