Programmatically Rescheduling Pipedream Workflows from Node.js Code

I’m looking to tap into the Pipedream community for some insights on workflow scheduling. Currently, I have a workflow that triggers daily at 6 PM, which executes several steps including making HTTP requests and running Node.js code.

Here’s my question: Is it possible to programmatically create a new schedule for this workflow from within my Node.js code? Specifically, I want to handle scenarios where the HTTP requests encounter rate limits or other error responses. Instead of waiting until the next scheduled run at 6 PM the following day, I would like to repeat the workflow one hour later after an error occurs.If anyone has experience with this or can point me in the right direction on how to achieve this, I would greatly appreciate your input! Thank you!

Hi @anondev,

To achieve the functionality of programmatically creating a new schedule for your workflow from within your Node.js code, you can use Pipedream’s built-in features to handle errors and retry logic. Here’s a step-by-step approach to achieve this:

  1. Error Handling and Retry Logic: Pipedream allows you to configure auto-retry for workflows. This means if your workflow encounters an error, it can automatically retry the execution. You can set the retry logic to attempt a rerun after a specified period, such as one hour. This is a built-in feature and doesn’t require additional coding.

  2. Custom Error Handling: If you want more control over the retry logic, you can implement custom error handling in your Node.js code. You can use the $.flow.rerun method to programmatically rerun the workflow after a delay if an error occurs. Here’s a basic example:

    export default defineComponent({
      async run({ steps, $ }) {
        try {
          // Your HTTP request or other logic here
        } catch (error) {
          console.error("An error occurred:", error);
          // Schedule a rerun of the workflow in one hour
          $.flow.rerun({ delay: 3600 * 1000 }); // delay is in milliseconds
        }
      },
    });
    
  3. Using the Schedule App: If you need to create a new schedule dynamically, you might consider using the Schedule app’s triggers to set up a new schedule. However, creating a new schedule programmatically from within a workflow isn’t directly supported via API calls. Instead, you can manage your workflow’s schedule through the Pipedream UI or use the retry logic as described.

For more detailed control over scheduling and error handling, you might need to combine these approaches or consider using external services to manage complex scheduling requirements. If you have further questions or need more assistance, feel free to reach out to the Pipedream community or visit Pipedream Support.

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.

You can also auto-retry on errors, a feature available in the Advanced plan where the event will automatically retry up to 8 times in a 10 hour span from the step it failed. This is the easiest and most elegant way

You can use $.flow.rerun. Which is option #2 mentioned by Pi.

When I run $.flow.rerun will it rerun only the current step of workflow or the whole workflow? I need to schedule running whole workflow in an hour from the “code” step. I need to rerun also steps before that and after that (from “definitions” to “custom_request”).

$.flow.rerun() will resume & continue the workflow from the current step.

But when the workflow resumes from $.flow.rerun(), it can trigger itself via HTTP request, and then terminate using $.flow.exit().

So effectively, if you want to schedule the workflow to run in one hour (or whatever interval), you can use $.flow.rerun() to delay for one hour before re-triggering itself.

1 Like

Perfect suggestion from @mroy. Here’s a code sample that I asked Pi to write:

export default defineComponent({
  async run({ steps, $ }) {
    try {
      // Your main logic here

      // If successful, continue the workflow
      return "Success!";
    } catch (error) {
      console.error("Error encountered:", error);

      // Schedule a rerun 1 hour later
      const oneHourInMs = 60 * 60 * 1000;
      $.flow.rerun({ delay: oneHourInMs });

      // trigger your workflow
      await axios($, {
        url: "", // your trigger URL
        method: "POST", // or GET
        data: {} // data you want to send
      });

      // Exit the workflow due to the error
      return $.flow.exit("Exiting due to error, will retry in 1 hour.");
    }
  },
});

But my workflow uses schedule daily at 6pm, not http request as trigger

You can add another trigger to your workflow.

Also, the code Andrew posted won’t exactly work as-is, because you need some code to handle when the workflow resumes from $.flow.rerun(). So it should look something like this instead:

export default defineComponent({
  async run({ steps, $ }) {
    if ($.context.run.runs > 1) {
      // Resuming from $.flow.rerun()
      // Trigger a new run of your workflow
      return await fetch("http://your-workflow-url/");
    }

    try {
      // Your main logic here

      // If successful, continue the workflow
      return "Success!";
    } catch (error) {
      console.error("Error encountered:", error);

      // Schedule a rerun 1 hour later
      const oneHourInMs = 60 * 60 * 1000;
      return $.flow.rerun({ delay: oneHourInMs });
    }
  },
});