Chron job on api (flask)
Hi!
I was wonder if there was a way to setup a chron job on an api (with base auth).
My current project (id e94c270f-2011-45b4-8571-92189e7bba32) is based on Flask.
From the settings i can set a chron only on deployment, if i understand correctly.
Solution:Jump to solution
your API would be a long running process, you would not put that on a cron schedule, you would either do an async in code schedule job or write another script to do the task and then exit, and have another railway service to just run that script on a schedule
21 Replies
Project ID:
e94c270f-2011-45b4-8571-92189e7bba32
would you mind telling us why you want to set a cron job on an API service?
Sure! My android app relies on a database provided by the BE (the db is saved outside of Railway). From the BE, i have to check if this db is out of date and update it only if needed (i want to do this check once or twice a day). I have a dedicated api for this task.
Solution
your API would be a long running process, you would not put that on a cron schedule, you would either do an async in code schedule job or write another script to do the task and then exit, and have another railway service to just run that script on a schedule
Ok then, and how can i setup that script as chron on railway? Maybe there is a railway.json to create?
no I don't think you need to go down that route, simply use the start command field in the service settings to start your script that does it's task and then exits
Ok thanks, i will try this solution later π
And what about the cost consumption / usage of your solution vs a chron job of the api (5s of time exec in best case, 15s in the worst)? @Brody
it would be a negligible difference, I'm sure a background async schedule would not add much if any overhead, and another service to run the task would only charge you for the time the task runs
seems working, thanks again @Brody π
awesome!
Hi @Brody i was wondering...how does memory usage/cost work? My script is always running yes, but i do stuff only every 6h
what option did you take? in code schedular or railways cron schedular?
in code, using aiocron (i attached the code)...the start command is python3 scriptName.py
then you are charged for the ram you use, doing an in code schedular means your script runs 24/7 so there will always be idle memory usage
My server is on since May 5, so 660min = 11h != 72h...maybe i'm not really understanding this metric XD...BTW Did you have suggestions to optimize this? Like programmatically wake up the server every 6h, do the job and sleep?
i assume you have your main service and then another service to run the in code scheduler?
I deployed on railways only a script that update my db every 6h...it's the only job i delegated to this platform, at the moment
then you could use railway's cron schedular as i mentioned in my first message, that way (if setup correctly) you would only be paying for the resources you use when that task runs and the rest of the time you would not pay for anything
From here? So, i have to change my script to do the job once, and move the chron expression in railway settings...and the service will run every 6h...if i understand correctly...thanks for your help again π
you got it! yep your script just needs to be changed to do the task when its ran and then exit when its done