Once a Day Process

I need to run a process once a day from a Xojo web app at around 10:00 am. Usually I would use a cron job or launchd or something in the os, but I need this to run from the web app with no other dependencies. My idea is to run a method when the app launches that calculates the next time that the process should run. Then a timer will be set to fire at that time. When the timer fires, the process will be run and the timer will be reset to run the next day at 10:00 am.

I realize there is a chance that the app could be crashed at the time the timer is fired and could miss a day. That should not be too bad if it happens. Other than that, is there something I am missing?

I’d store a success value, like the date the last time it completed successfully, in a database so that you can fire the event when the app is run again in case of a crash.

2 Likes

you can just start a timer which runs permanently, and within the timer you check the current time. If it is 10:00 am execute your code. But as 10:00 am is valid for 59 seconds, you have to use a flag variable, so that your code will only run once during those 60 seconds. If the time is not 10:00 am you can change that flag variable in your timer.

1 Like

And as a general rule, it may be better to always use the UTC time instead of the local time. This is particularly true if trying to schedule something around 2-3am instead of 10am due to local time variations around switching to/from DST or summer time or whatever you want to call it.

Or just have your time run every minute (or second or whatever) and compute the difference between the current time and a saved value of the last time completed normally. If over 24 hours and the job not currently running, set a flag and start it running. When the job finishes, clear the flag and save the date/time if successful.

2 Likes

You may also like to consider having the scheduled process as a separate app. This has the advantage that if the main app is down, the scheduled process would still run. The down side is that you have a little more code to maintain, but if both apps use shared external code modules, this is not a problem.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.