-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathWorkerSpecificActivities.cs
52 lines (47 loc) · 1.76 KB
/
WorkerSpecificActivities.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
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Logging;
using Temporalio.Activities;
namespace TemporalioSamples.WorkerSpecificTaskQueues;
public static class WorkerSpecificActivities
{
[Activity]
#pragma warning disable CA1054
public static async Task<string> DownloadFileToWorkerFileSystemAsync(string url)
#pragma warning restore CA1054
{
var path = Path.GetTempFileName();
ActivityExecutionContext.Current.Logger.LogInformation("Downloading {Url} and saving to path {Path}", url, path);
// Here's were the real download code goes.
var body = Encoding.UTF8.GetBytes("downloaded body");
await Task.Delay(TimeSpan.FromSeconds(3));
await File.WriteAllBytesAsync(path, body);
return path;
}
[Activity]
public static async Task WorkOnFileInWorkerFileSystemAsync(string path)
{
var content = await File.ReadAllBytesAsync(path);
var checksum = ComputeSha256Hash(content);
await Task.Delay(TimeSpan.FromSeconds(3));
ActivityExecutionContext.Current.Logger.LogInformation("Did some work on {Path}, checksum: {Checksum}", path, checksum);
}
[Activity]
public static async Task CleanupFileFromWorkerFileSystemAsync(string path)
{
await Task.Delay(TimeSpan.FromSeconds(3));
ActivityExecutionContext.Current.Logger.LogInformation("Removing {Path}", path);
File.Delete(path);
}
private static string ComputeSha256Hash(byte[] input)
{
var hash = SHA256.HashData(input);
var sb = new StringBuilder();
foreach (var c in hash)
{
sb.Append(c.ToString("x2", CultureInfo.InvariantCulture));
}
return sb.ToString();
}
}