Hi 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
take a look at backgrounDRb. maybe this could be an option for you. backgroundrb.rubyforge.org 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I thought that may be the case Thanks for your help - will read up on background DRb On 12 Feb, 12:30, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> take a look at backgrounDRb. maybe this could be an option for you.http://backgroundrb.rubyforge.org > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12 Feb 2009, at 12:32, Gavin wrote:> > 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. Fred> On 12 Feb, 12:30, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> take a look at backgrounDRb. maybe this could be an option for you.http://backgroundrb.rubyforge.org >> >> 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. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gavin wrote:> 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 > endSorry 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 savedI 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." en.wikipedia.org/wiki/Finite_state_machine There is a Ruby gem implementation of this: github.com/rubyist/aasm/tree/master 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. -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So, would it be more appropriate to write a method like def purchased? if self.status > 3 return true else return false end end and then call the purchased? method to perform the check? On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Gavin wrote: > > 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." > > en.wikipedia.org/wiki/Finite_state_machine > > There is a Ruby gem implementation of this:github.com/rubyist/aasm/tree/master > > 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. > -- > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gavin wrote:> So, would it be more appropriate to write a method like > > def purchased? > if self.status > 3 > return true > else > return false > end > end > > and then call the purchased? method to perform the check? > > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Actually it could be as simple as adding some constants to represent your states. Example: STARTED = 1 PROCESSING = 2 COMPLETED = 3 BILLED = 4 PAID = 5 if self.state > COMPLETED You just want to avoid the use of a "magic number" by giving it clear meaning. -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Writing more unit tests for this as we speak Thanks for the tip Robert PS - I presume, if I were to use constants that I would define the constants in the class itself? On 12 Feb, 15:17, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Gavin wrote: > > So, would it be more appropriate to write a method like > > > def purchased? > > if self.status > 3 > > return true > > else > > return false > > end > > end > > > and then call the purchased? method to perform the check? > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > Actually it could be as simple as adding some constants to represent > your states. > > Example: > STARTED = 1 > PROCESSING = 2 > COMPLETED = 3 > BILLED = 4 > PAID = 5 > > if self.state > COMPLETED > > You just want to avoid the use of a "magic number" by giving it clear > meaning. > > -- > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
+1 for implementing this as a cron job. Sometimes the cleanest solution is outside the framework. On Thu, Feb 12, 2009 at 4:20 PM, Gavin <gavin-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote:> > Writing more unit tests for this as we speak > > Thanks for the tip Robert > > PS - I presume, if I were to use constants that I would define the > constants in the class itself? > > On 12 Feb, 15:17, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > wrote: > > Gavin wrote: > > > So, would it be more appropriate to write a method like > > > > > def purchased? > > > if self.status > 3 > > > return true > > > else > > > return false > > > end > > > end > > > > > and then call the purchased? method to perform the check? > > > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > > > > Actually it could be as simple as adding some constants to represent > > your states. > > > > Example: > > STARTED = 1 > > PROCESSING = 2 > > COMPLETED = 3 > > BILLED = 4 > > PAID = 5 > > > > if self.state > COMPLETED > > > > You just want to avoid the use of a "magic number" by giving it clear > > meaning. > > > > -- > > Posted viahttp://www.ruby-forum.com. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? On 12 Feb, 18:27, Chris Kottom <chris.kot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> +1 for implementing this as a cron job. Sometimes the cleanest solution is > outside the framework. > > On Thu, Feb 12, 2009 at 4:20 PM, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote: > > > Writing more unit tests for this as we speak > > > Thanks for the tip Robert > > > PS - I presume, if I were to use constants that I would define the > > constants in the class itself? > > > On 12 Feb, 15:17, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > wrote: > > > Gavin wrote: > > > > So, would it be more appropriate to write a method like > > > > > def purchased? > > > > if self.status > 3 > > > > return true > > > > else > > > > return false > > > > end > > > > end > > > > > and then call the purchased? method to perform the check? > > > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > > Actually it could be as simple as adding some constants to represent > > > your states. > > > > Example: > > > STARTED = 1 > > > PROCESSING = 2 > > > COMPLETED = 3 > > > BILLED = 4 > > > PAID = 5 > > > > if self.state > COMPLETED > > > > You just want to avoid the use of a "magic number" by giving it clear > > > meaning. > > > > -- > > > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, Check out this excelent railscast: railscasts.com/episodes/129-custom-daemon It is really easy & effective. Also, Thorny Gorms has some built-in checks for form expiration: groups.google.com/group/rubyonrails-talk/browse_thread/thread/9ec5a5112548c95e Cheers, Sazima On Feb 12, 4:35 pm, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote:> 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? > > On 12 Feb, 18:27, Chris Kottom <chris.kot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > +1 for implementing this as a cron job. Sometimes the cleanest solution is > > outside the framework. > > > On Thu, Feb 12, 2009 at 4:20 PM, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote: > > > > Writing more unit tests for this as we speak > > > > Thanks for the tip Robert > > > > PS - I presume, if I were to use constants that I would define the > > > constants in the class itself? > > > > On 12 Feb, 15:17, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > > wrote: > > > > Gavin wrote: > > > > > So, would it be more appropriate to write a method like > > > > > > def purchased? > > > > > if self.status > 3 > > > > > return true > > > > > else > > > > > return false > > > > > end > > > > > end > > > > > > and then call the purchased? method to perform the check? > > > > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > > > Actually it could be as simple as adding some constants to represent > > > > your states. > > > > > Example: > > > > STARTED = 1 > > > > PROCESSING = 2 > > > > COMPLETED = 3 > > > > BILLED = 4 > > > > PAID = 5 > > > > > if self.state > COMPLETED > > > > > You just want to avoid the use of a "magic number" by giving it clear > > > > meaning. > > > > > -- > > > > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 12 Feb, 19:33, Sazima <rsaz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Check out this excelent railscast: > > railscasts.com/episodes/129-custom-daemon > > It is really easy & effective. > > Also, Thorny Gorms has some built-in checks for form expiration: > > groups.google.com/group/rubyonrails-talk/browse_thread/thread... > > Cheers, Sazima > > On Feb 12, 4:35 pm, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote: > > > 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? > > > On 12 Feb, 18:27, Chris Kottom <chris.kot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > +1 for implementing this as a cron job. Sometimes the cleanest solution is > > > outside the framework. > > > > On Thu, Feb 12, 2009 at 4:20 PM, Gavin <ga...-YMj/zd8x6QpKMzDMP321V2ksYUyLi9NM@public.gmane.org> wrote: > > > > > Writing more unit tests for this as we speak > > > > > Thanks for the tip Robert > > > > > PS - I presume, if I were to use constants that I would define the > > > > constants in the class itself? > > > > > On 12 Feb, 15:17, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > > > wrote: > > > > > Gavin wrote: > > > > > > So, would it be more appropriate to write a method like > > > > > > > def purchased? > > > > > > if self.status > 3 > > > > > > return true > > > > > > else > > > > > > return false > > > > > > end > > > > > > end > > > > > > > and then call the purchased? method to perform the check? > > > > > > > On 12 Feb, 14:39, Robert Walker <rails-mailing-l...@andreas-s.net<mail.google.com/mail?view=cm&tf=0&to=rails-mailing-l...@andreas-s.net> > > > > > > Actually it could be as simple as adding some constants to represent > > > > > your states. > > > > > > Example: > > > > > STARTED = 1 > > > > > PROCESSING = 2 > > > > > COMPLETED = 3 > > > > > BILLED = 4 > > > > > PAID = 5 > > > > > > if self.state > COMPLETED > > > > > > You just want to avoid the use of a "magic number" by giving it clear > > > > > meaning. > > > > > > -- > > > > > Posted viahttp://www.ruby-forum.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---