R
Railwayβ€’7mo ago
feduss

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:
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
Jump to solution
21 Replies
Percy
Percyβ€’7mo ago
Project ID: e94c270f-2011-45b4-8571-92189e7bba32
Brody
Brodyβ€’7mo ago
would you mind telling us why you want to set a cron job on an API service?
feduss
fedussOPβ€’7mo ago
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
Brody
Brodyβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
Ok then, and how can i setup that script as chron on railway? Maybe there is a railway.json to create?
Brody
Brodyβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
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
Brody
Brodyβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
seems working, thanks again @Brody πŸ˜„
No description
Brody
Brodyβ€’7mo ago
awesome!
feduss
fedussOPβ€’7mo ago
Hi @Brody i was wondering...how does memory usage/cost work? My script is always running yes, but i do stuff only every 6h
No description
No description
Brody
Brodyβ€’7mo ago
what option did you take? in code schedular or railways cron schedular?
feduss
fedussOPβ€’7mo ago
in code, using aiocron (i attached the code)...the start command is python3 scriptName.py
Brody
Brodyβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
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?
Brody
Brodyβ€’7mo ago
i assume you have your main service and then another service to run the in code scheduler?
feduss
fedussOPβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
No description
Brody
Brodyβ€’7mo ago
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
feduss
fedussOPβ€’7mo ago
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 πŸ˜„
No description
Brody
Brodyβ€’7mo ago
you got it! yep your script just needs to be changed to do the task when its ran and then exit when its done
Want results from more Discord servers?
Add your server