-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Chad Retz <[email protected]>
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace TemporalioSamples.Timer; | ||
|
||
using Temporalio.Activities; | ||
|
||
public class MyActivities | ||
{ | ||
[Activity] | ||
public static string Charge(string userId) => "charge successful"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Temporalio.Client; | ||
using Temporalio.Worker; | ||
using TemporalioSamples.Timer; | ||
|
||
// 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; | ||
}; | ||
|
||
// Create an activity instance with some state | ||
var activities = new MyActivities(); | ||
|
||
// Run worker until cancelled | ||
Console.WriteLine("Running worker"); | ||
using var worker = new TemporalWorker( | ||
client, | ||
new TemporalWorkerOptions(taskQueue: "timer-sample"). | ||
AddActivity(MyActivities.Charge). | ||
AddWorkflow<Subscription>()); | ||
try | ||
{ | ||
await worker.ExecuteAsync(tokenSource.Token); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
Console.WriteLine("Worker cancelled"); | ||
} | ||
} | ||
|
||
async Task ExecuteWorkflowAsync() | ||
{ | ||
Console.WriteLine("Executing workflow"); | ||
await client.ExecuteWorkflowAsync( | ||
(Subscription wf) => wf.RunAsync("user-id-123"), | ||
new(id: "timer-workflow-id", taskQueue: "timer-sample")); | ||
} | ||
|
||
switch (args.ElementAtOrDefault(0)) | ||
{ | ||
case "worker": | ||
await RunWorkerAsync(); | ||
break; | ||
case "workflow": | ||
await ExecuteWorkflowAsync(); | ||
break; | ||
default: | ||
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Timer | ||
|
||
Use a timer (`Workflow.DelayAsync`) to implement a monthly subscription. Also, handle workflow cancellation. | ||
|
||
To run, first see [README.md](../../README.md) for prerequisites. Then, run the following from this directory | ||
in a separate terminal to start the worker: | ||
|
||
dotnet run worker | ||
|
||
Then in another terminal, run the workflow from this directory: | ||
|
||
dotnet run workflow | ||
|
||
The worker terminal will show logs from running the workflow. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace TemporalioSamples.Timer; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Temporalio.Workflows; | ||
|
||
[Workflow] | ||
public class Subscription | ||
{ | ||
[WorkflowRun] | ||
public async Task RunAsync(string userId) | ||
{ | ||
try | ||
{ | ||
while (true) | ||
{ | ||
await Workflow.DelayAsync(TimeSpan.FromDays(30)); | ||
|
||
var result = await Workflow.ExecuteActivityAsync( | ||
() => MyActivities.Charge(userId), | ||
new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) }); | ||
Workflow.Logger.LogInformation("Activity result: {Result}", result); | ||
} | ||
} | ||
catch (Exception e) when (TemporalException.IsCanceledException(e)) | ||
{ | ||
Workflow.Logger.LogInformation("Workflow cancelled, cleaning up..."); | ||
// Handle any cleanup here | ||
// Re-throw to close the workflow as Cancelled. Otherwise, it will be closed as Completed. | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
</Project> |