VR
VR•2mo ago

Is there a problem if I use cronjob as a server "tick"

So I wanted to try using convex for making a board game where you have different rooms with specific logic and events etc.. but i want most of my logic to be on server side so the way I am doing this right now is using a cronjob to trigger every 1 sec "my tick" and update what is required. Actually it does work very well, but I was just wondering is this okay and is there better way to do it? Doing it on the server reduces the amount of calls if we need to make same calls on every user and like that we make calls every second but only from server side, yes clients have some actions as well but not that much. Also this allows me to create "bots" and do more automation that makes the game more interesting. Would love some feedback if thats okay or something else will be better. Of course I can use another technology but I want to do this with convex and like I said I already have done it and it does work. Also currently I am doing this on cloud but eventualy I would like maybe to do self hosted or I will have some limitations there, still have not checked that?
9 Replies
Convex Bot
Convex Bot•2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart•2mo ago
This makes sense for events that are not based on user activity, like timers and such. Anything that is driven by a user interaction should organically flow from a user triggered mutation to a db change to query updates.
VR
VROP•2mo ago
Yes of course, so this is the right spot then 😄 I was just a bit concerned if its okay to call cronjob every 1 second even though the game feels smooth just wanted to check.
ian
ian•2mo ago
You might end up missing some depending on how many current games are in-flight, only if you're backed up overall. A few things I can think of: - Have the mutation handle every game's tick, instead of a per-game tick. - Have a singleton action that runs for 5 minutes straight then re-schedules itself, that's every second doing an action for every game. you pay for all the compute usage in between ticks though, and you need to handle the action dying. so less ideal, but this is what we did for ai-town back in the day
VR
VROP•2mo ago
I am doing it with single mutation that first checks for the active games/rooms and only then continues with the rest of the logic. It does run every second and it will consume on every second even if no games are running at least 1 call, so not ideal but for now i think it will work for me and if it becomes too much maybe i will think of a way to put something to ensure this runs only when having active games without checking that all the time. Also I the self hosted part its super easy to setup especially with coolify so love that part.
ian
ian•2mo ago
I'm a fan of this approach! Also, you can use the crons component if you want to dynamically start/stop crons, but I think running once a second isn't too bad of overhead (<9¢ per day on hosted version)
VR
VROP•2mo ago
Oh thanks for letting me know probably i will add this when i am optimizing it to run only on active rooms. $9 is definetly not bad, especially if the project is active with players etc.. but for now self hosted until developing will be best and not sure if eventually there are more players need to see what will be better, or if i need some of the premium features. @ian Do you have any clue what will be the ideal maximum execution time for a cronjob if i am doing 1 second tick, for example currently I am <100ms but have not yet made full tests with a lot of rooms or people and i will be aiming around 10 active rooms to be the ideal case, but just wondering what should i be looking at for execution time, for example <500ms to be the maximum or i can go higher ? Of course i stay under 1 second but wanted to check if you have experimented with this previously ?
ian
ian•2mo ago
The limit is 1s of CPU time for query/mutation - so if you're doing things that have more latency (e.g. calling ctx.runQuery/ ctx.runMutation / calling components which do those under the hood), they can run longer. If you run into query limits you'll want to have batches running, maybe dynamically starting those crons at runtime when too many games start with the crons component.
VR
VROP•2mo ago
I see, thank you, I will try for now to limit only the games and number of users and try to work with that, even though i also made it to work with batches if i reach some limits but maybe it wont be needed in my case.

Did you find this page helpful?