Skip to content

Instantly share code, notes, and snippets.

@sfmskywalker
Last active August 3, 2021 08:43
Show Gist options
  • Save sfmskywalker/ab8abe1021e6632b5bcff99116ad6e23 to your computer and use it in GitHub Desktop.
Save sfmskywalker/ab8abe1021e6632b5bcff99116ad6e23 to your computer and use it in GitHub Desktop.

Revisions

  1. sfmskywalker revised this gist Aug 3, 2021. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions StartDocumentWorkflows.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    using System.Threading;
    using System.Threading.Tasks;
    using DocumentManagement.Core.Events;
    using Elsa.Models;
    using Elsa.Services;
    using MediatR;
    using Open.Linq.AsyncExtensions;

    namespace DocumentManagement.Workflows.Handlers
    {
    /// <summary>
    /// Handles the <see cref="NewDocumentReceived"/> event by starting new workflows associated with the document type.
    /// </summary>
    public class StartDocumentWorkflows : INotificationHandler<NewDocumentReceived>
    {
    private readonly IWorkflowRegistry _workflowRegistry;
    private readonly IWorkflowDefinitionDispatcher _workflowDispatcher;

    public StartDocumentWorkflows(IWorkflowRegistry workflowRegistry, IWorkflowDefinitionDispatcher workflowDispatcher)
    {
    _workflowRegistry = workflowRegistry;
    _workflowDispatcher = workflowDispatcher;
    }

    public async Task Handle(NewDocumentReceived notification, CancellationToken cancellationToken)
    {
    var document = notification.Document;
    var documentTypeId = document.DocumentTypeId;

    // Get all workflow blueprints tagged with the received document type ID.
    var workflowBlueprints = await _workflowRegistry.FindManyAsync(x => x.IsPublished && x.Tag == documentTypeId, cancellationToken).ToList();

    // Dispatch each workflow. Each workflow will be correlated by Document ID.
    foreach (var workflowBlueprint in workflowBlueprints)
    await _workflowDispatcher.DispatchAsync(new ExecuteWorkflowDefinitionRequest(workflowBlueprint.Id, CorrelationId: document.Id, Input: new WorkflowInput(document.Id)), cancellationToken);
    }
    }
    }
  2. sfmskywalker revised this gist Aug 3, 2021. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions StartHelloFileWorkflow.cs
    Original file line number Diff line number Diff line change
    @@ -20,14 +20,13 @@ public StartHelloFileWorkflow(IWorkflowRegistry workflowRegistry, IWorkflowDefin
    _workflowRegistry = workflowRegistry;
    _workflowDispatcher = workflowDispatcher;
    }

    public async Task Handle(NewDocumentReceived notification, CancellationToken cancellationToken)
    {
    var document = notification.Document;

    // Get our HelloFile workflow.
    var workflow =
    await _workflowRegistry.GetAsync("HelloFile", default, VersionOptions.Published, cancellationToken);
    var workflow = await _workflowRegistry.FindAsync(x => x.Name == "HelloFile" && x.IsPublished, cancellationToken);

    if (workflow == null)
    return; // Do nothing.
  3. sfmskywalker revised this gist Aug 3, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion StartHelloFileWorkflow.cs
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    using Elsa.Models;
    using Elsa.Services;
    using MediatR;
    using Open.Linq.AsyncExtensions;

    namespace DocumentManagement.Workflows.Handlers
    {
    @@ -30,6 +29,9 @@ public async Task Handle(NewDocumentReceived notification, CancellationToken can
    var workflow =
    await _workflowRegistry.GetAsync("HelloFile", default, VersionOptions.Published, cancellationToken);

    if (workflow == null)
    return; // Do nothing.

    // Dispatch the workflow.
    await _workflowDispatcher.DispatchAsync(new ExecuteWorkflowDefinitionRequest(workflow!.Id, Input: new WorkflowInput(document.Id)), cancellationToken);
    }
  4. sfmskywalker revised this gist Aug 3, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion StartHelloFileWorkflow.cs
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,6 @@
    using MediatR;
    using Open.Linq.AsyncExtensions;


    namespace DocumentManagement.Workflows.Handlers
    {
    /// <summary>
  5. sfmskywalker created this gist Aug 3, 2021.
    38 changes: 38 additions & 0 deletions StartHelloFileWorkflow.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    using System.Threading;
    using System.Threading.Tasks;
    using DocumentManagement.Core.Events;
    using Elsa.Models;
    using Elsa.Services;
    using MediatR;
    using Open.Linq.AsyncExtensions;


    namespace DocumentManagement.Workflows.Handlers
    {
    /// <summary>
    /// Handles the <see cref="NewDocumentReceived"/> event by starting the HelloFile workflow.
    /// </summary>
    public class StartHelloFileWorkflow : INotificationHandler<NewDocumentReceived>
    {
    private readonly IWorkflowRegistry _workflowRegistry;
    private readonly IWorkflowDefinitionDispatcher _workflowDispatcher;

    public StartHelloFileWorkflow(IWorkflowRegistry workflowRegistry, IWorkflowDefinitionDispatcher workflowDispatcher)
    {
    _workflowRegistry = workflowRegistry;
    _workflowDispatcher = workflowDispatcher;
    }

    public async Task Handle(NewDocumentReceived notification, CancellationToken cancellationToken)
    {
    var document = notification.Document;

    // Get our HelloFile workflow.
    var workflow =
    await _workflowRegistry.GetAsync("HelloFile", default, VersionOptions.Published, cancellationToken);

    // Dispatch the workflow.
    await _workflowDispatcher.DispatchAsync(new ExecuteWorkflowDefinitionRequest(workflow!.Id, Input: new WorkflowInput(document.Id)), cancellationToken);
    }
    }
    }