#work flow

13 messages · Page 1 of 1 (latest)

winter ruin
#

Can you give an example of where a workflow would be better than a cron job? can a work flow go from steps 1-2-3 and the back to 1?

uneven oracleBOT
#

Hi @winter ruin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Certainly! Here’s a focused answer to your questions about workflows vs. cron jobs in Convex, and about workflow step flexibility:

1. Example: When a Workflow is Better Than a Cron Job

A workflow is preferable over a cron job when you need to manage complex, multi-step, long-running, or stateful processes that require durability, retries, and the ability to track progress or react to failures. For example:

  • Customer Support Ticket Processing: Imagine a process where you fetch user history, summarize it, create a support ticket, fetch additional context, generate a resolution, and finally notify the user. Each step may depend on the previous one, may involve retries, and you want to ensure the process completes even if the server restarts. This is best modeled as a workflow, not a cron job, because workflows can track state, handle retries, and guarantee eventual completion, even across failures or restarts. Cron jobs, in contrast, are best for simple, stateless, recurring tasks like sending a daily email or cleaning up old records Durable workflows are the missing abstraction Building reliable workflows.

2. Can a Workflow Go from Steps 1-2-3 and Then Back to 1?

#

Yes, Convex workflows are flexible and can implement loops or cycles. For example, you can have a workflow that goes through steps 1, 2, 3, and then, based on some condition, returns to step 1. This is useful for scenarios like repeated polling, retries, or agentic reasoning cycles. The workflow handler is just code, so you can use loops, conditionals, and even cycles of reasoning and acting (ReAct) patterns Complex workflow patterns.

“Cycles of Reasoning and Acting (ReAct)” and “Workflows that can be paused and resumed” are explicitly supported patterns, so you can model workflows that revisit previous steps as needed.

In summary:

  • Use workflows for complex, stateful, multi-step, or long-running processes that need durability and progress tracking.
  • Workflows can absolutely loop or revisit steps, unlike cron jobs, which are for simple, stateless, scheduled tasks.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@winter ruin Kapa.ai is still learning and improving, please let me know how I did by reacting below.

winter ruin
#

@uneven oracle can you give an example of a workflow?

uneven oracleBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Certainly! Here’s a concrete example of a workflow in Convex, adapted from the knowledge sources:

Suppose you want to automate a customer support process. The workflow might look like this:

  1. Fetch the customer’s history from the database.
  2. Summarize the history using an AI model.
  3. Create a support ticket with the summary.
  4. Fetch additional context for the support request.
  5. Generate a proposed resolution using an LLM.
  6. Respond to the user.

Here’s how you might define such a workflow in Convex:

export const supportWorkflow = workflow.define({
  args: { user: User, message: Message },
  handler: async (step, { user, message }) => {
    // 1: Fetch the customer history from the database
    const userHistory = await step.runQuery(s.customerHistory, { user });
    const summary = await step.runAction(s.summarizeHistoryWithAI, { message, userHistory });

    // 2: Create a support ticket
    const ticketId = await step.runMutation(s.createTicket, { summary, user, message });

    // 3: Fetch related context
    const context = await step.runAction(s.fetchSupportContext, { summary, message, user });
    await step.runMutation(s.updateTicket, { ticketId, status: "in-progress", context });

    // 4: Generate a proposed resolution
    let resolution;
    for (let i = 0; i < 5; i++) {
      resolution = await step.runAction(s.getAIResolution, { context, userHistory, summary });
      // Retry up to 5 times if the resolution isn't good enough
      if (.5 < await step.runAction(s.gradeResolution, { context, resolution })) {
        break;
      }
    }
    await step.runMutation(s.updateTicket, { ticketId, status: "resolution", resolution });

    // 5: Respond to the user
    await step.runAction(s.sendUserReply, { user, message, resolution });
  }
});
#

This workflow is durable, supports retries, and can recover from failures or server restarts, ensuring each step is completed exactly once Revisiting our example.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@winter ruin Kapa.ai is still learning and improving, please let me know how I did by reacting below.

winter ruin
#

@uneven oracle are all of the actions just defined into 1 workflow?

uneven oracleBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Not all actions need to be defined within a single workflow. In Convex, actions, queries, and mutations are defined as separate Convex functions. A workflow then orchestrates these functions by calling them as steps using methods like step.runAction, step.runQuery, and step.runMutation. The workflow itself is defined as a handler that sequences these steps, but the underlying actions can be reused across different workflows or called independently elsewhere in your application.

For example, in the support workflow example, each step (such as summarizing history, creating a ticket, or sending a reply) is typically implemented as its own action or mutation. The workflow then references these actions by calling them in sequence or conditionally as needed Revisiting our example.

So, while a workflow brings together multiple actions, those actions are not defined "inside" the workflow—they are defined separately and orchestrated by the workflow handler. This modular approach allows for code reuse and better organization.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: