Skip to content

Instantly share code, notes, and snippets.

@bleonard
Created May 2, 2017 05:05
Show Gist options
  • Save bleonard/7afc041e4310e9285b1a75000e2312a8 to your computer and use it in GitHub Desktop.
Save bleonard/7afc041e4310e9285b1a75000e2312a8 to your computer and use it in GitHub Desktop.
class InvoiceChargeWorker
include TResque::Worker
inputs :invoice_id
worker_lock :invoice_id
def self.process_all!
Invoice.where(stat: 'pending').find_each do |invoice|
self.enqueue(invoice_id: invoice.id)
end
end
def work
return unless needed?
invoice.charge!
end
def needed?
invoice.pending?
end
def invoice
@invoice ||= Invoice.find(invoice_id)
end
end
# process all pending invoices
InvoiceChargeWorker.process_all!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment