Running one time scripts in context of the configu...
# workers-help
t
I would like to run a script on my local machine which would hook into the context of my worker which is already configured on Cloudflare and then perform any operation in that context, e.g. using D1, KV directly without having to actually run a worker with a fetch handler and then do a an HTTP request to achieve essentially the same. You could think about it as simply running the
node
command with a file in your project which has access to the whole context of the project (e.g. databases, env variables etc). A similar approach from other language/frameworks are Django management commands. The way I imagine this is simply running
wrangler dev
and instead of launching a worker service (which essentially runs as an HTTP service) it would execute a worker "script" which runs once and then exits. Is this possible in workers and if so how?
s
Yeah, I'd use wrangler dev for this. You can look into using wrangler's API () to spawn the worker inside your node script, then you can just fetch it in your script too
t
Thanks. Haven't considered looking for a wrangler API. The
unstable_dev
name put me a off a little bit though. Is there a chance this might become obsolete in the future?
s
It means that there could be breaking changes between wrangler versions - though those should mostly be within the
experimental
part of the options
If it's any consolation,
wrangler pages dev
has been powered by it for almost a year now
t
👍 All right. Thank you again. Will probably move forward using the Wrangler API then.
s
Awesome, feel free to follow up with any questions you get
I'd recommend taking a look at the
packages/wrangler/src/api
part of the repo if you want to see all of the options the API takes