public async Task <IActionResult> Create([FromBody] CreateTask dto) { if (dto == null) { return(BadRequest()); } try { dto.UserId = IdentityHelper.GetUserId(User); } catch (UnauthorizedAccessException) { return(Unauthorized()); } CreatedTask createdTask = await _taskService.CreateAsync(dto, _createValidator); // Notify var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(dto.ListId, dto.UserId, dto.IsPrivate.HasValue&& dto.IsPrivate.Value); if (usersToBeNotified.Any()) { var currentUser = await _userService.GetAsync(dto.UserId); SimpleList list = await _listService.GetAsync(dto.ListId); foreach (var user in usersToBeNotified) { CultureInfo.CurrentCulture = new CultureInfo(user.Language, false); var message = _localizer["CreatedTaskNotification", IdentityHelper.GetUserName(User), createdTask.Name, list.Name]; var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, list.Id, createdTask.Id, message); var notificationId = await _notificationService.CreateOrUpdateAsync(createNotificationDto); var pushNotification = new PushNotification { SenderImageUri = currentUser.ImageUri, UserId = user.Id, Application = "To Do Assistant", Message = message, OpenUrl = GetNotificationsPageUrl(notificationId) }; _senderService.Enqueue(pushNotification); } } return(StatusCode(201, createdTask.Id)); }
private async void OnCreated(CreatedTask @event) { var document = new Document <TaskDocument> { Id = @event.TaskId.ToString(), Content = new TaskDocument { Id = @event.TaskId, Title = @event.Title, Section = BoardSections.Open } }; await _bucket.InsertAsync(document); }
//EBVMInstanceStatus is the condition in here private int PerformRunCommandActions( string[] _UniqueInstanceNames, EBVMOSType _VMOperationSystemType, string[] _Commands, Action _OnCompleted, Action _OnFailure, Action <string> _ErrorMessageAction = null) { int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex); var ProgressStack = new Stack <object>(); if (_UniqueInstanceNames != null && _UniqueInstanceNames.Length > 0) { lock (ProgressStacks_Lock) { ProgressStacks.Add(ProgressStackIx, ProgressStack); } var Request = new ConcurrentQueue <Task>(); foreach (var _InstanceName in _UniqueInstanceNames) { var FoundInstance = FindInstanceByUniqueName(_InstanceName, _ErrorMessageAction); if (FoundInstance != null) { if (GetStatusFromString(FoundInstance.PowerState.ToString()) == EBVMInstanceStatus.Running) { try { var _CommandId = "RunPowerShellScript"; if (_VMOperationSystemType == EBVMOSType.Linux) { _CommandId = "RunShellScript"; } var _RunCommandInput = new RunCommandInput() { CommandId = _CommandId, Script = _Commands.ToList() }; Task RequestAction = FoundInstance.RunCommandAsync(_RunCommandInput); Request.Enqueue(RequestAction); ProgressStack.Push(new object()); } catch (System.Exception ex) { _ErrorMessageAction?.Invoke($"BVMServiceAZ->PerformRunCommandActions->An error occurred when RunCommandInput is casting. Error: { ex.Message } - StackTrace: {ex.StackTrace}"); _OnFailure?.Invoke(); } } else { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Virtual Machine is not running."); _OnFailure?.Invoke(); } } } if (ProgressStack.Count > 0) { BTaskWrapper.Run(() => { Thread.CurrentThread.IsBackground = true; try { if (Request.TryDequeue(out Task CreatedTask)) { using (CreatedTask) { CreatedTask.Wait(); lock (ProgressStacks_Lock) { if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0) { if (CreatedTask.Exception != null) { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Error: " + CreatedTask.Exception.Message); FoundStack.Clear(); _OnFailure?.Invoke(); } else { FoundStack.Pop(); if (FoundStack.Count == 0) { ProgressStacks.Remove(ProgressStackIx); _OnCompleted?.Invoke(); } } } } } } else { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->TryDequeue error occured."); _OnFailure?.Invoke(); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Exception: " + e.Message); _OnFailure?.Invoke(); } }); } else { lock (ProgressStacks_Lock) { ProgressStacks.Remove(ProgressStackIx); } } } return(ProgressStack.Count); }
//EBVMInstanceStatus is the condition in here private int PerformActionOnInstances( Tuple <string, EBVMInstanceAction, EBVMInstanceStatus>[] _Operations, Action _OnCompleted, Action _OnFailure, Action <string> _ErrorMessageAction = null) { int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex); var ProgressStack = new Stack <object>(); if (_Operations != null && _Operations.Length > 0) { lock (ProgressStacks_Lock) { ProgressStacks.Add(ProgressStackIx, ProgressStack); } var Request = new ConcurrentQueue <Task>(); foreach (var _Operation in _Operations) { var FoundInstance = FindInstanceByUniqueName(_Operation.Item1, _ErrorMessageAction); if (FoundInstance != null) { if (GetStatusFromString(FoundInstance.PowerState.ToString()) == _Operation.Item3) { Task RequestAction = null; if (_Operation.Item2 == EBVMInstanceAction.Start) { RequestAction = FoundInstance.StartAsync(); //FoundInstance.Start(); } else if (_Operation.Item2 == EBVMInstanceAction.Stop) { RequestAction = FoundInstance.DeallocateAsync(); } if (RequestAction != null) { Request.Enqueue(RequestAction); ProgressStack.Push(new object()); } } } } if (ProgressStack.Count > 0) { BTaskWrapper.Run(() => { Thread.CurrentThread.IsBackground = true; try { if (Request.TryDequeue(out Task CreatedTask)) { using (CreatedTask) { CreatedTask.Wait(); lock (ProgressStacks_Lock) { if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0) { if (CreatedTask.Exception != null) { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->Error: " + CreatedTask.Exception.Message); FoundStack.Clear(); _OnFailure?.Invoke(); } else { FoundStack.Pop(); if (FoundStack.Count == 0) { ProgressStacks.Remove(ProgressStackIx); _OnCompleted?.Invoke(); } } } } } } else { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->TryDequeue error occured."); _OnFailure?.Invoke(); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->Exception: " + e.Message); _OnFailure?.Invoke(); } }); } else { lock (ProgressStacks_Lock) { ProgressStacks.Remove(ProgressStackIx); } } } return(ProgressStack.Count); }
private void OnCreated(CreatedTask @event) { Id = @event.TaskId; Title = @event.Title; Section = BoardSections.Open; }