static async Task Main(string[] args)
        {
            var serviceProvider = BuildServiceProvider();

            long wfId;

            // setup
            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <OracleContext>();

                context.Database.EnsureCreated();

                var wf = new PersistedWorkflow()
                {
                    CreateTime           = DateTime.Now,
                    Data                 = "{\"$type\":\"WorkflowCore.Tests.Oracle.Scenarios.SimpleEventScenario + MyDataClass, WorkflowCore.Tests.Oracle\",\"StrValue1\":\"1b98c27f-f33c-48b3-bffa-a9f758f1e74c\",\"StrValue2\":\"0be48870-6741-467a-a3e1-f7becfc6e681\"}",
                    InstanceId           = Guid.NewGuid(),
                    NextExecution        = 0,
                    Status               = 0,
                    Version              = 1,
                    WorkflowDefinitionId = "EventWorkflow"
                };

                var predecessor = new PersistedExecutionPointer()
                {
                    Id        = Guid.NewGuid().ToString(),
                    StartTime = DateTime.Now,
                    EndTime   = DateTime.Now,
                    StepId    = 0,
                    Active    = false,
                    Status    = PointerStatus.Complete
                };
                wf.ExecutionPointers.Add(predecessor);

                wf.ExecutionPointers.Add(new PersistedExecutionPointer()
                {
                    Id             = Guid.NewGuid().ToString(),
                    Active         = true,
                    StartTime      = DateTime.Now,
                    EventKey       = "1b98c27f-f33c-48b3-bffa-a9f758f1e74c",
                    EventName      = "MyEvent",
                    EventPublished = true,
                    PredecessorId  = predecessor.Id,
                    StepId         = 1,
                    Status         = PointerStatus.WaitingForEvent
                });

                context.Set <PersistedWorkflow>().Add(wf);
                await context.SaveChangesAsync();

                wfId = wf.PersistenceId;
            }

            using (var scope = serviceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <OracleContext>();

                var wf = await context.Set <PersistedWorkflow>()
                         .Include(x => x.ExecutionPointers)
                         .FirstAsync(w => w.PersistenceId == wfId);

                wf.Data = "{\"$type\":\"WorkflowCore.Tests.Oracle.Scenarios.SimpleEventScenario+MyDataClass, WorkflowCore.Tests.Oracle\",\"StrValue1\":\"1b98c27f-f33c-48b3-bffa-a9f758f1e74c\",\"StrValue2\":\"0be48870-6741-467a-a3e1-f7becfc6e681\"}";

                var step0 = wf.ExecutionPointers.Single(e => e.StepId == 0);
                step0.Children = string.Empty;
                step0.Scope    = string.Empty;

                var step1 = wf.ExecutionPointers.Single(e => e.StepId == 1);
                step1.Active   = false;
                step1.Children = string.Empty;
                step1.Scope    = string.Empty;
                step1.EndTime  = DateTime.Now;

                wf.ExecutionPointers.Add(new PersistedExecutionPointer()
                {
                    Active          = true,
                    Children        = string.Empty,
                    ContextItem     = "null",
                    EndTime         = null,
                    EventData       = "null",
                    EventKey        = string.Empty,
                    EventName       = string.Empty,
                    EventPublished  = false,
                    Id              = Guid.NewGuid().ToString(),
                    Outcome         = "null",
                    PersistenceData = "null",
                    PredecessorId   = step1.Id,
                    RetryCount      = 0,
                    Scope           = string.Empty,
                    SleepUntil      = null,
                    StartTime       = DateTime.Now,
                    Status          = PointerStatus.Pending,
                    StepId          = 2,
                    StepName        = null,
                });

                await context.SaveChangesAsync();
            }
        }
        internal static PersistedWorkflow ToPersistable(this WorkflowInstance instance, PersistedWorkflow persistable = null)
        {
            if (persistable == null)
            {
                persistable = new PersistedWorkflow();
            }

            persistable.Data                 = JsonConvert.SerializeObject(instance.Data, SerializerSettings);
            persistable.Description          = instance.Description;
            persistable.Reference            = instance.Reference;
            persistable.InstanceId           = new Guid(instance.Id);
            persistable.NextExecution        = instance.NextExecution;
            persistable.Version              = instance.Version;
            persistable.WorkflowDefinitionId = instance.WorkflowDefinitionId;
            persistable.Status               = instance.Status;
            persistable.CreateTime           = instance.CreateTime;
            persistable.CompleteTime         = instance.CompleteTime;

            foreach (var ep in instance.ExecutionPointers)
            {
                var persistedEP = persistable.ExecutionPointers.FindById(ep.Id);

                if (persistedEP == null)
                {
                    persistedEP    = new PersistedExecutionPointer();
                    persistedEP.Id = ep.Id ?? Guid.NewGuid().ToString();
                    persistable.ExecutionPointers.Add(persistedEP);
                }

                persistedEP.StepId          = ep.StepId;
                persistedEP.Active          = ep.Active;
                persistedEP.SleepUntil      = ep.SleepUntil;
                persistedEP.PersistenceData = JsonConvert.SerializeObject(ep.PersistenceData, SerializerSettings);
                persistedEP.StartTime       = ep.StartTime;
                persistedEP.EndTime         = ep.EndTime;
                persistedEP.StepName        = ep.StepName;
                persistedEP.RetryCount      = ep.RetryCount;
                persistedEP.PredecessorId   = ep.PredecessorId;
                persistedEP.ContextItem     = JsonConvert.SerializeObject(ep.ContextItem, SerializerSettings);
                persistedEP.Children        = string.Empty;

                foreach (var child in ep.Children)
                {
                    persistedEP.Children += child + ";";
                }

                persistedEP.EventName      = ep.EventName;
                persistedEP.EventKey       = ep.EventKey;
                persistedEP.EventPublished = ep.EventPublished;
                persistedEP.EventData      = JsonConvert.SerializeObject(ep.EventData, SerializerSettings);
                persistedEP.Outcome        = JsonConvert.SerializeObject(ep.Outcome, SerializerSettings);
                persistedEP.Status         = ep.Status;

                persistedEP.Scope = string.Empty;
                foreach (var item in ep.Scope)
                {
                    persistedEP.Scope += item + ";";
                }

                foreach (var attr in ep.ExtensionAttributes)
                {
                    var persistedAttr = persistedEP.ExtensionAttributes.FirstOrDefault(x => x.AttributeKey == attr.Key);
                    if (persistedAttr == null)
                    {
                        persistedAttr = new PersistedExtensionAttribute();
                        persistedEP.ExtensionAttributes.Add(persistedAttr);
                    }

                    persistedAttr.AttributeKey   = attr.Key;
                    persistedAttr.AttributeValue = JsonConvert.SerializeObject(attr.Value, SerializerSettings);
                }
            }

            return(persistable);
        }
Exemple #3
0
        internal static PersistedWorkflow ToPersistable(this WorkflowInstance instance, PersistedWorkflow persistable = null)
        {
            if (persistable == null)
            {
                persistable = new PersistedWorkflow();
            }

            persistable.Data                 = JsonConvert.SerializeObject(instance.Data, SerializerSettings);
            persistable.Description          = instance.Description;
            persistable.InstanceId           = new Guid(instance.Id);
            persistable.NextExecution        = instance.NextExecution;
            persistable.Version              = instance.Version;
            persistable.WorkflowDefinitionId = instance.WorkflowDefinitionId;
            persistable.Status               = instance.Status;
            persistable.CreateTime           = instance.CreateTime;
            persistable.CompleteTime         = instance.CompleteTime;

            foreach (var ep in instance.ExecutionPointers)
            {
                var persistedEP = persistable.ExecutionPointers.FirstOrDefault(x => x.Id == ep.Id);

                if (persistedEP == null)
                {
                    persistedEP = new PersistedExecutionPointer();
                    persistable.ExecutionPointers.Add(persistedEP);
                }

                persistedEP.Id              = ep.Id ?? Guid.NewGuid().ToString();
                persistedEP.StepId          = ep.StepId;
                persistedEP.Active          = ep.Active;
                persistedEP.SleepUntil      = ep.SleepUntil;
                persistedEP.PersistenceData = JsonConvert.SerializeObject(ep.PersistenceData, SerializerSettings);
                persistedEP.StartTime       = ep.StartTime;
                persistedEP.EndTime         = ep.EndTime;
                persistedEP.StepName        = ep.StepName;
                persistedEP.EventName       = ep.EventName;
                persistedEP.EventKey        = ep.EventKey;
                persistedEP.EventPublished  = ep.EventPublished;
                persistedEP.ConcurrentFork  = ep.ConcurrentFork;
                persistedEP.PathTerminator  = ep.PathTerminator;
                persistedEP.EventData       = JsonConvert.SerializeObject(ep.EventData, SerializerSettings);

                foreach (var attr in ep.ExtensionAttributes)
                {
                    var persistedAttr = persistedEP.ExtensionAttributes.FirstOrDefault(x => x.AttributeKey == attr.Key);
                    if (persistedAttr == null)
                    {
                        persistedAttr = new PersistedExtensionAttribute();
                        persistedEP.ExtensionAttributes.Add(persistedAttr);
                    }

                    persistedAttr.AttributeKey   = attr.Key;
                    persistedAttr.AttributeValue = JsonConvert.SerializeObject(attr.Value, SerializerSettings);
                }

                foreach (var err in ep.Errors)
                {
                    var persistedErr = persistedEP.Errors.FirstOrDefault(x => x.Id == err.Id);
                    if (persistedErr == null)
                    {
                        persistedErr           = new PersistedExecutionError();
                        persistedErr.Id        = err.Id ?? Guid.NewGuid().ToString();
                        persistedErr.ErrorTime = err.ErrorTime;
                        persistedErr.Message   = err.Message;
                        persistedEP.Errors.Add(persistedErr);
                    }
                }
            }

            return(persistable);
        }