1
votes

I have a flask application that has a job that runs every minute(sends a request to an endpoint and stores some data). But now i want to make a job that runs always in the last day of every month.

I already have stored the last day of every month

from datetime import datetime
import calendar

now = datetime.now()
year = now.year
month = now.month
lastDay = calendar.monthrange(year, month)[1]

I did a job but i am not sure if this works every month or just one time.

def graphs():
    locale.setlocale(locale.LC_TIME, 'pt_PT.UTF-8')
    getMonth()

scheduler = BackgroundScheduler()
scheduler.add_job(func=graphs, trigger='cron', year=year, month=month, day=lastDay)
scheduler.start()
1

1 Answers

1
votes

If you want it to work every month, every year, you need to write:

from datetime import datetime
import calendar

def graphs():
    locale.setlocale(locale.LC_TIME, 'pt_PT.UTF-8')
    getMonth()

scheduler = BackgroundScheduler()
scheduler.add_job(func=graphs, trigger='cron', year='*', month='*', day='last')
scheduler.start()

The use of expressions like * and last to fire on every value or to fire on the last day within the month is very useful when working with a scheduler.

For more details, you can check this documentation