public static bool SpawAndRun(BackupScheduleWrapper job) { SpawnLogger.Log("Spawn request {0}".FillBlanks(job.Name)); string procName = "Bummer.ScheduleRunner.{0}".FillBlanks(job.ID); Process[] processes = Process.GetProcessesByName(procName); if (processes.Length > 0) { SpawnLogger.Log("Spawn request, already running {0}".FillBlanks(job.Name)); return(false); } DirectoryInfo dir = new DirectoryInfo(Configuration.DataDirectory.FullName + "\\" + job.ID); if (Directory.Exists(dir.FullName)) { try { Directory.Delete(dir.FullName, true); } catch (Exception ex) { SpawnLogger.Log("Spawn request, cleanup failed {0}: {1}".FillBlanks(job.Name, ex.Message)); return(false); } } Type t = typeof(ScheduleJobSpawner); FileInfo fi = new FileInfo(t.Assembly.Location); DirectoryInfo binDir = new DirectoryInfo(fi.DirectoryName); binDir.Copy(dir.FullName, true); string exeName = "{0}\\{1}.exe".FillBlanks(dir.FullName, procName); fi.CopyTo(exeName, true); FileInfo[] configs = binDir.GetFiles("*.config"); foreach (FileInfo configFile in configs) { if (configFile.Name.Contains(".vshost.")) { continue; } configFile.CopyTo(exeName + ".config"); break; } SpawnLogger.Log("Spawn request spawning {0}".FillBlanks(job.Name)); Process p = new Process(); p.StartInfo = new ProcessStartInfo(exeName); p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.EnableRaisingEvents = true; p.Exited += p_Exited; p.Start(); return(true); }
static void Main() { //bool s = true; //while( s ) { // Thread.Sleep( 100 ); //} SpawnLogger.Log("Spawn starting"); Process p = Process.GetCurrentProcess(); string n = p.ProcessName.Replace("Bummer.ScheduleRunner.", ""); int id; if (!int.TryParse(n, out id)) { SpawnLogger.Log("Spawn failed to parse ID from {0}".FillBlanks(n)); Console.WriteLine("Invalid start of ScheduleRunner"); Console.Error.WriteLine("Invalid start of ScheduleRunner"); return; } BackupScheduleWrapper sc = Configuration.GetSchedule(id); if (sc == null) { SpawnLogger.Log("Spawn unable to find schedule {0}".FillBlanks(id)); Console.WriteLine("ScheduleRunner is unable to find schedule to run"); Console.Error.WriteLine("ScheduleRunner is unable to find schedule to run"); return; } Process[] processesRunning = Process.GetProcessesByName(p.ProcessName); if (processesRunning.Length > 1) { SpawnLogger.Log("Spawn {0} already running".FillBlanks(id)); Console.WriteLine("ScheduleRunner's schedule is already running"); Console.Error.WriteLine("ScheduleRunner's schedule is already running"); return; } SpawnLogger.Log("Spawn executing schedule {0}".FillBlanks(sc.Name)); try { sc.Execute(); } catch (Exception ex) { SpawnLogger.Log("Spawn execution error for schedule {0}: {1}".FillBlanks(sc.Name, ex.Message)); } }