I’m building an app at the moment where specific items can be added to
an order and then purchased through paypal.
Becuase the items are limited, it’s important to relist them quickly
if the order is not completed within 5mins.
Anybody have any suggestions as to how I could go about this?
I was thinking of creating an observer for orders and including an
after_create like so:
Class OrderObserver < ActiveRecord::Observer
def after_create
sleep 300 # sleep for 5 mins
if self.status > 3 # anything above 3 should be saved
return
else
self.items.each do |item|
item.update_attribute :order_id, nil
end
self.destroy
end
end
Is there a better or a standard way of doing this though?
in a background process you could sleep for as long as you want to,
but if you try that in your main process your application might freeze
for that amount of time.
I thought that may be the case
Thanks for your help - will read up on background DRb
another possibility is to have a cron job or daemon that checks for
orders that have been waiting for too long (stick a expires_at column
or something on the table). Just calling sleep is a bad idea.
Anybody have any suggestions as to how I could go about this?
I was thinking of creating an observer for orders and including an
after_create like so:
Class OrderObserver < ActiveRecord::Observer
def after_create
sleep 300 # sleep for 5 mins
if self.status > 3 # anything above 3 should be saved
return
else
self.items.each do |item|
item.update_attribute :order_id, nil
end
self.destroy
end
end
Sorry if this response is a bit off the question’s topic, but I do have
an unrelated suggestion.
You have the following line in your sample code:
if self.status > 3 # anything above 3 should be saved
I would recommend against this use of “magic numbers,” such as “3” in
this case. The number 3 has no meaning here. I would recommend that you
use something like a “finite state machine.”
There is a Ruby gem implementation of this:
Now the same line of code can be written as:
if self.completed?
Now there is no ambiguity on the meaning of some “magic number.” The
code clearly states its intent.
Thanks Sazima - I set up a daemon like this earlier today and it’s
working a treat, just wasn’t sure if this was the most economic way to
get the job done!
But if it’s good enough for Ryan, it’s good enough for me.
Am I right in saying Daemons for frequent tasks (performing a specific
check every minute or so) and cron jobs for less regular tasks
(cleaning up sessions every week or so)?
Are Daemons resource-intensive?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.