Feature request: pass name and/or network to scheduled Autotasks

I have two separate Autotasks set up with the exact same code; one is for Kovan and the other for Mainnet. Because scheduled Autotasks don’t get the sentinel event schema that webhook Autotasks get, they have no context of the network they’re running on. This might be useful for fetching key-value pairs or secrets from within the Autotask that can be associated with the relevant network.

Currently, it seems I have to either manually change network-specific values in the Autotask code before each deploy (which is unsafe and may lead to incorrect values) or keep multiple network-specific files for each Autotask despite having the same code.

Interesting! We could allow having multiple schedules per Autotask, each of them tied to a different Relayer and with some env vars that get injected so the code can recognize in which schedule it is running.

Now, I get the sense that it’s a common use case having the same code for testnet and mainnet. However, do you update the code for both networks at the same time? Or do you first update testnet, run it for a little while, and then run the code update on mainnet?

In the meantime, keep in mind you can use the defender-autotask-client to programmatically update your autotask’s code, reducing the number of manual steps and the chance of entering incorrect values.

I first update testnet, run it for a while, and then update mainnet. I do use defender-autotask-client, but the zipped Autotasks don’t have access to env vars made available at runtime, so updating code may be tricky.

Have you considered using Autotask Secrets as a replacement for runtime env vars?

Yes, but if the scheduled Autotask has no context of whether it’s meant for Kovan or Mainnet, then it cannot pick out a network-specific secret.

When invoked by a Sentinel, however, the Autotask has access to the request.body.sentinel.network, which can be used to get a network-specific secret. For example:

export async function handler(event: AutotaskEvent) {
    const network = event.request?.body?.sentinel?.network;
    if (network) {
        const secret = event.secrets!['secret_for_the_${network}_network']
    }
}

Got it! I’ll take this to the drawing board and see what we can come up with. Thanks for the feedback!!

1 Like

Realize this is an old post but I have very similar needs!

I have 3 autotasks run with cron. Each has a different schedule and one different argument to a contract call.

I'd love to share the code here, and have 3 schedules, and each schedule has it's own secret? So I could have a schedule that runs every 3 hours with TOKEN_ID=1 and every 6 hours with TOKEN_ID=2

Hey @sbauch - we've had other requests for something similar and I think we will implement it in time, but we do not have a clean implementation today. One way to implement something like this though (assuming you want to use identical code in these Autotasks) is to use a convention for the Autotask Name where you are basically passing an environment variable. So for instance, you could have 3 autotasks named:

  • polygon_token_automator
  • mumbai_token_automator
  • goerli_token_automator

And then in the Autotask:

exports.handler = async function(event) {
  const network = event.autotaskName.split('_')[0];
  // do stuff based on value of network
}

Note that this implementation could be unstable (the name is easy to change) but it's the best way I've found for now. Hopefully we'll publish a cleaner implementation at some point - keep an eye on our changelog.

oh thats clever! that way i could maintain one autotask source, and I would just need to deploy it to each autotask ID. and then I could name them in a way where i can use the name to set "environment" variables. cool cool good thinking!