Python/ Scheduled task working locally but not in Render

Hi everyone, first time deploying an app here :wink:

I want to implement a scheduled task on render but it does not seem to work.

def scheduled_maps():
    scheduler = BackgroundScheduler()
    scheduler.add_job(func=generate_and_save_maps, trigger=CronTrigger(hour=11, minute=50), timezone=pytz.utc)
    scheduler.start()
    print("Scheduler started.")
    atexit.register(lambda: scheduler.shutdown())

I do see the print statement in my logs “Scheduler started” but nothing happens when it’s time.

It works perfectly fine locally.

Hi,

There will always be differences between environments: development mode/Local, production mode/Render, etc. These differences need to be considered and configured as required for your own app in each environment.

If you’re using a free Web service, this will spin-down after 15 minutes of inactivity, meaning your scheduler process would no longer be running. When a new request comes in, a free web instance will be spun up. More on this in the docs.

Scheduled tasks/jobs are better suited to Render’s dedicated Cron Job service, or if you want to use your own queue/scheduler a Background Worker. However, these service types don’t have a free offering.

Alan

Hi Alan,

Thank you very much for your time !

Since it is a personal project, I wasn’t willing to pay for it to work but I am still curious to see it live.
What should I understand from the offer since it is paid per time, would I pay everytime the cron job is worked or the time my cron job takes to be done ?

The idea was to run the job once a day to generate new maps daily.

From what I understand, I can’t execute a Cron Job from a python code right ? Only commands ?

Ty !
Sofiane

A Cron Job is charged for the total amount of time it runs during the month, with a minimum of $1 USD. More on the pricing page.

A Cron Job would expect a command. For example, if you want to run a Python script, it would be something like: python my_task_script.py

Alan

But if the script is part of my web app, should I run the whole app via a Cron Job or make it separate ?

If so, would creating a simple scheduler.py with a call to the function inside of my repository (that contains the whole app) be sufficient and allow me to use a command ?

I am a bit confused on how the Cron will affect my web app still, do I simply need to add my database into the environment variables of my Cron ?

Services are separate, but you could deploy the same repo to both a Web Service and a Cron Job. Then the difference would be to call what you need, e.g. Web Service Start Command = start a web server, Cron Job Command = call a script.

The Cron Job could access a database in the same way as your Web Service, e.g. env var with a connection string.

Alan

I think I understand better now but i still have some doubts concerning how would the cron job run and “talk” with my app.

I am using Flask as a framework for my app.

I initialize the DB in the app and the function I’d like to schedule starts like this :

def generate_and_save_maps(difficulties_to_rerun=None):

     with app.app_context():

and at some point in the end of the function, I store the maps into my DB.

I am having a hard time to conceptualize how the script will understand my app is running if I don’t “inform” him that it is working. Does the cron job even need to know that the app is working ?

Will the script understand that DB exists even if I just the whole function in a separate file ?

It wouldn’t matter if the app (Web Service) is running or not, the Cron Job is it’s own instance, it can communicate with the database directly.

I’m assuming you have a Postgres DB and your Web Service defines the database connection string as an environment variable.

The Cron Job can have the same environment variable set to connect to the same database.

Alan

1 Like

Just a quick update to say that it worked !
I’m so happy I just launch my first app ever :smiling_face_with_tear:

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