public bool SyncChannelsByPeriod(VideoReg video, DateInterval interval) { var cts = new CancellationTokenSource(); var task = new Task(() => { using (var ftp = FtpLoader.Start(video.FtpSettings)) { try { // Загружаем данные за необходимый интревал // video.BrigadeCode, video.ChannelFolder, channelFolder string localRoot = Path.Combine(channelFolder, video.BrigadeCode.ToString()); ftp.DownloadFilesByInterval(interval, video.ChannelFolder, localRoot, cts); } catch (OperationCanceledException e) { logger.Info(e, $"{video.BrigadeCode} [{interval}] operation canseled"); } catch (Exception e) { logger.Error(e); } } // Сннимаем задачу из списка задач lock (tasksLock) { tasks.RemoveAll(t => t.BrigadeCode == video.BrigadeCode && t.Interval == interval); } }, cts.Token); ChannelTask newTask = new ChannelTask { BrigadeCode = video.BrigadeCode, Interval = interval, Task = task, Cts = cts }; lock (tasksLock) { // Проверем выполняется ли в данный момент аналогичная задача если да то не надо ее дублировать ChannelTask oldTask = tasks.FirstOrDefault(x => x.BrigadeCode == video.BrigadeCode && x.Interval == interval); if (oldTask != null) { logger.Warn($"SyncChannelsByPeriod({video.BrigadeCode}, {interval}) [EXECUTION-MISS]"); return(false); } // Ставим задачу на выполнение tasks.Add(newTask); newTask.Task.Start(); } logger.Info($"SyncChannelsByPeriod({video.BrigadeCode}, {interval}) [EXECUTION]"); return(true); }
public bool SetOn(VideoReg reg) { var cts = new CancellationTokenSource(); // Задача постоянного обновления кеша данных с приборов var task = new Task(() => { logger.Info($"Task [{reg.BrigadeCode}] autoupdate device values was started"); if (string.IsNullOrEmpty(reg.ValuesFile)) { logger.Info($"Task [{reg.BrigadeCode}] autoupdate device values. File name is uncorrect."); DeleteTask(reg.BrigadeCode); return; } using (var ftp = FtpLoader.Start(reg.FtpSettings)) { while (!cts.IsCancellationRequested) { string strJson = ""; try { strJson = ftp.DownloadFile(reg.ValuesFile); } catch (Exception e) { logger.Error(e, $"Task [{reg.BrigadeCode}] autoupdate device values. Faild load file {reg.ValuesFile} [ERROR]"); } BrigadeChannelValue obj = null; try { obj = JsonConvert.DeserializeObject <BrigadeChannelValue>(strJson); } catch (Exception e) { logger.Error(e, $"Task [{reg.BrigadeCode}] autoupdate device values. Faild load file {reg.ValuesFile} [ERROR]"); return; } if (obj.BrigadeCode != reg.BrigadeCode) { logger.Error($"[IMPORTANT] Task [{reg.BrigadeCode}] Database data not equals device brigadeCode [ERROR]"); } DeviceDataCash.Instance.Set(obj); } } // Если задача была отменена убераем ее с выполнения DeleteTask(reg.BrigadeCode); logger.Info($"Task [{reg.BrigadeCode}] autoupdate device values was canceled"); }, cts.Token); DeviceDataTask newTask = new DeviceDataTask { BrigadeCode = reg.BrigadeCode, Task = task, Cts = cts }; newTask.Task.Start(); AddTask(newTask); logger.Info($"OnAutoUpdateChannelValue({reg.BrigadeCode}) [EXECUTION]"); return(true); }