-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathFileProcessingWorkflow.workflow.cs
More file actions
80 lines (74 loc) · 2.91 KB
/
FileProcessingWorkflow.workflow.cs
File metadata and controls
80 lines (74 loc) · 2.91 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
using Microsoft.Extensions.Logging;
using Temporalio.Workflows;
namespace TemporalioSamples.WorkerSpecificTaskQueues;
[Workflow]
public class FileProcessingWorkflow
{
[WorkflowRun]
public async Task RunAsync(int maxAttempts)
{
// When using a worker-specific task queue, if a failure occurs, we want to retry all of the
// worker-specific logic, so wrap all the logic here in a loop.
var attempt = 0;
while (true)
{
attempt++;
try
{
await ProcessFileAsync();
return;
}
catch (Exception e)
{
// If it's at max attempts, re-throw to fail the workflow
if (attempt >= maxAttempts)
{
Workflow.Logger.LogError(
e,
"File processing failed and reached {Attempt} attempts, failing workflow",
attempt);
throw;
}
// Otherwise, just warn and continue
Workflow.Logger.LogWarning(
e,
"File processing failed on attempt {Attempt}, trying again",
attempt);
}
}
}
private async Task ProcessFileAsync()
{
var uniqueWorkerTaskQueue = await Workflow.ExecuteActivityAsync(
(NormalActivities act) => act.GetUniqueTaskQueue(),
new() { StartToCloseTimeout = TimeSpan.FromMinutes(1) });
var downloadPath = await Workflow.ExecuteActivityAsync(
() => WorkerSpecificActivities.DownloadFileToWorkerFileSystemAsync("https://temporal.io"),
new()
{
TaskQueue = uniqueWorkerTaskQueue,
// Note the use of ScheduleToCloseTimeout.
// The reason this timeout type is used is because this task queue is unique
// to a single worker. When that worker goes away, there won't be a way for these
// activities to progress.
ScheduleToCloseTimeout = TimeSpan.FromMinutes(5),
HeartbeatTimeout = TimeSpan.FromMinutes(1),
});
await Workflow.ExecuteActivityAsync(
() => WorkerSpecificActivities.WorkOnFileInWorkerFileSystemAsync(downloadPath),
new()
{
TaskQueue = uniqueWorkerTaskQueue,
ScheduleToCloseTimeout = TimeSpan.FromMinutes(5),
HeartbeatTimeout = TimeSpan.FromMinutes(1),
});
await Workflow.ExecuteActivityAsync(
() => WorkerSpecificActivities.CleanupFileFromWorkerFileSystemAsync(downloadPath),
new()
{
TaskQueue = uniqueWorkerTaskQueue,
ScheduleToCloseTimeout = TimeSpan.FromMinutes(5),
HeartbeatTimeout = TimeSpan.FromMinutes(1),
});
}
}