diff --git a/AspNetCore/React/AdvancedSearch/Controllers/SampleDataController.cs b/AspNetCore/React/AdvancedSearch/Controllers/SampleDataController.cs index 0fb3546a..c34f8f85 100644 --- a/AspNetCore/React/AdvancedSearch/Controllers/SampleDataController.cs +++ b/AspNetCore/React/AdvancedSearch/Controllers/SampleDataController.cs @@ -1,43 +1,34 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Temporalio.Client; +using EqDemo.Workflows; namespace EqDemo.Controllers { [Route("api/[controller]")] public class SampleDataController : Controller { - private static string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; + private readonly ITemporalClient _temporalClient; - [HttpGet("[action]")] - public IEnumerable WeatherForecasts() + public SampleDataController(ITemporalClient temporalClient) { - var rng = new Random(); - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - DateFormatted = DateTime.Now.AddDays(index).ToString("d"), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }); + _temporalClient = temporalClient; } - public class WeatherForecast + [HttpGet("[action]")] + public async Task> WeatherForecasts() { - public string DateFormatted { get; set; } - public int TemperatureC { get; set; } - public string Summary { get; set; } + var handle = await _temporalClient.StartWorkflowAsync( + (WeatherWorkflow wf) => wf.RunAsync(), + new WorkflowOptions + { + Id = $"weather-forecast-{Guid.NewGuid()}", + TaskQueue = "advanced-search-task-queue" + }); - public int TemperatureF - { - get { - return 32 + (int)(TemperatureC / 0.5556); - } - } + return await handle.GetResultAsync(); } } } diff --git a/AspNetCore/React/AdvancedSearch/EqDemo.AspNetCoreReact.AdvancedSearch.csproj b/AspNetCore/React/AdvancedSearch/EqDemo.AspNetCoreReact.AdvancedSearch.csproj index 40bb9e61..7a2c797b 100644 --- a/AspNetCore/React/AdvancedSearch/EqDemo.AspNetCoreReact.AdvancedSearch.csproj +++ b/AspNetCore/React/AdvancedSearch/EqDemo.AspNetCoreReact.AdvancedSearch.csproj @@ -16,6 +16,8 @@ + + diff --git a/AspNetCore/React/AdvancedSearch/Startup.cs b/AspNetCore/React/AdvancedSearch/Startup.cs index 6ea66069..b3e8ab7d 100644 --- a/AspNetCore/React/AdvancedSearch/Startup.cs +++ b/AspNetCore/React/AdvancedSearch/Startup.cs @@ -10,6 +10,8 @@ using Korzh.EasyQuery.Services; using EasyData.Export; +using Temporalio.Extensions.Hosting; +using EqDemo.Workflows; namespace EqDemo { @@ -49,6 +51,15 @@ public void ConfigureServices(IServiceCollection services) configuration.RootPath = "ClientApp/build"; }); + services.AddTemporalClient(clientOptions => { + clientOptions.TargetHost = Configuration.GetValue("Temporal:TargetHost") ?? "localhost:7233"; + clientOptions.Namespace = Configuration.GetValue("Temporal:Namespace") ?? "default"; + }); + + services.AddHostedTemporalWorker("advanced-search-task-queue") + .AddScopedActivities() + .AddWorkflow(); + services.AddEasyQuery() .UseSqlManager() .AddDefaultExporters() diff --git a/AspNetCore/React/AdvancedSearch/Workflows/WeatherActivities.cs b/AspNetCore/React/AdvancedSearch/Workflows/WeatherActivities.cs new file mode 100644 index 00000000..3958b8da --- /dev/null +++ b/AspNetCore/React/AdvancedSearch/Workflows/WeatherActivities.cs @@ -0,0 +1,31 @@ +using Temporalio.Activities; + +namespace EqDemo.Workflows; + +public class WeatherActivities +{ + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + [Activity] + public List GetWeatherForecasts() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + DateFormatted = DateTime.Now.AddDays(index).ToString("d"), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }).ToList(); + } + + public class WeatherForecast + { + public string DateFormatted { get; set; } + public int TemperatureC { get; set; } + public string Summary { get; set; } + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + } +} diff --git a/AspNetCore/React/AdvancedSearch/Workflows/WeatherWorkflow.cs b/AspNetCore/React/AdvancedSearch/Workflows/WeatherWorkflow.cs new file mode 100644 index 00000000..78acdba0 --- /dev/null +++ b/AspNetCore/React/AdvancedSearch/Workflows/WeatherWorkflow.cs @@ -0,0 +1,16 @@ +using Temporalio.Workflows; + +namespace EqDemo.Workflows; + +[Workflow] +public class WeatherWorkflow +{ + [WorkflowRun] + public async Task> RunAsync() + { + return await Workflow.ExecuteActivityAsync( + (WeatherActivities act) => act.GetWeatherForecasts(), + new ActivityOptions { StartToCloseTimeout = TimeSpan.FromSeconds(30) } + ); + } +} diff --git a/AspNetCore/React/AdvancedSearch/docker-compose.yml b/AspNetCore/React/AdvancedSearch/docker-compose.yml new file mode 100644 index 00000000..23ce19c7 --- /dev/null +++ b/AspNetCore/React/AdvancedSearch/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3.8' +services: + temporal: + image: temporalio/auto-setup:latest + ports: + - "7233:7233" + temporal-ui: + image: temporalio/ui:latest + ports: + - "8080:8080" + environment: + - TEMPORAL_ADDRESS=temporal:7233