public static void Enqueue(AppCommands commandType, IEntityBase entity, ExecutionTrigger trigger, object value, ModifyAction modifyAction) { var command = new QueuedCommand { CommandType = commandType, Target = entity.UniqueId, Status = ExecutionStatus.Pending, TriggerType = trigger, Value = value.ToString(), ModifyAction = modifyAction, DateCreated = DateTime.Now, DateScheduled = DateTime.Now }; ThreadPool.QueueUserWorkItem(delegate { ClientState.Current.DataService.Save(command); switch (trigger) { case ExecutionTrigger.Connection: EventBroker.Publish(AppEvents.RequestCommands); break; case ExecutionTrigger.Send: EventBroker.Publish(AppEvents.RequestSend); break; case ExecutionTrigger.Receive: EventBroker.Publish(AppEvents.RequestReceive); break; } }); }
public SyncEntityCommand(Entities entities, string key, QueuedCommand inner) { Values = new Dictionary<string, object> { { "wrap_access_token", CloudApi.AccessToken } }; this.key = key; this.entities = entities; this.inner = inner; }
static UserStatus GetUserStatus(QueuedCommand command) { var statusId = Int64.Parse(command.Target.Substring(1)); UserStatus status; using (VirtualMailBox.Current.StatusUpdates.ReaderLock) status = VirtualMailBox.Current.StatusUpdates.FirstOrDefault(s => s.StatusId == statusId); if (status == null) throw new ApplicationException(String.Format("The given source entity with key {0} was not found", command.Target)); return status; }
static Person GetPerson(QueuedCommand command) { var personId = Int64.Parse(command.Target.Substring(1)); Person person; using (VirtualMailBox.Current.Persons.ReaderLock) person = VirtualMailBox.Current.Persons.FirstOrDefault(s => s.PersonId == personId); if (person == null) throw new ApplicationException(String.Format("The given source entity with key {0} was not found", command.Target)); return person; }
static Message GetMessage(QueuedCommand command) { var messageid = Int64.Parse(command.Target.Substring(1)); Message message; using (VirtualMailBox.Current.Messages.ReaderLock) message = VirtualMailBox.Current.Messages.FirstOrDefault(m => m.MessageId == messageid); if (message == null) throw new ApplicationException(String.Format("The given source entity with key {0} was not found", command.Target)); return message; }
public static CommandBase CreateCommand(QueuedCommand command) { switch (command.CommandType) { case AppCommands.SendMessage: return new SendMessageCommand(GetMessage(command)); case AppCommands.SendStatusUpdate: return new SendStatusUpdateCommand(GetUserStatus(command)); case AppCommands.SyncMessage: return new SyncEntityCommand(Entities.Messages, GetMessage(command).MessageKey, command); case AppCommands.SyncPerson: return new SyncEntityCommand(Entities.Persons, GetPerson(command).PersonKey, command); case AppCommands.SyncStatusUpdate: return new SyncEntityCommand(Entities.StatusUpdates, GetUserStatus(command).StatusKey, command); } throw new ArgumentException(String.Format("Unexpected command type {0}", command.CommandType)); }