public void Run() { MQueryCommand.TryUpdatePluginTaskLastStartTime(Id, DateTime.UtcNow); if (!Directory.Exists(_pluginDirectiryPath)) { MLogger.Error($"Directory not exists: {_pluginDirectiryPath}"); return; } string pluginFileFullPath = Path.Combine(_pluginDirectiryPath, PluginFileName); if (!File.Exists(pluginFileFullPath)) { MLogger.Error($"File not exists: {pluginFileFullPath}"); return; } var res = PluginOperationAdapter.GetFromDll(pluginFileFullPath); if (!res.IsComplete) { MLogger.Error(res.Message); } try { PluginOperation pluginOperation = res.PluginOperationInstance; Assembly pluginAssembly = Assembly.Load(File.ReadAllBytes(pluginOperation.DllFullPath)); Type pluginOperationAttributesType = null; if (!string.IsNullOrEmpty(pluginOperation.AttributesTypeName)) { pluginOperationAttributesType = pluginAssembly.GetType(pluginOperation.AttributesTypeName); } Type pluginOperationType = pluginAssembly.GetType(pluginOperation.OperationTypeName); MagicUpdaterCommon.Abstract.Operation operation = null; string settingsJson = null; if (pluginOperationAttributesType != null) { string pluginSettingsJsonFileFullPath = Path.Combine(_pluginDirectiryPath, $"{PluginFileName.Replace($"{Path.GetExtension(PluginFileName)}", "")}.json"); if (!File.Exists(pluginSettingsJsonFileFullPath)) { MLogger.Error($"Missing settings json for plugin operation: {pluginSettingsJsonFileFullPath}"); return; } settingsJson = File.ReadAllText(pluginSettingsJsonFileFullPath); } if (pluginOperationType.GetConstructors()[0].GetParameters().Length == 2) { operation = (MagicUpdaterCommon.Abstract.Operation)Activator.CreateInstance(pluginOperationType, 0, settingsJson); } else { operation = (MagicUpdaterCommon.Abstract.Operation)Activator.CreateInstance(pluginOperationType, 0); } if (operation != null) { operation.Run(); } else { MLogger.Error("Plugin operation instance is null!"); } } catch (Exception ex) { MLogger.Error(ex.ToString()); } MQueryCommand.TryUpdatePluginTaskLastEndTime(Id, DateTime.UtcNow); DateTime period = new DateTime(1889, 1, 1, 0, 0, 0); switch ((ShedulerTaskModes)Mode) { case ShedulerTaskModes.Once: MQueryCommand.TrySetPluginTaskEnabled(Id, false); period = new DateTime(1889, 1, 1, 0, 0, 0); break; case ShedulerTaskModes.Daily: period = DateTime.UtcNow.AddDays(1); break; case ShedulerTaskModes.Weekly: period = DateTime.UtcNow.AddDays(7); break; case ShedulerTaskModes.Monthly: period = DateTime.UtcNow.AddMonths(1); break; case ShedulerTaskModes.Hours: period = DateTime.UtcNow.AddHours(RepeatValue ?? 0); break; case ShedulerTaskModes.Minutes: period = DateTime.UtcNow.AddMinutes(RepeatValue ?? 0); break; } MQueryCommand.TryUpdatePluginTaskNextStartTime(Id, period); }
private void InitOperationAttributes(int operationId) { if (operationId == 0) { operationAttributes1.InitializeControl(null); return; } _viewShedulerStepModel.nameVis = cbOperation.Text; string fileName = OperationTools.GetOperationFileNameById(operationId); if (string.IsNullOrEmpty(fileName)) { operationAttributes1.InitializeControl(null); return; } Type attrType = null; string operName = OperationTools.GetOperationNameEnById(operationId); string operFileName = OperationTools.GetOperationFileNameById(operationId); string operFileMd5 = OperationTools.GetOperationFileMd5ById(operationId); if (!string.IsNullOrEmpty(operName) && !string.IsNullOrEmpty(operFileName) && !string.IsNullOrEmpty(operFileMd5)) { attrType = PluginOperationAdapter.GetPluginOperationAttributesType( operName , operFileName , operFileMd5); } if (attrType != null) { IOperationAttributes operationAttributesInstance = (IOperationAttributes)Activator.CreateInstance(attrType); //Ищем сохраненные атрибуты JObject savedModel = NewtonJson.GetModelFromJson(_viewShedulerStepModel.OperationAttributes) as JObject; JToken token = null; if (savedModel != null && savedModel.HasValues) { foreach (var prop in operationAttributesInstance.GetType().GetProperties()) { savedModel.TryGetValue(prop.Name, out token); if (token != null) { object propValue = ExtTools.ConvertStringToType(token.ToString(), prop.PropertyType); if (propValue != null) { prop.SetValue(operationAttributesInstance, propValue); } } } } operationAttributes1.InitializeControl(operationAttributesInstance, operationId); } else { operationAttributes1.InitializeControl(null); } }
private void GetOperations() { OperationList.Clear(); DataSet ds = null; if (!EnableOnlyRestartOperationMode) { // Берем все оперции за исключением RestartMagicUpdater_Service (1003) т.к. она нужна для // перезапуска MagicUpdater'a и должна выполняться в отдельном потоке. ds = SqlWorks.ExecProc("SelectOperationsList", MainSettings.MainSqlSettings.ComputerId); } else { // Берем только операцию RestartMagicUpdater_Service (1003) ds = SqlWorks.ExecProc("SelectRestartOperation", MainSettings.MainSqlSettings.ComputerId); } if (ds != null) { foreach (DataRow dr in ds.Tables[0].Rows) { int id = Convert.ToInt32(dr["ID"]); if (!SetOperationReaded(id)) { NLogger.LogErrorToBaseOrHdd(MainSettings.MainSqlSettings.ComputerId, $"Ошибка чтения операции. ID: {id}"); continue; } string attrsJson = Convert.ToString(dr["Attributes"]); var operationSqlDto = GetOperationName(Convert.ToInt32(dr["OperationType"])); string ot = operationSqlDto.Name; string fileName = operationSqlDto.FileName; string fileMD5 = operationSqlDto.FileMD5; Type t = null; //Если имя файла не пустое - это плагин операция (dll) if (string.IsNullOrEmpty(fileName)) { t = Type.GetType($"MagicUpdater.Operations.{ot}"); } else { t = PluginOperationAdapter.GetPluginOperationType(ot, fileName, fileMD5); } if (t != null) { try { Operation operation; if (t.GetConstructors()[0].GetParameters().Length == 2) { operation = (Operation)Activator.CreateInstance(t, id, attrsJson); } else { operation = (Operation)Activator.CreateInstance(t, id); } OperationList.Add(operation); } catch (Exception ex) { NLogger.LogErrorToBaseOrHdd(MainSettings.MainSqlSettings.ComputerId, $"Возможная причина. Несовпадение сигнатур операций БД и Шарпа. Original: {ex.Message.ToString()}"); } } else { NLogger.SetReportForOperation(id, false, "Не удалось найти тип операции"); } } } }