-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathschedules.ts
More file actions
123 lines (105 loc) · 4.98 KB
/
schedules.ts
File metadata and controls
123 lines (105 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Schedules API Journey — Full lifecycle of workflow schedule management
*
* Demonstrates all SchedulerClient APIs:
* - Save, get, pause, resume, delete schedules
* - Search schedule executions
* - Get next execution times
* - Tag management for schedules
* - List all schedules
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/api-journeys/schedules.ts
*/
import {
OrkesClients,
ConductorWorkflow,
simpleTask,
} from "../../src/sdk";
async function main() {
const clients = await OrkesClients.from();
const scheduler = clients.getSchedulerClient();
const workflowClient = clients.getWorkflowClient();
const scheduleName = "journey_example_schedule";
const wfName = "schedule_journey_workflow";
try {
// Register a workflow for the schedule
const wf = new ConductorWorkflow(workflowClient, wfName)
.description("Workflow used by schedule journey")
.add(simpleTask("step_ref", "schedule_demo_task", {}))
.outputParameters({ result: "${step_ref.output}" });
await wf.register(true);
console.log("Registered workflow:", wfName);
// ── 1. Get next execution times ─────────────────────────────────
const nextTimes = await scheduler.getNextFewSchedules("0 */5 * * * ?", undefined, undefined, 5);
console.log(
"1. Next 5 execution times for '0 */5 * * * ?':",
nextTimes.map((t) => new Date(t).toISOString())
);
// ── 2. Save a schedule ──────────────────────────────────────────
await scheduler.saveSchedule({
name: scheduleName,
cronExpression: "0 */10 * * * ?", // Every 10 minutes
startWorkflowRequest: {
name: wfName,
version: 1,
},
paused: true, // Start paused so it doesn't trigger immediately
});
console.log("2. Saved schedule:", scheduleName);
// ── 3. Get schedule ─────────────────────────────────────────────
const schedule = await scheduler.getSchedule(scheduleName);
console.log("3. Schedule details:", JSON.stringify({
name: schedule.name,
cronExpression: schedule.cronExpression,
paused: schedule.paused,
}));
// ── 4. Resume schedule ──────────────────────────────────────────
await scheduler.resumeSchedule(scheduleName);
console.log("4. Resumed schedule");
// ── 5. Pause schedule ───────────────────────────────────────────
await scheduler.pauseSchedule(scheduleName);
console.log("5. Paused schedule");
// ── 6. List all schedules ───────────────────────────────────────
const allSchedules = await scheduler.getAllSchedules();
console.log("6. Total schedules:", allSchedules.length);
// ── 7. List schedules for workflow ───────────────────────────────
const wfSchedules = await scheduler.getAllSchedules(wfName);
console.log("7. Schedules for workflow:", wfSchedules.length);
// ── 8. Search schedule executions ───────────────────────────────
const searchResult = await scheduler.search(0, 10);
console.log("8. Schedule executions found:", searchResult.totalHits ?? 0);
// ── 9. Tag management ───────────────────────────────────────────
await scheduler.setSchedulerTags(
[
{ key: "env", value: "staging" },
{ key: "team", value: "platform" },
],
scheduleName
);
console.log("9. Set schedule tags");
const tags = await scheduler.getSchedulerTags(scheduleName);
console.log("10. Schedule tags:", JSON.stringify(tags));
await scheduler.deleteSchedulerTags(
[{ key: "env", value: "staging" }],
scheduleName
);
console.log("11. Deleted schedule tag");
// ── 10. Cleanup ─────────────────────────────────────────────────
await scheduler.deleteSchedule(scheduleName);
console.log("12. Deleted schedule");
const metadataClient = clients.getMetadataClient();
await metadataClient.unregisterWorkflow(wfName);
console.log("13. Cleaned up workflow definition");
} catch (err) {
console.error("Error:", err);
// Best-effort cleanup
try { await scheduler.deleteSchedule(scheduleName); } catch { /* ignore */ }
}
console.log("\nDone.");
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});