Sometimes you donโ€™t know when you write the code what the trigger or schedule will be.

DynamicTrigger and DynamicSchedule allow you to configure triggers and schedules at runtime.

DynamicSchedule

//1. create a DynamicSchedule
const dynamicSchedule = client.defineDynamicSchedule({
  id: "dynamicinterval",
});

//2. create a Job that is attached to the dynamic schedule
client.defineJob({
  id: "user-dynamicinterval",
  name: "User Dynamic Interval",
  version: "0.1.1",
  //3. set the DynamicSchedule as the Trigger
  trigger: dynamicSchedule,
  run: async (payload, io, ctx) => {
    await io.logger.info("The userId is ", ctx.source.id);
  },
});

//4. Register the DynamicSchedule anywhere in your app
async function registerUserCronJob(userId: string, userSchedule: string) {
  //use the userId as the id for the DynamicSchedule
  //so it comes through to run() in the context source.id
  await dynamicSchedule.register(userId, {
    type: "cron",
    options: {
      cron: userSchedule,
    },
  });
}

//5. Register inside other Jobs
client.defineJob({
  id: "register-dynamicinterval",
  name: "Register Dynamic Interval",
  version: "0.1.1",
  trigger: eventTrigger({
    name: "dynamic.interval",
    schema: z.object({
      userId: z.string(),
      seconds: z.number().int().positive(),
    }),
  }),
  run: async (payload, io, ctx) => {
    //6. Register the DynamicSchedule (this will automatically create a task)
    await dynamicSchedule.register(userId, {
      type: "cron",
      options: {
        cron: userSchedule,
      },
    });

    await io.wait("wait", 60);

    //7. Unregister the DynamicSchedule if you want (this will automatically create a task)
    await dynamicSchedule.unregister(userId);
  },
});

DynamicTrigger

//1. create a DynamicTrigger
const dynamicOnIssueOpenedTrigger = client.defineDynamicTrigger({
  id: "github-issue-opened",
  event: events.onIssueOpened,
  source: github.sources.repo,
});

//2. create a Job that is attached to the dynamic trigger
client.defineJob({
  id: "listen-for-dynamic-trigger",
  name: "Listen for dynamic trigger",
  version: "0.1.1",
  trigger: dynamicOnIssueOpenedTrigger,
  integrations: {
    slack,
  },
  run: async (payload, io, ctx) => {
    await io.slack.postMessage("Slack ๐Ÿ“", {
      text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
      channel: "C04GWUTDC3W",
    });
  },
});

//3. Register the DynamicTrigger anywhere in your app
async function registerRepo(owner: string, repo: string) {
  //the first param (key) should be unique
  await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
    owner,
    repo,
  });
}

//4. Register inside other Jobs
client.defineJob({
  id: "new-repo",
  name: "New repo",
  version: "0.1.1",
  //5. when a new repo is created in your org
  trigger: github.triggers.org({
    event: events.onNewRepository,
    org: "triggerdotdev",
  }),
  run: async (payload, io, ctx) => {
    //6. Register the dynamic trigger so you get notified when an issue is opened
    await dynamicOnIssueOpenedTrigger.register(`${owner}-${repo}`, {
      owner,
      repo,
    });
  },
});