Run a method once a month

Hi all.
I just wondering what best practice is for running a method once a month in a web app.
It will probably be used as a back up but I need to run a report on the first day of a month.

Am thinking of comparing the date with a timer every few hours and the. If date in range pause the timer for 24hours. Is there a better way?
This would have to run at the app level.

Depending on your situation, a cron job to run the routine on a monthly schedule might be easier. With this approach, you’d create a console app that does your backup and runs the report, and then schedule the execution with cron.

Lifeboat can help you set up a monthly cron job very easily. This is the method I use (albeit set as daily) to collect the latest versions of plugins from Einhugur and MBS for Plugins Pro.

If you want to do it all in your main app, you’d probably want to track the last time the report ran. No harm in checking hourly and bailing if it’s not time yet. If you set the Timer period too large, you run the risk of missing your expected date.

1 Like

Thanks Tim.

I promise I will get to lifeboat as I’ve heard good things. To date I’m running on xojocloud hosting.

Cron is definitely the best way to go.

However, if you want to use a Timer for this, a trick I learned is

  • Store the last run date in persistent storage outside the app - file, database, etc.
  • When your app launches, start the timer with a short period
  • The timer will calculate how much time remains until the next execution, if it’s close enough, it fires of the process. Otherwise it sets its own period to 75% of the time remaining.

That smooths out the vagaries of xojo timers. The period will get smaller and smaller, making it more and more accurate. It also protects you against your web app restarting.

That said, use cron if you can.

2 Likes

xojo web apps usually run as linux daemon or windows service 24/7.
you could read the next task with datetime from database, memory it.
you could run a timer in quarter-hour interval.
check if execution is possible or at good time. update/insert database task.