Chron job on api (flask)
Anonymous
TRIALOP

2 years ago

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.

21 Replies

2 years ago

would you mind telling us why you want to set a cron job on an API service?


Anonymous
TRIALOP

2 years 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.


2 years 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


Anonymous
TRIALOP

2 years ago

Ok then, and how can i setup that script as chron on railway? Maybe there is a railway.json to create?


2 years 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


Anonymous
TRIALOP

2 years ago

Ok thanks, i will try this solution later 🙂


Anonymous
TRIALOP

2 years ago

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


2 years 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


Anonymous
TRIALOP

2 years ago

seems working, thanks again @Brody 😄

1236782426391576600


2 years ago

awesome!


Anonymous
TRIALOP

2 years 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

1237790209874333700
1237790210129924400


2 years ago

what option did you take? in code schedular or railways cron schedular?


Anonymous
TRIALOP

2 years ago

in code, using aiocron (i attached the code)…the start command is python3 scriptName.py


2 years 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


Anonymous
TRIALOP

2 years 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?


2 years ago

i assume you have your main service and then another service to run the in code scheduler?


Anonymous
TRIALOP

2 years 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


Anonymous
TRIALOP

2 years ago

1237793856498700500


2 years 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


Anonymous
TRIALOP

2 years 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 😄

1237794902344204500


2 years 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


Loading...