Module: Bushpig::Job
- Defined in:
- lib/bushpig/job.rb
Class Method Summary collapse
- .hydrate(job_payload) ⇒ Object
- .job(handler, *args) ⇒ Object
- .keyed(handler, unique_key, *args) ⇒ Object
Class Method Details
.hydrate(job_payload) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/bushpig/job.rb', line 76 def self.hydrate(job_payload) h = JSON.parse(job_payload, symbolize_names: true) klass = const_get(h[:class]) job = klass.new(*h[:args]) job.job_id = h[:id] job end |
.job(handler, *args) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bushpig/job.rb', line 9 def self.job(handler, *args) Struct.new(*args) do @job_handler = handler def self.job_handler @job_handler end def job_id @job_id ||= SecureRandom.hex(32) end def job_id=(job_id) @job_id = job_id end def job_key job_id end def job_payload JSON.generate({ class: self.class.name, id: job_id, args: each.to_a }) end def handle self.class.job_handler.call(self) end end end |
.keyed(handler, unique_key, *args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bushpig/job.rb', line 39 def self.keyed(handler, unique_key, *args) Struct.new(*args) do @job_handler = handler @unique_key = unique_key def self.job_handler @job_handler end def self.job_unique_key @unique_key end def job_id @job_id ||= SecureRandom.hex(32) end def job_id=(job_id) @job_id = job_id end def job_key self.class.job_unique_key.inject(Digest::SHA256.new.update(self.class.name)) do |digest, key| digest.update(self[key].to_s) end.hexdigest end def job_payload JSON.generate({ class: self.class.name, id: job_id, args: each.to_a }) end def handle self.class.job_handler.call(self) end end end |