tdawg
tdawg2w ago

If you don't mind, yes please 🙏 I want

If you don't mind, yes please 🙏 I want it to run immediately after convex redeploys itself – i.e. nothing external is triggering it
28 Replies
Sara
Sara2w ago
Ok, so you want to run the actual cron job itself only once, correct? I initially read it as that there's an action running, i need to google this real quick
tdawg
tdawgOP2w ago
That's right! It's funny to me that Convex has such awesome cron / scheduling features, but I can't seem to just set one to run as a one-off... tricky!! Thanks for helping 🙂
Sara
Sara2w ago
Yeah, Using cron jobs i don't think it's possible, you're gonna have to run it byweekly or monthly or what ever, cause that's what a cron job is after all
tdawg
tdawgOP2w ago
yeah this is what i'm running up against it's a shame that there's no convex startup routine I can hook into
Sara
Sara2w ago
😅 what?
tdawg
tdawgOP2w ago
well, if there was a startup hook that ran after every deploy, I could then use that to (hopefully) trigger an action it's funny – i can schedule a function to run once at any point in the future, but i can't just get it to run once after every deploy
Clever Tagline
I recommend posting in #support-community and tagging it as a feature request. I don't recall anyone else request anything like this before. You might want to include more details about your use case (i.e. why this feature would be useful to you) to help them gauge the potential interest for other users.
tdawg
tdawgOP2w ago
thanks, when i get some spare time i'll put a request together and explain in more detail about what i'm trying to achieve
Sara
Sara2w ago
This is definitely a first, but just to be clear, the point of a "cron job" is to make repetitive calls, and if you want to run it only once you would use what i described earlier (using a scheduler accessed from a mutation) , you might be able to run the function in the deployment command which also should work, and is the easiest route, But if you want to make them run once after every deployment programmatically, I think this would require you to do some work beneath just a cron job, If i remember correctly, you might be able to do this using the platform API, if you cmd+f this PlatformDeploymentResponse https://api.convex.dev/v1/openapi.json But also this is not what you want 😅 If you give us what your use case is in the support thread, we might think of something else
omusumo
omusumo2w ago
right so its not a cron job then is it its an event-triggered function the event is admin deployed new blah
tdawg
tdawgOP7d ago
Thanks both. Indeed, I don't want to do this with a cron job (sorry, I thought that was clear) – it was just the only mechanism available right now (that I knew of) which could run something without requiring user interaction or external scripts... As I said, I'd love a 'startup' hook (or something similar), e.g:
// startup.ts

import { Startup } from 'convex/server'

const startup = new Startup()

startup.run(ctx => {
// run whatever I want here after each deploy
})

export default startup
// startup.ts

import { Startup } from 'convex/server'

const startup = new Startup()

startup.run(ctx => {
// run whatever I want here after each deploy
})

export default startup
@Sara – appreciate you mentioning PlatformDeploymentResponse – I will definitely take a look at that I can think of a few use cases for this kind of hook, but I'll consolidate my thoughts and share once I have some more time
Sara
Sara7d ago
This is a bit problematic, you should consider adding a CLI Command to your deployment command 😅 easiest and fastest way and you don't have to do anything after that
Sara
Sara7d ago
CLI | Convex Developer Hub
Command-line interface for managing Convex projects and functions
tdawg
tdawgOP7d ago
Thanks – how would I do this during local dev though, where I'm saving files and convex functions redeploy locally? I'm running the full convex backend stack locally.
Sara
Sara7d ago
Can you give me a description of what you're trying to do? For dev it would be something like pnpx convex dev & pnpx convex run functionName ^ you should do some googling and gpting for this one incase it doesn't work
tdawg
tdawgOP7d ago
If it was just the initial startup, I would definitely do that
Sara
Sara7d ago
Mhm?
tdawg
tdawgOP7d ago
I mean any time a convex function is redeployed though so if I literally hit CMD + S in a convex file, I'd like to re-run an action
Sara
Sara7d ago
I need to think about this, in the mean time use #support-community
tdawg
tdawgOP7d ago
Sure thing – thank you! 🙏 FYI – I figured out a slightly hacky workaround with crons + a node-contained action (which will persist state across the lifetime of the process)
tdawg
tdawgOP7d ago
Code example here
No description
tdawg
tdawgOP7d ago
with a low interval of 5 seconds, it runs close enough to satisfy my needs
Sara
Sara7d ago
Thia runs every 5 seconds, does it not?
tdawg
tdawgOP7d ago
lol – yes, but the state flag prevents the underlying action from ever running again i'm not over the moon about it, but it works... 🤷‍♂️
Sara
Sara7d ago
I don't think it does, you should add self destruction to it or something I don't think the files retain state like you think it does 😅 i might be wrong though
tdawg
tdawgOP7d ago
they don't, unless you run it within node with use node there are tons of node libs that hold mini state like this so i think it's a requirement for it to hold runtime state, otherwise lots of node packages would probably break i'd much prefer a startup hook offered by convex though – so i will put together a feature request at some point 🙂
Sara
Sara7d ago
If it works for you then i guess this is resolved
tdawg
tdawgOP7d ago
i haven't deployed to a production env yet so it remains to be seen... but i'm hopeful! thanks for your help / time on this though much appreciated

Did you find this page helpful?