stalker 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/bin/stalk CHANGED
@@ -2,19 +2,15 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/../lib/stalker'
4
4
 
5
- usage = "stalk <jobs.rb> [<priority>[,<priority>,..]]"
5
+ usage = "stalk <jobs.rb> [<job>[,<job>,..]]"
6
6
  file = ARGV.shift or abort usage
7
- priorities = (ARGV.shift || 'all').split(',')
7
+ jobs = ARGV.shift.split(',') rescue nil
8
8
 
9
9
  require file
10
10
 
11
- jobs = Stalker.jobs(priorities)
12
- puts "Working #{jobs.size} #{priorities.join(', ')} priority jobs: [ #{jobs.join(' ')} ]"
13
-
14
11
  trap('INT') do
15
12
  puts "\rExiting"
16
13
  exit
17
14
  end
18
15
 
19
- Stalker.work priorities
20
-
16
+ Stalker.work jobs
data/examples/enqueue.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  $LOAD_PATH.unshift '../lib'
2
2
  require 'stalker'
3
3
 
4
- require 'jobs'
5
-
6
4
  Stalker.enqueue('send.email', :email => 'hello@example.com')
7
5
  Stalker.enqueue('cleanup.strays')
8
-
data/examples/jobs.rb CHANGED
@@ -3,18 +3,14 @@ require 'stalker'
3
3
 
4
4
  include Stalker
5
5
 
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
10
9
 
11
- job 'transform.image' do |args|
12
- puts "Image transform"
13
- end
10
+ job 'transform.image' do |args|
11
+ log "Image transform"
14
12
  end
15
13
 
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"
20
16
  end
data/lib/stalker.rb CHANGED
@@ -6,40 +6,37 @@ module Stalker
6
6
  extend self
7
7
 
8
8
  def enqueue(job, args={})
9
- beanstalk.use find_priority(job)
9
+ beanstalk.use job
10
10
  beanstalk.put [ job, args ].to_json
11
11
  end
12
12
 
13
- def priority(p, &block)
14
- @@priority = p.to_s
15
- block.call
16
- @@priority = nil
17
- end
18
-
19
13
  def job(j, &block)
20
- @@priority ||= 'default'
21
- @@priorities ||= {}
22
- @@priorities[j] = @@priority
23
-
24
14
  @@handlers ||= {}
25
15
  @@handlers[j] = block
26
16
  end
27
17
 
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]
31
28
  end
32
29
 
30
+ log "Working #{jobs.size} jobs :: [ #{jobs.join(' ')} ]"
31
+
33
32
  beanstalk.list_tubes_watched.each { |tube| beanstalk.ignore(tube) }
34
- priorities.each { |priority| beanstalk.watch(priority) }
33
+ jobs.each { |job| beanstalk.watch(job) }
35
34
 
36
35
  loop do
37
36
  work_one_job
38
37
  end
39
38
  end
40
39
 
41
- class NoSuchJob < RuntimeError; end
42
-
43
40
  def work_one_job
44
41
  job = beanstalk.reserve
45
42
  name, args = JSON.parse job.body
@@ -65,21 +62,6 @@ module Stalker
65
62
  puts "[#{Time.now}] #{msg}"
66
63
  end
67
64
 
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
-
83
65
  def beanstalk
84
66
  @@beanstalk ||= Beanstalk::Pool.new([ beanstalk_host_and_port ])
85
67
  end
@@ -106,4 +88,8 @@ module Stalker
106
88
 
107
89
  msg.join("\n")
108
90
  end
91
+
92
+ def all_jobs
93
+ @@handlers.keys
94
+ end
109
95
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Adam Wiggins
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-26 00:00:00 -07:00
17
+ date: 2010-04-10 00:00:00 -07:00
18
18
  default_executable: stalk
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency