private void SaveCompletedTaskToDisk(TaskComplete task) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\KoFrMa\CompletedTasksBuffer\"); StreamWriter w = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\KoFrMa\CompletedTasksBuffer\" + task.IDTask + ".dat", true); w.WriteLine(JsonSerializationUtility.Serialize(task)); w.Close(); w.Dispose(); }
private void OnTimerTasksTick(object sender, ElapsedEventArgs e) { debugLog.WriteToLog("Task timer tick", 8); this.timerTasks.Stop(); this.timerTasks.Interval = 2147483647; if (this.ScheduledTasks.Count > 0) { List <Task> tasksDelete = new List <Task>(); debugLog.WriteToLog("Tasks found, starting to check if the time has come for each of the tasks", 5); bool successfull = false; foreach (Task item in ScheduledTasks) { if (!item.InProgress) { item.InProgress = true; debugLog.WriteToLog("Checking if the task should be started for task with ID " + item.IDTask, 7); if (item.TimeToBackup.CompareTo(DateTime.Now) <= 0) //Pokud čas úlohy už uběhl nebo zrovna neběží { debugLog.WriteToLog("Task " + item.IDTask + " should be running because it was planned to run in " + item.TimeToBackup.ToString() + ", starting the inicialization now...", 6); BackupSwitch backupInstance = new BackupSwitch(); try { debugLog.WriteToLog("Task locked, starting the backup...", 6); if (item.ScriptBefore != null) { if (item.ScriptBefore.PathToLocalScript != null || item.ScriptBefore.PathToLocalScript != "") { debugLog.WriteToLog("Runnig script from disk...", 6); this.RunScriptFromDisk(item.ScriptBefore.PathToLocalScript); } else if (item.ScriptBefore.ScriptItself != null || item.ScriptBefore.ScriptItself != "") { debugLog.WriteToLog("Runnig script included with the task...", 6); this.RunScriptFromString(item.ScriptBefore.ScriptItself, item.ScriptBefore.ScriptItselfFormat); } } //debugLog.WriteToLog("Destination of the backup is " + item.WhereToBackup[0], 8); backupInstance.Backup(this.LoadJournalFromCacheIfNeeded(item)); debugLog.WriteToLog("Task completed, setting task as successfully completed...", 6); successfull = true; //connection.TaskCompleted(item, backupInstance.BackupJournalNew, debugLog, true); } catch (Exception ex) { debugLog.WriteToLog("Task failed with fatal error " + ex, 2); //connection.TaskCompleted(item, backupInstance.BackupJournalNew, debugLog, false); } finally { if (item.ScriptAfter != null) { if (item.ScriptAfter.PathToLocalScript != null && item.ScriptAfter.PathToLocalScript != "") { debugLog.WriteToLog("Runnig script from disk...", 6); this.RunScriptFromDisk(item.ScriptAfter.PathToLocalScript); } else if (item.ScriptAfter.ScriptItself != null && item.ScriptAfter.ScriptItself != "") { debugLog.WriteToLog("Runnig script included with task...", 6); this.RunScriptFromString(item.ScriptAfter.ScriptItself, item.ScriptAfter.ScriptItselfFormat); } } if (daemonSettings.LocalLogPath != null && daemonSettings.LocalLogPath != "") { StreamWriter w = new StreamWriter(daemonSettings.LocalLogPath, true); for (int i = 0; i < backupInstance.taskDebugLog.logReport.Count; i++) { w.WriteLine(backupInstance.taskDebugLog.logReport[i]); } w.Close(); w.Dispose(); } debugLog.WriteToLog("Task " + item.IDTask + " ended. Information about the completed task will be send with the rest to the server on next occasion.", 6); TaskComplete completedTask = new TaskComplete { TimeOfCompletition = DateTime.Now, IDTask = item.IDTask, DatFile = backupInstance.BackupJournalNew, IsSuccessfull = successfull, DebugLog = backupInstance.taskDebugLog.logReport }; CompletedTasksYetToSend.Add(completedTask); this.SaveCompletedTaskToDisk(completedTask); tasksDelete.Add(item); //, DebugLog = backupInstance.taskDebugLog.logReport } } else { TimeSpan tmp = item.TimeToBackup - DateTime.Now; if (tmp.TotalMilliseconds < 2147483647) { if (timerTasks.Interval > tmp.TotalMilliseconds) { timerTasks.Stop(); timerTasks.Interval = tmp.TotalMilliseconds; timerTasks.Start(); debugLog.WriteToLog("Timer value set to this task.", 7); } else { debugLog.WriteToLog("There is another task planned earlier than this one, not changing the timer.", 7); } } else { debugLog.WriteToLog("Task is planned too far in the future, timer not set. This is a problem of server sending tasks too early.", 3); } debugLog.WriteToLog("Task " + item.IDTask + " was skipped because " + item.TimeToBackup.ToString() + " is in future.", 6); } item.InProgress = false; //ScheduledTasks.Remove(item); } } foreach (Task item in tasksDelete) { ScheduledTasks.Remove(item); } tasksDelete = null; if (timerTasks.Interval == 2147483647) { debugLog.WriteToLog("No other tasks planned", 5); } else { debugLog.WriteToLog("No other tasks started, service will check again after " + timerTasks.Interval / 1000 + 's', 5); } } else { debugLog.WriteToLog("No tasks planned.", 5); } }