Scheduling workflows
Some work needs doing at a set time: a report every morning, a clean-up every Sunday night, a change in tonight’s maintenance window. Recipe 7.3 used a policy for work at intervals. For most timetable jobs a schedule is simpler. It’s one workflow, one set of inputs, and a recurrence, and Orchestrator starts the run when the time comes.
In this recipe you schedule Build VM report from recipe 7.6 to run every weekday morning, look at what decides whose identity the run uses, and then do the same from a script and from the REST API. The last part is about the thing that makes schedules fail quietly: the stored credentials of the starting user.
Getting ready
- The workflow Build VM report from recipe 7.6, in VCF Orchestrator Guide/Chapter 7.
- The
vrohelper from recipe 7.1, for steps 14 and 15. - The time zone of
vco01.vmw.lab. Schedules run on the server’s clock, so check it under Administration or in the appliance’s settings before you pick a time.
How to do it
Schedule a recurring run
- Go to Library > Workflows and open Build VM report, or select its card. Click Schedule.
- Under General, enter the name
VM report every weekdayand the descriptionBuilds the Chapter 7 VM report at 06:00, Monday to Friday. - Set Starting user. Leave it at the current user for now. How it works explains the three choices.
- Under Scheduling, set Start to tomorrow at
06:00, and Start if in the past to No, so that a schedule saved after 06:00 waits for the next morning instead of running at once. - In Schedule, add a recurrence that repeats every day at
06:00, limited to Monday to Friday. - Build VM report has no inputs, so there’s no Workflow section. For a workflow with inputs, that section asks for the values the scheduled runs will use, as a run would.
- Click Save.
The Schedule dialog for Build VM report: General with name VM report every weekday, description, and Starting user open on its three choices; Scheduling with Start tomorrow at 06:00, Start if in the past = No, and a daily recurrence at 06:00 limited to Monday to Friday.
Watch it and change it
- Go to Activity > Scheduled.
VM report every weekdayis listed with its workflow, its next run time, and its state. - The next morning, open Activity > Workflow Runs. A run of Build VM report started at 06:00, and
vcfguide-vm-report-latest.htmlhas a new version (recipe 7.6). - To change the time, open the schedule from Activity > Scheduled, change the recurrence to
07:00, and save. The next run time in the list moves. - To stop it for a while without losing it, use the schedule’s suspend or pause action, and resume it later. To remove it, click Delete on the schedule.
Activity > Scheduled: VM report every weekday with workflow Build VM report, next run tomorrow at 06:00 (or 07:00 after step 10), state, and starting user; the schedule panel open with Edit, the suspend or pause action, and Delete.
Schedule a one-off run from a script
- Some runs should happen once, at a set time: a change at 22:00 tonight, or a clean-up the day after a VM is built. Create the action
com.vcfguide.chapter7/scheduleReportAtwith one input,startAt(Date), and the return typeTask:
var wf = null;var category = Server.getWorkflowCategoryWithPath("VCF Orchestrator Guide/Chapter 7");var found = category ? category.workflows : [];for (var i = 0; i < found.length; i++) { if (found[i].name === "Build VM report") { wf = found[i]; }}if (!wf) { throw "Workflow Build VM report not found in VCF Orchestrator Guide/Chapter 7";}
var props = new Properties();// Inputs of the workflow go here, by name. Build VM report has none.props.put("__taskName", "VM report once at " + startAt.toString());props.put("__taskDescription", "One-off run scheduled by scheduleReportAt");
var task = wf.schedule(props, startAt);System.log("Scheduled " + task.name + " for " + startAt);return task;- Run it with
startAtset to ten minutes from now. The schedule appears in Activity > Scheduled with the name from__taskName. Ten minutes later it runs once and doesn’t run again.
Checked on 9.1.1: Server has no getWorkflowsWithName, so the action finds the workflow through its folder, with Server.getWorkflowCategoryWithPath and the folder’s workflows. wf.schedule(properties, date) returns a Task in the state pending, named after __taskName, and task.cancel() removes it again: it no longer appears in GET /vco/api/tasks.
__taskDescription shows on the schedule in Activity > Scheduled (the Task object returned undefined for description), the Starting user of a schedule made from a script, and Workflow.scheduleRecurrently.Manage schedules through the REST API
- List the schedules on the server:
. vro-env.shvro GET '/tasks' | jq '.relations.link[] | select(.rel == "down") | [(.attributes[] | select(.name=="name") | .value), .href]'- A pipeline that deploys a new version of the report workflow can remove the old schedule and create a new one. Delete a schedule by its ID:
TASK_ID="<ID from the href>"vro DELETE "/tasks/$TASK_ID"Checked on 9.1.1: GET /vco/api/tasks answers with relations.link, not a top-level link. Each schedule is an entry with rel = down, its href, and the attributes name, state, description, recurrenceCycle (such as one-time), recurrenceStartDate, workflowName and workflowHref. The Task Service also has GET /tasks/{id}/executions for a schedule’s runs, POST /tasks to create one, POST /tasks/{id} to update one, and GET /workflows/{workflowId}/tasks for the schedules of one workflow. A new schedule needs at least workflow (with its id), recurrence-start-date, and recurrence-cycle: one-time, every-minutes, every-hours, every-days, every-weeks or every-months.
POST /vco/api/tasks on 9.1.1 and record a body that works, including recurrence-pattern and the input parameters.How it works
A schedule is a task. In the API and in scripts, a schedule is a Task: a workflow, a set of input values, a start time, a recurrence, and a starting user. Orchestrator’s scheduler checks its tasks and starts a normal workflow run when one is due. The run is like any other: it shows in Activity > Workflow Runs, it has a log and variables, and it counts against the server’s limit of concurrent runs.
Start if in the past. A schedule’s start time can be in the past when you save it, or pass while the server is down. Yes runs the workflow at once in that case. No waits for the next time in the recurrence. Choose No for reports, so that a server restart at 09:00 doesn’t send the morning report twice. Choose Yes for work that must happen once, such as a change that was due during the outage.
Whose identity the run uses. A scheduled run happens when nobody is logged in, so Orchestrator keeps a way to authenticate as the Starting user:
| Starting user | The run authenticates as | Watch out for |
|---|---|---|
| The current user | You, with credentials Orchestrator stored when you saved | Your account changing: a new password, leaving the team, a disabled account |
| The Orchestrator system user | Orchestrator’s own service identity | Runs that aren’t traceable to a person, and rights that may differ from yours |
| An API token | The account that owns the token | The token expiring or being revoked |
Pick the one that will still be valid in a year. For production jobs, an API token that belongs to a service account, such as svc-vcfguide@vsphere.local on a standalone appliance or a service account in VCF Automation, survives people changing jobs.
When schedules stop. A schedule whose credentials stop working doesn’t fail loudly. It just stops starting runs, or its runs fail at the first step. Broadcom’s 9.0 known issues list two cases. On a standalone Orchestrator that uses VCF SSO authentication, scheduled workflows stopped running some 30 to 60 minutes after the user who scheduled them stopped using the client. On the Orchestrator embedded in VCF Automation, scheduling with the service account or an API token failed with Can not login in VMware Cloud Foundation Automation with username and password. Broadcom KB 445644 has a workaround for the embedded case and says it’s fixed in VCF Automation 9.1.1, after which an API token for scheduling takes the form {apiToken}@orgName:{tenantName}.
auto.vmw.lab (9.1.1 or later carries the KB 445644 fix), and whether a schedule on vco01 with VCF SSO authentication (recipe 1.6) keeps running overnight on 9.1.1. Schedule Build VM report on the embedded Orchestrator with each Starting user choice and record which ones work.There's more
Schedule or policy? Use a schedule for “run this workflow at these times”. Use a policy (recipe 7.3) when you need several handlers that share variables, start and stop hooks, or events from a plug-in. A schedule has nothing to keep running, so there’s less to go wrong after a restart.
Inputs that change. A schedule stores its input values when you save it. If a value should be worked out at run time, such as yesterday’s date, don’t pass it as an input. Let the workflow calculate it, or read it from a configuration element (recipe 4.7).
Don’t let runs pile up. If a run takes longer than the interval, the next one starts anyway, and two runs work on the same thing. Choose an interval longer than the workflow takes, or protect the work with a lock (recipe 7.5) and make the second run give up instead of waiting.
Tags as the job list. Combine this recipe with recipe 7.4: a nightly schedule runs a workflow that calls findVmsByTag("vcfguide-backup") and acts on each VM it returns. Adding a VM to the job is a tag, not a change to the schedule.
Watch your schedules. Because a broken schedule fails quietly, check Activity > Scheduled after an upgrade, a certificate change, or a change to the authentication provider. A small daily workflow that compares each schedule’s last run with its recurrence, through the Task Service, catches the ones that stopped.
See also
- 3.8 Asynchronous and nested workflows, for the Schedule workflow element on the canvas.
- 7.3 Policies and event triggers, for work at intervals with more than one handler.
- 7.5 Locks, to keep overlapping runs apart.
- Broadcom TechDocs (VCF 9.1): Schedule workflows in VCF Operations orchestrator, and Edit scheduled task.
- Broadcom TechDocs: VCF Automation 9.0 known issues, for the scheduling issues described in How it works.
- Broadcom KB 445644: scheduling with an API token on the embedded Orchestrator fails.
- Broadcom developer portal: VCF Operations orchestrator API, Task Service.
- CloudBlogger, the author’s blog: Solving Scheduled Workflow Failures from Service Broker.
