private static void StartRelevantAutomationHandlers(HubEvent evt) { // Get a reference to the automation var automations = AutomationFactory.GetAutomations(evt, _hub); foreach (IAutomation automation in automations) { // If this automation is already running, cancel all running instances _taskManager.CancelRunningInstances(automation.GetType(), evt.DeviceId); // Start a task to handle the automation and a CancellationToken Source // so we can cancel it later. CancellationTokenSource cts = new CancellationTokenSource(); Func <Task> handleTask = async() => { var startedTime = DateTime.Now; Console.WriteLine($"{DateTime.Now} {automation} event: {evt.DescriptionText}"); using (var operation = _telemetryClient.StartOperation <RequestTelemetry>(automation.ToString())) { _telemetryClient.TrackEvent("Automation Started", evt.GetDictionary()); try { // This runs the Handle method on the automation class await automation.Handle(cts.Token); } catch (TaskCanceledException) { TimeSpan executionTime = DateTime.Now - startedTime; _telemetryClient.TrackEvent($"Automation Cancelled", new Dictionary <string, string>() { { "WaitTime", executionTime.TotalSeconds.ToString() }, }); Console.WriteLine($"{DateTime.Now} {automation} event from {startedTime} cancelled."); } catch (Exception ex) { operation.Telemetry.Success = false; _telemetryClient.TrackException(ex); Console.WriteLine($"{DateTime.Now} {automation} {ex} {ex.Message}"); } } }; // Ready... go handle it! Task work = Task.Run(handleTask, cts.Token); // Hold on to the task and its cancellation token source for later. _taskManager.Track(work, cts, automation.GetType(), evt.DeviceId); } // Let's take this opportunity to get rid of any completed tasks. _taskManager.RemoveCompletedTasks(); }
private static void StartRelevantAutomationHandlers(HubEvent evt) { // Get a reference to the automation var automations = AutomationFactory.GetAutomations(evt, _hub); foreach (IAutomation automation in automations) { // If this automation is already running, cancel all running instances _taskManager.CancelRunningInstances(automation.GetType(), evt.DeviceId); // Start a task to handle the automation and a CancellationToken Source // so we can cancel it later. CancellationTokenSource cts = new CancellationTokenSource(); Func <Task> handleTask = async() => { var startedTime = DateTime.Now; Console.WriteLine($"{DateTime.Now} {automation} event: {evt.DescriptionText}"); try { // This runs the Handle method on the automation class await automation.Handle(cts.Token); } catch (TaskCanceledException) { Console.WriteLine($"{DateTime.Now} {automation} event from {startedTime} cancelled."); } catch (Exception ex) { Console.WriteLine($"{DateTime.Now} {automation} {ex} {ex.Message}"); } }; // Ready... go handle it! Task work = Task.Run(handleTask, cts.Token); // Hold on to the task and its cancellation token source for later. _taskManager.Track(work, cts, automation.GetType(), evt.DeviceId); } // Let's take this opportunity to get rid of any completed tasks. _taskManager.RemoveCompletedTasks(); }