Module: Squeese
Defined Under Namespace
Classes: NoJobsDefined, NoSuchJob
Instance Method Summary
collapse
Instance Method Details
#all_jobs ⇒ Object
102
103
104
|
# File 'lib/squeese.rb', line 102
def all_jobs
@@handlers.keys
end
|
#enqueue(job, args = {}) ⇒ Object
13
14
15
|
# File 'lib/squeese.rb', line 13
def enqueue(job, args={})
queue.send_message [ job, args ].to_json
end
|
#exception_message(e) ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/squeese.rb', line 91
def exception_message(e)
msg = [ "Exception #{e.class} -> #{e.message}" ]
base = File.expand_path(Dir.pwd) + '/'
e.backtrace.each do |t|
msg << " #{File.expand_path(t).gsub(/#{base}/, '')}"
end
msg.join("\n")
end
|
#job(j, &block) ⇒ Object
17
18
19
20
|
# File 'lib/squeese.rb', line 17
def job(j, &block)
@@handlers ||= {}
@@handlers[j] = block
end
|
#log(msg) ⇒ Object
71
72
73
|
# File 'lib/squeese.rb', line 71
def log(msg)
puts "[Squeese][#{Time.now}] #{msg}"
end
|
#log_job(name, args) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/squeese.rb', line 63
def log_job(name, args)
args_flat = args.inject("") do |accum, (key,value)|
accum += "#{key}=#{value} "
end
log sprintf("%-15s :: #{args_flat}", name)
end
|
#purge ⇒ Object
9
10
11
|
# File 'lib/squeese.rb', line 9
def purge
queue.delete
end
|
#queue ⇒ Object
87
88
89
|
# File 'lib/squeese.rb', line 87
def queue
sqs.queue(queue_name, true)
end
|
#queue_name ⇒ Object
83
84
85
|
# File 'lib/squeese.rb', line 83
def queue_name
@@queue_name ||= "squeese"
end
|
#queue_name=(val) ⇒ Object
79
80
81
|
# File 'lib/squeese.rb', line 79
def queue_name=(val)
@@queue_name = val
end
|
#sqs ⇒ Object
75
76
77
|
# File 'lib/squeese.rb', line 75
def sqs
@sqs ||= Aws::Sqs.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'], :logger => Logger.new(nil))
end
|
#work ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/squeese.rb', line 25
def work
raise NoJobsDefined unless defined?(@@handlers)
jobs = all_jobs
jobs.each do |job|
raise(NoSuchJob, job) unless @@handlers[job]
end
log "Working #{jobs.size} jobs :: [ #{jobs.join(' ')} ]"
loop do
work_one_job
end
end
|
#work_one_job ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/squeese.rb', line 43
def work_one_job
msg = queue.pop
unless msg
sleep 2
return
end
name, args = JSON.parse msg.body
args = Hash.new {|h,k| h[k.to_s] if h.keys.include? k.to_s}.merge(args)
log_job(name, args)
handler = @@handlers[name]
raise(NoSuchJob, name) unless handler
handler.call(args)
rescue => e
log exception_message(e)
log "Deleted failed job." if msg
end
|