/// <summary>
        /// Attempts to load the workflow if possible.
        /// </summary>
        /// <returns></returns>
        async Task LoadWorkflow()
        {
            // might already be loaded somehow
            if (workflow == null)
            {
                // load state from actor
                await state.LoadAsync();

                // only continue if not already completed
                if (state.InstanceState != InstanceState.Completed)
                {
                    // generate owner ID
                    if (state.InstanceOwnerId == Guid.Empty)
                    {
                        state.InstanceOwnerId = Guid.NewGuid();
                    }

                    if (state.InstanceId == Guid.Empty)
                    {
                        // clear existing data
                        state.ClearInstanceData();
                        state.ClearInstanceMetadata();

                        // create workflow application
                        await CreateWorkflow(actor.CreateActivity(), actor.CreateActivityInputs() ?? new Dictionary <string, object>());

                        state.InstanceId = workflow.Id;
                        await InvokeAndPump(workflow.RunAsync);

                        await state.SaveAsync();
                    }
                    else
                    {
                        // create workflow application
                        await CreateWorkflow(actor.CreateActivity());
                        await InvokeAndPump(async() => await workflow.LoadAsync(state.InstanceId));
                        await InvokeAndPump(workflow.RunAsync);

                        await state.SaveAsync();
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Handles a <see cref="SaveWorkflowCommand"/>.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="command"></param>
        Task <bool> SaveWorkflowCommand(InstancePersistenceContext context, SaveWorkflowCommand command)
        {
            // save the instance data
            state.InstanceState = InstanceState.Initialized;
            SaveInstanceData(context.InstanceView.InstanceId, command.InstanceData);
            SaveInstanceMetadata(context.InstanceView.InstanceId, command.InstanceMetadataChanges);
            state.OnPersisted();

            // clear instance data when complete
            if (command.CompleteInstance)
            {
                state.InstanceState = InstanceState.Completed;
                state.ClearInstanceData();
                state.ClearInstanceMetadata();
                state.OnCompleted();
            }

            return(Task.FromResult(true));
        }