Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Created June 11, 2011 21:05
Show Gist options
  • Save resistorsoftware/1020963 to your computer and use it in GitHub Desktop.
Save resistorsoftware/1020963 to your computer and use it in GitHub Desktop.
Elegant Delayed Job for Shopify
# here comes 1, 2, 200, or even 603 id's
get '/orders/do_it_man/?' do
if ShopifyAPI.credit_left > 0
Delayed::Job.enqueue(Hookah::Pipe.new(current_shop.site, params[:ids])) # <-- regular run, we have some credit...
else
Delayed::Job.enqueue(Hookah::Pipe.new(current_shop.site, params[:ids]), 0, 601.seconds.from_now) # <--- no credit, run later
end
end
module Hookah
class Pipe < DynamicJob
def initialize(url, list)
@url = url
@list = list
super()
end
def perform
ActiveResource::Base.site = @url
index = @list.length
@list.each_index do |i|
id = @list[i]
if id.to_i > 0
credit = ShopifyAPI.credit_left
if credit > 2
order = ShopifyAPI::Order.find(id)
order.note = "Thanks for helping out with this Chris"
order.save
else
index = i # <-- let us know where to continue
break
end
end
end
if index < @list.length # <-- only run if we need to
# not enough API calls
Delayed::Job.enqueue(
Hookah::Pipe.new(@url, @list[index,@list.length]), # the job
0, # a priority of 0 is nice
601.seconds.from_now # wait 10 minutes to run
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment