Forums

Flask app stops automatically or console stops printing ?

Hi ,

I have a flask app running APScheduler which is scheduled to trigger every hour .

scheduler = BackgroundScheduler()
job_scan = scheduler.add_job(xyz, 'cron',minute=0,second=3)
lev = scheduler.add_job(abc, 'cron',hour=0,minute=0,second=0)
scheduler.start()

During this trigger , there are some print statements that are printed on the console. Now sometimes when I come back to the app console , I do not see the logs on the console , so it makes me think has the scheduler stopped or the logs have been stopped on the console ?

I noticed If I keep the browser tab open with the app console open , and check the console on random intervals (hours) , it prints on console just fine. But when I close the app tab and reopen the app file with it's console , I find an empty console sometimes. This is a random observation could be wrong but I've noticed most of the times it with clear console when I reopen the app console , and even after opening it , at the time of next trigger , there is still no prints on console , so I have to stop the app , restart it and reload from the Web section , then it prints as expected on console.

So wanted to understand , whether the scheduler or console session stops ? or the console buffer is full ? What exactly is happening here ? Please help , I want to make sure my app is running the scheduler all the time.

Our consoles aren't meant to be used for long running jobs. Use Always-on task instead.

I don't see an option of scheduling the task at a certain second :) Is this a feature which could be added on the platform in the near future ?

We probably won't offer scheduled tasks at per-second resolution, because things like machine load could cause variations of more than a couple of seconds either way.

However, always-on tasks are a different kind of thing, configured in the separate table at the bottom of the "Tasks" page. They're not scheduled to run at a particular time -- instead, when you create one, we start the specified program up. If it then exits for any reason -- say, if it crashes, or if the machine it's on has hardware problems, or anything else -- then we start it up again on another machine.

So your APScheduler script should work fine as one of those. It will start up, and continue to run, just like it will if you run it in a console, but -- unlike running in a console -- it will be automatically restarted if necessary.

Oh understood !! That solves my problem completely. My bad , I was looking at Scheduled Tasks and not Always-on. Thanks for the quick support.

No problem -- glad the problem is solved!

Although I am getting a ModuleNotFoundError for one of the import while running my python file with python 3.6 and python 3.7. With python 3.8 , no logs and stuck in starting state , any idea about that ?

If you're getting import errors that most probably means that you didn't install the required package for those Python versions. If you want to debug your script, I recommend doing it in a console and adding some prints, so you can establish where is it failing -- it will save you some time, since setting up an Always on task usually takes some. When the script is ready, run it as an Always on task.

Makes sense , I run my app with Python 3.8 console , hence import errors coming with 3.7 and 3.6. WIth 3.8 version , always-on task was in Starting state for more than an hour without any error log. I believe I'll wait for more time ? Usually any estimate of the starting time for an always-on task , to go from starting to running state ?

One hour seems way too long -- is your script running correctly in a console?

yes , it is running on console just fine.

Are you running the task the same way you run it in a console? The behaviour I see in our system shows that the script is exiting immediately it's being run, and then restarts, so it's in a start-exit loop. You may add some prints to the script to see how it behaves.

Um yes , the same file that I run on console I have added in the always-on task. And as you see above the app.py file has an APScheduler that triggers every hour. I am able to see the output on console every hour.

I added 3 print statements. I restarted the app on console . I am able to see the prints in task logs as well but with a delay without restarting the task (Note that task is still in the starting state )

After flushing the output buffer for print , I am able to see logs added in the app at irregular intervals (seconds,minutes difference).

So I'm guessing as you mentioned , looks like app is starting and stopping as the logs are not continuous.

The script file was not executable . I've modified the permissions via chmod. Hope it will work now.

If that was an issue, it wouldn't start on the first place (if you were running it with python <scriptname>, you don't need it to be executable). So if you confirmed that the script is in start-exit loop, it's most probably not what you want in an Always-on task. Usually users have kind of infinite loop inside the script so it runs continuously (everything depends on what the script is supposed to do, of course). You may find some inspiration in this help page (it links to a blog post as well).

Thanks for the suggestion. I would like to understand one thing about running the app. Usually I run it by clicking the ">>>Run" button on the app file and that is how my app is able to run continuously on console with the APScheduler - BackgroundScheduler. However , I just noticed , if I am running the same file via bash console using "python3.8 file-location" , it is running the script and exiting (same how it is run in always-on task). So , I believe how the app is running via ">>>Run" button , If I do the same in always-on task , the problem could be solved. Could you please shed some light on this.

It's hard to guess without knowing the code. Have a look at this example which is linked in the APScheduler docs. There is an infinite loop which -- as the comment says -- " is here to simulate application activity (which keeps the main thread alive).". My guess would be that when you run the app using the Run button the main process keeps to be alive (Python REPL) and thus the scheduler can work. In the Bash console and Always on task, the execution of your script ends when the last function returns. So, in order for this to work as an Always on task, you probably need to add such infinite loop to your script.

Thank you, that gave me a better understanding . I added an infinite loop and it is running on bash and always-on task.

Great, thanks for confirming that!