Created
May 2, 2017 05:05
-
-
Save bleonard/7afc041e4310e9285b1a75000e2312a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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