-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathProgram.cs
152 lines (133 loc) · 4.1 KB
/
Program.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using Microsoft.Extensions.Logging;
using Temporalio.Api.Enums.V1;
using Temporalio.Client;
using Temporalio.Client.Schedules;
using Temporalio.Worker;
using TemporalioSamples.Schedules;
// Create a client to localhost on default namespace
var client = await TemporalClient.ConnectAsync(new("localhost:7233")
{
LoggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information)),
});
async Task RunWorkerAsync()
{
// Cancellation token cancelled on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};
// Run worker until cancelled
Console.WriteLine("Running worker");
using var worker = new TemporalWorker(
client,
new TemporalWorkerOptions(taskQueue: "schedules").
AddActivity(MyActivities.NotifyUserAsync).
AddActivity(MyActivities.AddReminderToDatabase).
AddWorkflow<MyWorkflow>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker cancelled");
}
}
async Task ScheduleStartAsync()
{
Console.WriteLine("Scheduling workflow");
var text = "Dear future self, please take out the recycling tonight. Sincerely, past you";
var action = ScheduleActionStartWorkflow.Create<MyWorkflow>(
wf => wf.RunAsync(text),
new()
{
Id = "schedule-workflow-id",
TaskQueue = "schedules",
});
var spec = new ScheduleSpec
{
Intervals = new List<ScheduleIntervalSpec>
{
new(Every: TimeSpan.FromSeconds(10)),
},
};
var schedule = new Schedule(action, spec)
{
Policy = new()
{
CatchupWindow = TimeSpan.FromDays(1),
Overlap = ScheduleOverlapPolicy.AllowAll,
},
};
var scheduleHandle = await client.CreateScheduleAsync("sample-schedule", schedule);
Console.WriteLine(@$"Started schedule {scheduleHandle.Id}
The reminder Workflow will run and log from the Worker every 10 seconds.
dotnet run schedule-go-faster
dotnet run schedule-pause
dotnet run schedule-unpause
dotnet run schedule-delete
");
}
async Task ScheduleGoFasterAsync()
{
var handle = client.GetScheduleHandle("sample-schedule");
await handle.UpdateAsync(input =>
{
var spec = new ScheduleSpec
{
Intervals = new List<ScheduleIntervalSpec>
{
new(Every: TimeSpan.FromSeconds(5)),
},
};
var schedule = input.Description.Schedule with { Spec = spec };
return new ScheduleUpdate(schedule);
});
Console.WriteLine("Schedule is now triggered every 5 seconds.");
}
async Task SchedulePauseAsync()
{
var handle = client.GetScheduleHandle("sample-schedule");
await handle.PauseAsync();
Console.WriteLine("Schedule is now paused.");
}
async Task ScheduleUnpauseAsync()
{
var handle = client.GetScheduleHandle("sample-schedule");
await handle.UnpauseAsync();
Console.WriteLine("Schedule is now unpaused.");
}
async Task ScheduleDeleteAsync()
{
var handle = client.GetScheduleHandle("sample-schedule");
await handle.DeleteAsync();
Console.WriteLine("Schedule is now deleted.");
}
switch (args.ElementAtOrDefault(0))
{
case "worker":
await RunWorkerAsync();
break;
case "schedule-start":
await ScheduleStartAsync();
break;
case "schedule-go-faster":
await ScheduleGoFasterAsync();
break;
case "schedule-pause":
await SchedulePauseAsync();
break;
case "schedule-unpause":
await ScheduleUnpauseAsync();
break;
case "schedule-delete":
await ScheduleDeleteAsync();
break;
default:
throw new ArgumentException("Must pass 'worker', 'schedule-start', 'schedule-go-faster', 'schedule-pause', 'schedule-unpause' or 'schedule-delete' as the single argument");
}