public TheWorkerServiceHost(cdeHostType pType)
 {
     m_hostType = pType;
     StartupLog?.Log("Service: Exiting service instance constructor");
 }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                if (File.Exists("cdeenablestartuplog"))
                {
                    bStartupLog = true;
                    string[] lines = File.ReadAllLines("cdeenablestartuplog");
                    if (lines.Length > 0)
                    {
                        var path = lines[0];
                        if (!string.IsNullOrEmpty(path))
                        {
                            startupLogPath = path;
                        }
                    }
                }
            }
            catch { }

            foreach (string t in args)
            {
                StartupLog("Arg " + t);
            }
            //Thread.Sleep(10000);
            if (args.Length < 3)
            {
                return;
            }

            var    target           = args[0];
            var    processDirectory = args[1];
            string processName      = args[2];
            int    processPid;

            if (args[2].Contains(":"))
            {
                var t = args[2].Split(':');
                processPid = CInt(t[0]);
                if (t.Length > 1)
                {
                    processName = t[1];
                }
            }
            else
            {
                processPid = 0;
            }
            cdeHostType HostType = 0;

            if (args.Length > 3)
            {
                HostType = CHostType(args[3]);
            }

            StartupLog($"Upgrading to \"{target}\" of {processName} / pid {processPid} in directory {processDirectory}");
            //Thread.Sleep(10000);
            try
            {
                switch (target.ToUpper())
                {
                case "RESTART":
                    switch (HostType)
                    {
                    case cdeHostType.Service:
                        StartStopService(false, processName, processDirectory, processPid);
                        StartStopService(true, processName, processDirectory, processPid);
                        break;

                    case cdeHostType.IIS:
                        StopAppPool("C-DEngine", true);
                        break;

                    default:
                        if (IsMainProcessRunning(processName, processDirectory, processPid))
                        {
                            KillProcess(processName, processDirectory, processPid);
                        }
                        StartProcess(processName, processDirectory);
                        break;
                    }
                    return;

                case "WIPENODE":
                    switch (HostType)
                    {
                    case cdeHostType.Service:
                        StartStopService(false, processName, processDirectory, processPid);
                        break;

                    case cdeHostType.IIS:
                        StopAppPool("C-DEngine", false);
                        break;

                    default:
                        if (IsMainProcessRunning(processName, processDirectory, processPid))
                        {
                            KillProcess(processName, processDirectory, processPid);
                        }
                        break;
                    }
                    //HEFRE WIPE
                    try
                    {
                        StartupLog($"Wiping Node: {processDirectory}/ClientBin");
                        Directory.Delete($"{processDirectory}/ClientBin", true);
#if !CDE_NET35
                        StartupLog($"Wiping Plugins from {processDirectory}");
                        var dir = new DirectoryInfo(processDirectory);
                        foreach (var file in dir.EnumerateFiles("CDMy*.dll"))
                        {
                            file.Delete();
                        }
                        foreach (var file in dir.EnumerateFiles("C-DMy*.dll"))
                        {
                            file.Delete();
                        }
#endif
                    }
                    catch (Exception)
                    {
                        StartupLog($"WipeNode Failed for {processDirectory}/ClientBin");
                    }
                    switch (HostType)
                    {
                    case cdeHostType.Service:
                        StartStopService(true, processName, processDirectory, processPid);
                        break;

                    case cdeHostType.IIS:
                        StopAppPool("C-DEngine", true);
                        break;

                    default:
                        StartProcess(processName, processDirectory);
                        break;
                    }
                    return;

                case "STOPSVC":
                    StartStopService(false, processName, processDirectory, processPid);
                    return;

                case "STARTSVC":
                    StartStopService(true, processName, processDirectory, processPid);
                    return;

                case "START":
                    StartupLog($"Waiting to close {processName} / {processPid} in directory {processDirectory}");
                    while (IsMainProcessRunning(processName, processDirectory, processPid))
                    {
                        ;
                    }
                    StartProcess(processName, processDirectory);
                    StartupLog($"Starting {processName}.exe in directory ${processDirectory}");
                    //Thread.Sleep(10000);
                    return;

                case "STOP":
                    if (!IsMainProcessRunning(processName, processDirectory, processPid))
                    {
                        return;
                    }
                    StartupLog($"Trying to kill {processName} / {processPid} in directory {processDirectory}");
                    KillProcess(processName, processDirectory, processPid);
                    return;

                case "SET":
                    if (args.Length > 3)
                    {
                        using (Process mainProcess = new Process())
                        {
                            mainProcess.StartInfo.FileName         = "cdeTimeSet.exe";
                            mainProcess.StartInfo.WorkingDirectory = processDirectory;
                            mainProcess.StartInfo.Arguments        = target + " \"" + args[3] + "\"";
                            if (args.Length > 4)
                            {
                                mainProcess.StartInfo.Arguments += " \"" + args[4] + "\"";
                            }
                            mainProcess.Start();
                        }
                        while (IsMainProcessRunning("cdeTimeSet", null, 0))
                        {
                            ;                                                    // CODE REVIEW: where is this called from? Unclear if processDirectory and processPid are specified for SET, keeping compatible for now
                        }
                    }
                    return;
                }
                string[]      NewFiles   = cdeSplit(target, ";:;", true, true);
                List <string> MyNewFiles = new List <string>();
                foreach (string tFile in NewFiles)
                {
                    if (!File.Exists(tFile))
                    {
                        StartupLog("Update File " + tFile + " was not found....");
                    }
                    else
                    {
                        MyNewFiles.Add(tFile);
                    }
                }
                if (MyNewFiles.Count == 0)
                {
                    StartupLog("No fils found to update - exiting....");
                    return;
                }
                StartupLog("Waiting for " + processName + " to close....");
                if (args.Length > 3)
                {
                    HostType = CHostType(args[3]);
                    switch (HostType)
                    {
                    case cdeHostType.NOTSET:
                        break;

                    case cdeHostType.Service:
                        StartStopService(false, processName, processDirectory, processPid);
                        break;

                    case cdeHostType.IIS:
                        break;

                    default:
                        int tTimeOut = 15;
                        while (IsMainProcessRunning(processName, processDirectory, processPid))
                        {
                            StartupLog("waiting ... " + tTimeOut);
                            Thread.Sleep(1000);
                            tTimeOut--; if (tTimeOut == 0)
                            {
                                break;
                            }
                        }
                        if (IsMainProcessRunning(processName, processDirectory, processPid))
                        {
                            KillProcess(processName, processDirectory, processPid);
                        }
                        break;
                    }
                }
                StartupLog($"App {processName} was closed....");

                // TODO Handle cases of shutdown crashes: the process sticks around while WER does it's work, even though the SCM reports the service as stopped
                //while (IsMainProcessRunning(args[2]))
                //{
                //    StartupLog("App " + args[2] + " found running after close....");
                //    Thread.Sleep(3000);
                //}

                //Thread.Sleep(2000);

                foreach (string tFile in MyNewFiles)
                {
                    try
                    {
                        using (ZipFile zip = ZipFile.Read(tFile, new ReadOptions()))  //args[1] + "\\" +
                        {
                            if (tFile.ToUpper().EndsWith(".CDEX"))
                            {
                                zip.ExtractAll(args[1], ExtractExistingFileAction.OverwriteSilently);
                            }
                            else
                            {
                                zip.ExtractAll(args[1] + $"{Path.DirectorySeparatorChar}ClientBin", ExtractExistingFileAction.OverwriteSilently);
                            }
                        }
                        File.Delete(tFile + ".old");
                        File.Move(tFile, tFile + ".old");
                    }
                    catch (Exception ee)
                    {
                        StartupLog($"File: {tFile} failed to upgrade: {ee.ToString()}");
                    }
                }
            }
            catch (Exception e)
            {
                StartupLog(e.ToString());
                //return;
            }

            try
            {
                switch (HostType)
                {
                case cdeHostType.NOTSET:
                    break;

                case cdeHostType.Service:
                    StartStopService(true, processName, processDirectory, processPid);
                    break;

                case cdeHostType.IIS:
                    break;

                default:
                    StartProcess(processName, processDirectory);
                    break;
                }
            }
            catch { }
            StartupLog("Updater done.");
        }