private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); if (!KeepInstanceDataAfterCompletion && ToDelete != null) { //DeleteFiles(context.InstanceView.InstanceId); ToDelete(); } } else { Dictionary <string, InstanceValue> instanceData = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData); Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command); try { //serialize_dc(instanceData, instanceMetadata); //string serializedCorrelation = WorkflowSerialization.Serialize<WorkflowCorrelation>(this.Correlation); string serializedInstanceData = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceData); string serializedInstanceMetadata = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceMetadata); if (ToPersist != null) { ToPersist(new Serialized() { SerializedInstanceData = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceData), SerializedInstanceMetadata = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceMetadata) }); } } catch (Exception exc) { throw exc; } context.PersistedInstance(command.InstanceData); if (command.CompleteInstance) { context.CompletedInstance(); } if (command.UnlockInstance || command.CompleteInstance) { context.InstanceHandle.Free(); } } return(true); }
private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); if (!KeepInstanceDataAfterCompletion) { DeleteFiles(context.InstanceView.InstanceId); } } else { Dictionary <string, InstanceValue> instanceData = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData); Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command); try { var serializedInstanceData = JsonConvert.SerializeObject(instanceData, Formatting.Indented, _jsonSerializerSettings); File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceData", serializedInstanceData); var serializedInstanceMetadata = JsonConvert.SerializeObject(instanceMetadata, Formatting.Indented, _jsonSerializerSettings); File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceMetadata", serializedInstanceMetadata); } catch (Exception) { throw; } foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges) { context.WroteInstanceMetadataValue(property.Key, property.Value); } context.PersistedInstance(command.InstanceData); if (command.CompleteInstance) { context.CompletedInstance(); } if (command.UnlockInstance || command.CompleteInstance) { context.InstanceHandle.Free(); } } return(true); }
private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); } else { string instanceType = ""; const string InstanceTypeXName = "{urn:schemas-microsoft-com:System.Runtime.DurableInstancing/4.0/metadata}InstanceType"; InstanceValue instanceTypeInstanceValue; if (command.InstanceMetadataChanges.TryGetValue(InstanceTypeXName, out instanceTypeInstanceValue)) { instanceType = instanceTypeInstanceValue.Value.ToString(); } Dictionary <string, object> fullInstanceData = new Dictionary <string, object>(); fullInstanceData.Add("instanceId", context.InstanceView.InstanceId); fullInstanceData.Add("instanceOwnerId", context.InstanceView.InstanceOwner.InstanceOwnerId); fullInstanceData.Add("instanceData", SerializeablePropertyBag(command.InstanceData)); fullInstanceData.Add("instanceMetadata", SerializeInstanceMetadata(context, command)); foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges) { context.WroteInstanceMetadataValue(property.Key, property.Value); } context.PersistedInstance(command.InstanceData); _stores.Save(WorkflowStoreComponents.Definition | WorkflowStoreComponents.Metadata | WorkflowStoreComponents.Streams | WorkflowStoreComponents.TerminatingError | WorkflowStoreComponents.Timer | WorkflowStoreComponents.ActivityState | WorkflowStoreComponents.JobState, fullInstanceData); } return(true); }
private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); if (!KeepInstanceDataAfterCompletion) { _repository.Delete(context.InstanceView.InstanceId); } } else { var data = SerializeData(command.InstanceData); var metadata = SerializeMetadata(context.InstanceView.InstanceMetadata, command.InstanceMetadataChanges); _repository.Save(context.InstanceView.InstanceId, new InstanceDataPackage(metadata, data)); foreach (var property in command.InstanceMetadataChanges) { context.WroteInstanceMetadataValue(property.Key, property.Value); } context.PersistedInstance(command.InstanceData); if (command.CompleteInstance) { context.CompletedInstance(); } if (command.UnlockInstance || command.CompleteInstance) { context.InstanceHandle.Free(); } } return(true); }
private long ProcessSaveCommand(InstancePersistenceContext context, SaveWorkflowCommand command) { Owner owner = CheckOwner(context, command.Name); Instance instance = PersistenceItemManager.Load <Instance>(context.InstanceView.InstanceId); if (instance == null) { // Checking instance.Owner is like an InstanceLockQueryResult. context.QueriedInstanceStore(new InstanceLockQueryResult(context.InstanceView.InstanceId, Guid.Empty)); if (context.InstanceView.IsBoundToLock) { context.InstanceHandle.Free(); throw new InstanceLockLostException(command.Name, context.InstanceView.InstanceId); } instance = new Instance() { Version = 1, Id = context.InstanceView.InstanceId, Owner = context.InstanceView.InstanceOwner.InstanceOwnerId, Metadata = new PropertyBag() }; PersistenceItemManager.SaveToFile <Instance>(instance); context.BindAcquiredLock(1); } else { // Checking instance.Owner is like an InstanceLockQueryResult. context.QueriedInstanceStore(new InstanceLockQueryResult(context.InstanceView.InstanceId, instance.Owner)); if (instance.State == InstanceState.Completed) { throw new InstanceCompleteException(command.Name, context.InstanceView.InstanceId); } if (instance.Owner == Guid.Empty) { if (context.InstanceView.IsBoundToLock) { context.InstanceHandle.Free(); throw new InstanceLockLostException(command.Name, context.InstanceView.InstanceId); } instance.Version++; instance.Owner = context.InstanceView.InstanceOwner.InstanceOwnerId; PersistenceItemManager.SaveToFile <Instance>(instance); context.BindAcquiredLock(instance.Version); } else { if (instance.Owner != context.InstanceView.InstanceOwner.InstanceOwnerId) { if (context.InstanceView.IsBoundToLock) { context.InstanceHandle.Free(); throw new InstanceLockLostException(command.Name, context.InstanceView.InstanceId); } throw new InstanceLockedException(command.Name, instance.Owner); } if (context.InstanceView.IsBoundToLock) { if (context.InstanceVersion != instance.Version) { if (context.InstanceVersion > instance.Version) { throw new InvalidProgramException("This is a bug, the context should never be bound higher than the lock."); } context.InstanceHandle.Free(); throw new InstanceLockLostException(command.Name, context.InstanceView.InstanceId); } } else { // This is the very interesting parallel-convoy conflicting handle race resolution case. Two handles // can get bound to the same lock, which is necessary to allow parallel convoy to succeed without preventing // zombied locked instances from being reclaimed. return(instance.Version); } } } foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > keyEntry in command.InstanceKeysToAssociate) { Key key = PersistenceItemManager.Load <Key>(keyEntry.Key); if (key != null) { if (key.TargetInstanceId != Guid.Empty && key.TargetInstanceId != context.InstanceView.InstanceId) { throw new InstanceKeyCollisionException(command.Name, context.InstanceView.InstanceId, new InstanceKey(keyEntry.Key), key.TargetInstanceId); } // The SaveWorkflowCommand treats this as a no-op, whether completed or not. } else { key = new Key() { Id = keyEntry.Key, State = InstanceKeyState.Associated, TargetInstanceId = context.InstanceView.InstanceId, Metadata = new PropertyBag(keyEntry.Value) }; PersistenceItemManager.SaveToFile <Key>(key); context.AssociatedInstanceKey(keyEntry.Key); if (keyEntry.Value != null) { foreach (KeyValuePair <XName, InstanceValue> property in keyEntry.Value) { context.WroteInstanceKeyMetadataValue(keyEntry.Key, property.Key, property.Value); } } } } foreach (Guid keyGuid in command.InstanceKeysToComplete) { Key key = PersistenceItemManager.Load <Key>(keyGuid); if (key != null && key.TargetInstanceId == context.InstanceView.InstanceId) { if (key.State == InstanceKeyState.Associated) //if (key.State != InstanceKeyState.Completed) { key.State = InstanceKeyState.Completed; PersistenceItemManager.SaveToFile <Key>(key); context.CompletedInstanceKey(keyGuid); } } else { // The SaveWorkflowCommand does not allow this. (Should it validate against it?) throw new InvalidOperationException("Attempting to complete a key which is not associated."); } } foreach (Guid keyGuid in command.InstanceKeysToFree) { Key key = PersistenceItemManager.Load <Key>(keyGuid); if (key != null && key.TargetInstanceId == context.InstanceView.InstanceId) { if (key.State != InstanceKeyState.Completed) { context.CompletedInstanceKey(keyGuid); } key.State = InstanceKeyState.Unknown; key.TargetInstanceId = Guid.Empty; key.Metadata = null; PersistenceItemManager.SaveToFile <Key>(key); context.UnassociatedInstanceKey(keyGuid); } else { // The SaveWorkflowCommand does not allow this. (Should it validate against it?) throw new InvalidOperationException("Attempting to complete a key which is not associated."); } } foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > keyEntry in command.InstanceKeyMetadataChanges) { Key key = PersistenceItemManager.Load <Key>(keyEntry.Key); if (key != null && key.TargetInstanceId == context.InstanceView.InstanceId && key.State == InstanceKeyState.Associated) { if (keyEntry.Value != null) { foreach (KeyValuePair <XName, InstanceValue> property in keyEntry.Value) { if (property.Value.IsDeletedValue) { key.Metadata.Remove(property.Key); } else { key.Metadata[property.Key] = new InstanceValue(property.Value); } context.WroteInstanceKeyMetadataValue(keyEntry.Key, property.Key, property.Value); } PersistenceItemManager.SaveToFile <Key>(key); } } else { // The SaveWorkflowCommand does not allow this. (Should it validate against it?) throw new InvalidOperationException("Attempting to complete a key which is not associated."); } } foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges) { if (property.Value.IsDeletedValue) { instance.Metadata.Remove(property.Key); } else { instance.Metadata[property.Key] = new InstanceValue(property.Value); } context.WroteInstanceMetadataValue(property.Key, property.Value); } if (command.InstanceData.Count > 0) { instance.Data = new PropertyBag(command.InstanceData); context.PersistedInstance(command.InstanceData); } PersistenceItemManager.SaveToFile <Instance>(instance); // The command does the implicit advancement of everything into safe completed states. if (command.CompleteInstance) { if (instance.Data == null) { instance.Data = new PropertyBag(); PersistenceItemManager.SaveToFile <Instance>(instance); context.PersistedInstance(new Dictionary <XName, InstanceValue>()); } Queue <Guid> keysToComplete = new Queue <Guid>(); foreach (KeyValuePair <Guid, InstanceKeyView> keyEntry in context.InstanceView.InstanceKeys) { if (keyEntry.Value.InstanceKeyState == InstanceKeyState.Associated) { keysToComplete.Enqueue(keyEntry.Key); } } foreach (Guid keyToComplete in keysToComplete) { Key key = PersistenceItemManager.Load <Key>(keyToComplete); key.State = InstanceKeyState.Completed; PersistenceItemManager.SaveToFile <Key>(key); context.CompletedInstanceKey(keyToComplete); } instance.State = InstanceState.Completed; instance.Owner = Guid.Empty; PersistenceItemManager.SaveToFile <Instance>(instance); context.CompletedInstance(); context.InstanceHandle.Free(); } if (command.UnlockInstance) { instance.Owner = Guid.Empty; PersistenceItemManager.SaveToFile <Instance>(instance); context.InstanceHandle.Free(); } return(0); }
private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); if (!KeepInstanceDataAfterCompletion) { DeleteFiles(context.InstanceView.InstanceId); } } else { Dictionary <string, InstanceValue> instanceData = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData); Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command); try { //var serializedInstanceData = JsonConvert.SerializeObject(instanceData, Formatting.Indented, _jsonSerializerSettings); ////File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceData", serializedInstanceData); //File.WriteAllText(_storePathInstanceData, serializedInstanceData); //var test_deserializ = JsonConvert.DeserializeObject<Dictionary<string, InstanceValue>>(serializedInstanceData, _jsonSerializerSettings); //var serializedInstanceMetadata = JsonConvert.SerializeObject(instanceMetadata, Formatting.Indented, _jsonSerializerSettings); ////File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceMetadata", serializedInstanceMetadata); //File.WriteAllText(_storePathInstanceMetadata, serializedInstanceMetadata); serialize_dc(instanceData, instanceMetadata); } catch (Exception exc) { System.Runtime.Serialization.DataContractSerializerSettings settings = new System.Runtime.Serialization.DataContractSerializerSettings { PreserveObjectReferences = true, KnownTypes = _knownTypes }; string s1 = null; System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(instanceData.GetType(), settings); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, instanceData); s1 = System.Text.Encoding.UTF8.GetString(ms.ToArray()); } Dictionary <string, InstanceValue> obj = null; System.Runtime.Serialization.DataContractSerializer deserializer = new System.Runtime.Serialization.DataContractSerializer(instanceData.GetType(), settings); using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(s1))) { obj = (Dictionary <string, InstanceValue>)deserializer.ReadObject(ms); } throw; } foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges) { context.WroteInstanceMetadataValue(property.Key, property.Value); } context.PersistedInstance(command.InstanceData); if (command.CompleteInstance) { context.CompletedInstance(); } if (command.UnlockInstance || command.CompleteInstance) { context.InstanceHandle.Free(); } } return(true); }
private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command) { if (context.InstanceVersion == -1) { context.BindAcquiredLock(0); } if (command.CompleteInstance) { context.CompletedInstance(); if (!KeepInstanceDataAfterCompletion) { DeleteFiles(context.InstanceView.InstanceId); } } else { Dictionary <string, InstanceValue> instanceData = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData); Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command); FileStream instanceDataStream; FileStream instanceMetadataStream; GetFileStreams(context.InstanceView.InstanceId, out instanceDataStream, out instanceMetadataStream, FileMode.OpenOrCreate); DataContractSerializer instanceDataSerializer; DataContractSerializer instanceMetadataSerializer; GetDataContractSerializers(out instanceDataSerializer, out instanceMetadataSerializer); try { instanceDataSerializer.WriteObject(instanceDataStream, instanceData); instanceMetadataSerializer.WriteObject(instanceMetadataStream, instanceMetadata); } catch (Exception) { throw; } finally { instanceDataStream.Flush(); instanceDataStream.Dispose(); instanceMetadataStream.Flush(); instanceMetadataStream.Dispose(); } foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges) { context.WroteInstanceMetadataValue(property.Key, property.Value); } context.PersistedInstance(command.InstanceData); if (command.CompleteInstance) { context.CompletedInstance(); } if (command.UnlockInstance || command.CompleteInstance) { context.InstanceHandle.Free(); } } return(true); }