MultiPaaS

Run workflows

A workflow is a list of steps that find, create, update and delete records together: every step's change lands, or none does. You declare it once in a file. Your app then starts it with a single call to the generated API, which returns a run at once. The run happens in the background, as whoever made the call. Run the commands in your app's directory, after multipaas init, which Build on the generated API covers.

A workflow is part of your model. Declaring, changing and deleting one needs permission to edit the model, and each makes a new API version.

Write the definition

A workflow's definition is one JSON file. This one closes a ticket, so it needs a Ticket entity whose status field is an enum with the value CLOSED. Model your data shows how to add one.

{
  "key": "closeTicket",
  "name": "Close a ticket",
  "description": "Closes a ticket",
  "trigger": {
    "kind": "onDemand",
    "inputs": [{ "key": "ticketId", "type": "ID", "required": true }]
  },
  "runAs": { "kind": "starter" },
  "steps": [
    {
      "key": "ticket",
      "kind": "find",
      "name": "Find the ticket",
      "description": "Looks the ticket up by id",
      "entity": "Ticket",
      "where": { "id": { "eq": { "ref": "trigger.input.ticketId" } } },
      "limit": 1
    },
    {
      "key": "close",
      "kind": "update",
      "name": "Close it",
      "description": "Sets the status to closed",
      "entity": "Ticket",
      "target": { "each": "ticket" },
      "values": { "status": "CLOSED" }
    }
  ]
}

Entities are named by their GraphQL type name and fields by their GraphQL field name, as multipaas model show prints them. Every property the file doesn't list below is refused.

A workflow can't set a file or related field. Text the database can't store, a NUL character or an unpaired surrogate, in a name, a description or a literal in values or where, is refused as a problem at its path with must not contain a NUL character or an unpaired surrogate.

Not supported yet

A definition that uses one of the other kinds the model knows is refused with its own message:

Any other kind or value is refused as not valid, with the message of the property it is on.

Declare it

multipaas workflows create --file closeTicket.json

A definition that doesn't hold together is refused with WORKFLOW_INVALID, and the CLI prints every problem as path: message, such as steps[1].target.each: ticket is not an earlier find step on Ticket. Fix them all and run the command again.

multipaas workflows list and multipaas workflows show KEY read workflows back. To change one, edit the file and run multipaas workflows update KEY --file closeTicket.json: the workflow keeps its id, and if the file's key is different, that becomes its new key. multipaas workflows delete KEY deletes it.

A change to the model is refused when a live workflow would no longer resolve against it, such as removing a field the workflow uses, or changing a field's type so a condition no longer fits. The message names the workflow. Removing an entity or field it uses ends workflow KEY uses a field or entity this change removes; update or delete the workflow first. Adding a required field with no default, making a field required or removing a required field's default, while a create step leaves that field out or a step sets it to null, says workflow KEY step STEP would leave required FIELDS empty; update or delete the workflow first. Any other change the workflow no longer fits says workflow KEY no longer fits this model (WHY); update or delete the workflow first. A run uses the model at the API version the call is made on. To get past the required-field refusal, change what the named step does with the field, then retry the change. Where the step sets the field to null, give it a value. Where a create step leaves it out, set it in the step's values. A field you are adding can't be named in a step yet, so add it with a default, or add it optional, set it in the step's values, and then make it required. A file field can't be set by a workflow, so it can't be required while a create step writes its entity: remove that step, or leave the field optional. Or delete the workflow. To get past the other refusals, update the workflow so it no longer depends on what is changing, or delete it.

An application with workflows can't have an entity or enum whose GraphQL type name is WorkflowRun or WorkflowRunStatus, or an entity whose plural name is WorkflowRun, which makes its generated query workflowRun, because the generated API uses those names. Declaring one in an application that has workflows, or the first workflow in an application that has one, is refused with WORKFLOW_RUN_NAME_CLASH. To clear it, give the entity or enum a different --type-name with multipaas entity create or update, or multipaas enum create or update, and give the entity a different --plural-name when the plural clashes. Changing only --name keeps the type name of one that already exists. Or delete the workflows.

An API version made before you update or delete a workflow keeps the mutation it had, with the definition and key the workflow had then. Only versions made after the change see it.

Let a caller run it

A workflow becomes a mutation in the generated API: run plus the key with its first letter capitalized, plus Workflow, so closeTicket becomes runCloseTicketWorkflow. It appears in the next API version made after you declare the workflow, so move your app to it first.

Call it with the workflow's inputs in one input argument. A workflow with no inputs takes input: {}.

mutation ($ticketId: ID!) {
  runCloseTicketWorkflow(input: { ticketId: $ticketId }) {
    id
    workflowKey
    status
    failedStep
    error
    recordsChanged
    createdAt
    finishedAt
  }
}

The caller needs the workflow.run permission for that workflow. Give a role that permission, using the workflow's id, the ID column of multipaas workflows list:

multipaas roles update --role ROLE_KEY --add-permission workflow.run=WORKFLOW_ID

A workflow id that is unknown, deleted or another application's is refused with workflow X is not a workflow of this application.

Run background jobs shows how to give a role to an app identity, and a person gets one when you invite them to an app-tenant.

What a run does

The call returns at once with the run: its id and workflowKey, and normally status QUEUED, with failedStep and error null, recordsChanged 0, createdAt set and finishedAt null, and nothing changed yet. A worker picks the run up and runs the steps in order in one transaction, in the background. Follow it by asking the generated API for the run by its id until status is SUCCEEDED or FAILED; QUEUED and RUNNING are not final. Ask from the same environment and app-tenant, as the same user, that started the run: workflowRun(id) answers null for any other.

query ($id: ID!) {
  workflowRun(id: $id) {
    status
    failedStep
    error
    recordsChanged
    finishedAt
  }
}

Only the person or app identity who started a run, in the app-tenant it was started in, can read it. Anyone else, and an id that matches no run, gets null. workflowRun is in every API version that has a workflow.

Read the run history

Every run, whether queued, running, succeeded or failed, is kept with the inputs the caller gave, who ran it, how many records it changed and, for a failed run, the step that failed and why. Read a workflow's runs in an environment, newest first:

multipaas workflows runs KEY

Without --env it reads the environment in your app's multipaas.json, or the default one when there is none. Add --env ENV_ID for another. Run history shows the inputs callers supplied, which are your app's data, so it needs permission to edit the model.