Drew, Since you can't control the user closing their browser and thus killing your Ajax implementation for long-running timers the server-side solution is the only way to go.
A cron job is a reasonable solution. As you pointed out, if you want no more than 5 minutes of latency between when a timer goes over the limit and an email is sent then you have to run the cron job every 5 minutes. Depending on how resource-intensive the search for overdue timers is you could probably get away with executing the cron job every minute instead. This will depend on how you've got your database or other persistence setup. You clearly don't want to run a job every minute that takes 2 minutes to run! Another approach would be to fire off a background job like DelayedJob to monitor a particular timer. You could tell it to run at some time in the future, say, the timer expiration time. The DJ job would just check to see if it's particular timer had been stopped, and if not, send the email. This keeps all the logic in your app. If you have potentially thousands of timers running concurrently having all those DJ threads running might get expensive but it's probably worth considering. Anyone with more experience in background jobs should pipe in here! For the weekly report, yes, a cron job absolutely makes the most sense. FYI, there's a cool little gem called "whenever" that I use to manage cron jobs from within a Rails app: https://github.com/javan/whenever Cheers, and good luck! Chris On Sunday, October 12, 2014 10:05:14 AM UTC-7, Andrew Haines wrote: > > Hi everyone, > > This is Drew Haines from DevCo <http://dmdevco.com/>. I am working on a > web app called HoursLogger.com and want a second opinion on how to > automate the emails for my Rails app. > > > Hours Logger is a time tracking applications and currently sends 2 > automated emails: > 1. When a user's timer runs too long, it sends an email fix their timer > 2. At the end of the week, it emails the user their weekly report > > > *1. Timer Runs Too Long* > Currently, the timer application runs on the client side. When the timer > runs over the set time limit, an AJAX request is sent to the server to send > the email. This is a problem because the app has to be open in the user's > browser to fire the email, which isn't that helpful. So ... I need a way > to track when a user's timer goes over their limit, *on the server*. > > I was thinking implement with cron job. > - When a user starts the timer, use AJAX to save the start time in the > database > - Run a cron job that checks if any users have a start time, but no > stop time > - If they do, see if (now - time started) is longer than their limit > - Send the email > > I am pretty sure I can make this work but is it a waste of resources? > I would need a cron job constantly (every 5 minutes) monitor user's > start and stop times. > Is there a better way to implement? > > > *2. Weekly Report* > Cron job for sure on the weekly report. > > > If you agree/ disagree please tell me. I wanted to kick around ideas > with others on the community because 2 heads are better than 1. ( And an > internet forum is even better than that! ) > > I appreciate any comments. > Drew Haines > > > > -- -- SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby --- You received this message because you are subscribed to the Google Groups "SD Ruby" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
