Skip to content

Commit

Permalink
Update to use new Semaphore and change name to "Safe"
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz committed Jun 27, 2024
1 parent be233c9 commit a466055
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Temporalio" Version="1.1.2" />
<PackageReference Include="Temporalio.Extensions.DiagnosticSource" Version="1.1.2" />
<PackageReference Include="Temporalio.Extensions.Hosting" Version="1.1.2" />
<PackageReference Include="Temporalio.Extensions.OpenTelemetry" Version="1.1.2" />
<PackageReference Include="Temporalio" Version="1.2.0" />
<PackageReference Include="Temporalio.Extensions.DiagnosticSource" Version="1.2.0" />
<PackageReference Include="Temporalio.Extensions.Hosting" Version="1.2.0" />
<PackageReference Include="Temporalio.Extensions.OpenTelemetry" Version="1.2.0" />
<!--
Can also reference the SDK downloaded to a local directory:
<ProjectReference Include="$(MSBuildThisFileDirectory)..\temporal-sdk-dotnet\src\Temporalio\Temporalio.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Prerequisites:
* [ActivityHeartbeatingCancellation](src/ActivityHeartbeatingCancellation) - How to use heartbeating and cancellation handling in an activity.
* [ActivitySimple](src/ActivitySimple) - Simple workflow that runs simple activities.
* [ActivityWorker](src/ActivityWorker) - Use .NET activities from a workflow in another language.
* [AtomicMessageHandlers](src/AtomicMessageHandlers) - Use `SemaphoreSlim` to ensure operations are atomically processed in a workflow.
* [AspNet](src/AspNet) - Demonstration of a generic host worker and an ASP.NET workflow starter.
* [ClientMtls](src/ClientMtls) - How to use client certificate authentication, e.g. for Temporal Cloud.
* [ContextPropagation](src/ContextPropagation) - Context propagation via interceptors.
* [DependencyInjection](src/DependencyInjection) - How to inject dependencies in activities and use generic hosts for workers
* [Encryption](src/Encryption) - End-to-end encryption with Temporal payload codecs.
* [Mutex](src/Mutex) - How to implement a mutex as a workflow. Demonstrates how to avoid race conditions or parallel mutually exclusive operations on the same resource.
* [Polling](src/Polling) - Recommended implementation of an activity that needs to periodically poll an external resource waiting its successful completion.
* [SafeMessageHandlers](src/SafeMessageHandlers) - Use `Semaphore` to ensure operations are atomically processed in a workflow.
* [Saga](src/Saga) - Demonstrates how to implement a saga pattern.
* [Schedules](src/Schedules) - How to schedule workflows to be run at specific times in the future.
* [SignalsQueries](src/SignalsQueries) - A loyalty program using Signals and Queries.
Expand Down
12 changes: 6 additions & 6 deletions TemporalioSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalioSamples.WorkflowU
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalioSamples.ContextPropagation", "src\ContextPropagation\TemporalioSamples.ContextPropagation.csproj", "{7B797D20-485F-441D-8E71-AF7E315FA9CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalioSamples.AtomicMessageHandlers", "src\AtomicMessageHandlers\TemporalioSamples.AtomicMessageHandlers.csproj", "{5E497499-F87C-4DC6-B5DC-F508F31EA172}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalioSamples.SafeMessageHandlers", "src\SafeMessageHandlers\TemporalioSamples.SafeMessageHandlers.csproj", "{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -151,10 +151,10 @@ Global
{7B797D20-485F-441D-8E71-AF7E315FA9CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B797D20-485F-441D-8E71-AF7E315FA9CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B797D20-485F-441D-8E71-AF7E315FA9CF}.Release|Any CPU.Build.0 = Release|Any CPU
{5E497499-F87C-4DC6-B5DC-F508F31EA172}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E497499-F87C-4DC6-B5DC-F508F31EA172}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E497499-F87C-4DC6-B5DC-F508F31EA172}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E497499-F87C-4DC6-B5DC-F508F31EA172}.Release|Any CPU.Build.0 = Release|Any CPU
{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -184,6 +184,6 @@ Global
{B79F07F7-3429-4C58-84C3-08587F748B2D} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{7B797D20-485F-441D-8E71-AF7E315FA9CF} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{5E497499-F87C-4DC6-B5DC-F508F31EA172} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{FAF5984A-5B1C-4686-B056-A4F2AC0E8EB7} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/AspNet/Worker/TemporalioSamples.AspNet.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<UserSecretsId>dotnet-AspNet-Worker-a95eda12-24b8-4044-81a5-4d4d3f959742</UserSecretsId>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TemporalioSamples.AtomicMessageHandlers;
namespace TemporalioSamples.SafeMessageHandlers;

using Microsoft.Extensions.Logging;
using Temporalio.Activities;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace TemporalioSamples.AtomicMessageHandlers;
namespace TemporalioSamples.SafeMessageHandlers;

using Microsoft.Extensions.Logging;
using Temporalio.Exceptions;
using Temporalio.Workflows;

[Workflow]
public sealed class ClusterManagerWorkflow : IDisposable
public class ClusterManagerWorkflow
{
public record State
{
Expand All @@ -29,7 +29,7 @@ public record Result(
int MaxAssignedNodes,
int NumAssignedNodes);

private readonly SemaphoreSlim nodesLock = new(1, 1);
private readonly Semaphore nodesLock = new(1);
private readonly int maxHistoryLength;
private readonly TimeSpan sleepInterval;

Expand Down Expand Up @@ -167,8 +167,6 @@ await Workflow.ExecuteActivityAsync(
}
}

public void Dispose() => nodesLock.Dispose();

private int NumAssignedNodes =>
CurrentState.Nodes.Count(kvp => kvp.Value is { } val && val != "BAD!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Temporalio.Api.Enums.V1;
using Temporalio.Client;
using Temporalio.Worker;
using TemporalioSamples.AtomicMessageHandlers;
using TemporalioSamples.SafeMessageHandlers;

// Create a client to localhost on default namespace
using var loggerFactory = LoggerFactory.Create(builder =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Atomic Message Handlers
# Safe Message Handlers

This sample shows a workflow using `SemaphoreSlim` to atomically process certain blocks of workflow code to prevent data
race issues. The sample code demonstrates assigning cluster nodes to jobs atomically.
This sample shows a workflow using `Temporalio.Workflows.Semaphore` to atomically process certain blocks of workflow
code to prevent data race issues. The sample code demonstrates assigning cluster nodes to jobs atomically.

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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace TemporalioSamples.AtomicMessageHandlers;
namespace TemporalioSamples.Tests.SafeMessageHandlers;

using Temporalio.Client;
using Temporalio.Worker;
using TemporalioSamples.Tests;
using TemporalioSamples.SafeMessageHandlers;
using Xunit;
using Xunit.Abstractions;

Expand Down
2 changes: 1 addition & 1 deletion tests/TemporalioSamples.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
<ItemGroup>
<ProjectReference Include="..\src\ActivitySimple\TemporalioSamples.ActivitySimple.csproj" />
<ProjectReference Include="..\src\ActivityWorker\TemporalioSamples.ActivityWorker.csproj" />
<ProjectReference Include="..\src\AtomicMessageHandlers\TemporalioSamples.AtomicMessageHandlers.csproj" />
<ProjectReference Include="..\src\ContextPropagation\TemporalioSamples.ContextPropagation.csproj" />
<ProjectReference Include="..\src\Encryption\Codec\TemporalioSamples.Encryption.Codec.csproj" />
<ProjectReference Include="..\src\Encryption\CodecServer\TemporalioSamples.Encryption.CodecServer.csproj" />
<ProjectReference Include="..\src\SafeMessageHandlers\TemporalioSamples.SafeMessageHandlers.csproj" />
<ProjectReference Include="..\src\WorkflowUpdate\TemporalioSamples.WorkflowUpdate.csproj" />
</ItemGroup>

Expand Down

0 comments on commit a466055

Please sign in to comment.