/// <exception cref="Aditi.Scheduler.SchedulerModelValidationException">Thrown when Task model is invalid</exception> /// <exception cref="Aditi.Scheduler.SchedulerException"></exception> public async Task<Guid> UpdateTaskAsync(TaskModel task) { var client = CreateHttpClient(); var jsonFormatter = new JsonMediaTypeFormatter(); var content = new ObjectContent<TaskModel>(task, jsonFormatter); var response = client.PutAsync(_uri.ToString() + task.Id.ToString(), content).Result; if (response.StatusCode == HttpStatusCode.Accepted) return GetOperationId(response); if (response.StatusCode == HttpStatusCode.BadRequest) { //check for model state errors string responseMessage = await response.Content.ReadAsStringAsync(); //Json Message received from server //{"Message":"The request is invalid.","ModelState":{"task.Start":["Time must be in the future"], //"task.End":["Time must be in the future"],"task.CronExpression":["Cron expression is invalid"]}} if (responseMessage.Contains(SchedulerConstants.Modelstate)) { throw CreateSchedulerException(responseMessage); } else { throw new SchedulerException(responseMessage); } } //TODO Is there any scenario code block will reach this? return Guid.Empty; }
private async void btnCreate_Click(object sender, RoutedEventArgs e) { List<Guid> opIds; List<OperationStatus> opStatus; var tasks = new TaskModel[5]; tasks = GetTasks(); //Synchronous creation of tasks txtStatus.Text += "Starting synchronous task creation."; //start creating tasks and get the list of operation Ids opIds = new List<Guid>(); opStatus = new List<OperationStatus>(); foreach (TaskModel t in tasks) { try { opIds.Add(scheduledTask.CreateTask(t)); } catch (SchedulerModelValidationException me) { txtStatus.Text += "\n" + me.ExceptionMessage; //TODO: Log exception } catch (SchedulerException schedExp) { //TODO: Log exception } } txtStatus.Text += "\nGot operationIds, now checking status"; //get operation status for each operationId if (opIds.Count > 0) { try { opStatus = opIds.Select(t => scheduledTask.GetOperationStatus(t, true)).ToList(); } catch (SchedulerException shedExp) { //TODO: Log exception } if (opStatus.Count > 0) DisplayOperationStatus(opStatus); } txtStatus.Text += "\nEnding synchronous task creation."; //Asynsynchronous creation of tasks txtStatus.Text += "\nStarting asynsynchronous task creation."; //opIds = await CreatTasksAsync(); foreach (TaskModel t in tasks) { try { var currentOperationId = await scheduledTask.CreateTaskAsync(t); opIds.Add(currentOperationId); } catch (SchedulerModelValidationException me) { //write to log txtStatus.Text += "\n" + me.ExceptionMessage; } catch (SchedulerException exp) { //write to log } } txtStatus.Text += "\nGot operationIds, now checking status"; if (opIds.Count > 0) { foreach (Guid t in opIds) { try { var currentStatus = await scheduledTask.GetOperationStatusAsync(t, true); opStatus.Add(currentStatus); } catch (SchedulerException exp) { //write log } } if (opStatus.Count > 0) DisplayOperationStatus(opStatus); } txtStatus.Text += "\nEnding asyncsynchronous task creation."; }
/// <exception cref="Aditi.Scheduler.SchedulerModelValidationException">Thrown when Task model is invalid</exception> /// <exception cref="Aditi.Scheduler.SchedulerException"></exception> public Guid UpdateTask(TaskModel task) { var request = CreateWebApiRequest(SchedulerConstants.TaskRelativePath + task.Id.ToString()); request.Method = HttpMethod.Put.Method; string json = JsonConvert.SerializeObject(task, new IsoDateTimeConverter() { DateTimeFormat = SchedulerConstants.IsoDateFormat }); try { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); //get response HttpWebResponse response = GetResponse(request); if (response.StatusCode == HttpStatusCode.Accepted) return GetOperationId(response); } } catch (WebException we) { throw CreateSchedulerException(we); } //TODO: Is there any scenario code block will reach this? return Guid.Empty; }
private static TaskModel[] GetTasks() { var tasks = new TaskModel[5]; //valid tasks[0] = new TaskModel { Name = "ZZ first Nuget Task", JobType = JobType.Webhook, CronExpression = "0 0 12 1/1 * ? *", // run every 5 minutes; http://cronmaker.com/ Params = new Dictionary<string, object> { {"url", "http:/local/"} } }; //valid tasks[1] = new TaskModel { Name = "ZZ second Nuget Task", JobType = JobType.Webhook, CronExpression = "0 0 12 1/1 * ? *", // run every 5 minutes; http://cronmaker.com/ Params = new Dictionary<string, object> { {"url", "http:/local/"} } }; //invalid tasks[2] = new TaskModel { Name = "ZZ third Nuget Task", JobType = JobType.Webhook, CronExpression = "ABCD", // run every 5 minutes; http://cronmaker.com/ Params = new Dictionary<string, object> { {"url", "http:/local/"} }, Start = DateTime.Now, End = DateTime.Now.Subtract(new TimeSpan(3, 0, 0)) }; //invalid tasks[3] = new TaskModel { Name = "", JobType = JobType.Webhook, CronExpression = "ABCD", // run every 5 minutes; http://cronmaker.com/ Params = new Dictionary<string, object> { {"url", "http:/local/"} } }; //valid tasks[4] = new TaskModel { Name = "ZZ fifth Nuget Task", JobType = JobType.Webhook, CronExpression = "0 0 12 1/1 * ? *", // run every 5 minutes; http://cronmaker.com/ Params = new Dictionary<string, object> { {"url", "http:/local/"} } }; return tasks; }