Example #1
0
    private static void LoggingSvc()
    {
        if (Conf.OutFileDir == null)
        {
            Abort("Error: OutFileDir must not be $NULL");
        }

        Libs.NewThread(loggingSvc);
        Console.ReadLine();
    }
Example #2
0
    private static void StartService()
    {
        if (Sc.Status == ServiceControllerStatus.Running)
        {
            Abort($"Service \"{Sc.ServiceName}\" is already started");
        }

        Console.Write($"Starting...");
        Libs.NewThread(() => Waiting("start"));
        Sc.StartSvc();
        Exit($"\r\nStarted Service \"{Sc.ServiceName}\"");
    }
Example #3
0
    private static int stopService()
    {
        if (Sc.Status == ServiceControllerStatus.Stopped)
        {
            Console.WriteLine($"Service \"{Sc.ServiceName}\" is already stopped");
            return(1);
        }

        Console.Write($"Stopping...");
        Libs.NewThread(() => Waiting("stop"));
        Sc.StopSvc();
        Console.WriteLine($"\r\nStopped Service \"{Sc.ServiceName}\"");
        return(0);
    }
Example #4
0
    private static int StartService(ServiceController sc)
    {
        if (sc.Status == ServiceControllerStatus.Running)
        {
            Libs.Print($"Service \"{sc.ServiceName}\" is already started");
            return(1);
        }

        Console.Write($"Starting...");
        Libs.NewThread(Waiting);
        sc.StartSvc();
        Libs.Print($"\nStarted Service \"{sc.ServiceName}\"");
        return(0);
    }
Example #5
0
    private static void LoggingSvc(Conf conf, string status)
    {
        if (status != "running")
        {
            Libs.Abort($"Service \"{conf.ServiceName}\" is not running");
        }

        if (conf.OutFileDir == null)
        {
            Libs.Abort("Error: OutFileDir must not be $NULL");
        }

        Libs.NewThread(() => Libs.MonitorFile(conf.LastLineFile));
        Console.ReadLine();
    }
Example #6
0
    private static int StartService(ServiceController sc)
    {
        if (sc.Status == ServiceControllerStatus.Running)
        {
            Libs.Print($"Service \"{sc.ServiceName}\" is already started");
            return(1);
        }

        var errInfo = "Failed to start the service, please refer to svc.log to see what happened";

        Libs.NewThread(() => Waiting("Start", errInfo));
        sc.StartSvc();
        Libs.Print($"Started Service \"{sc.ServiceName}\"");
        return(0);
    }
Example #7
0
    public void Start()
    {
        if (Conf.MaxLogFilesNum != 0 && thDelete == null)
        {
            thDelete = new Thread(DeleteLoop)
            {
                IsBackground = true
            };
            thDelete.Start();
        }

        Proc = new Process
        {
            StartInfo = Psi
        };

        if (RedirectMode)
        {
            Proc.OutputDataReceived += OutPut;
            Proc.ErrorDataReceived  += OutPut;
        }

        Proc.Exited += OnExit;
        Proc.EnableRaisingEvents = true;

        try
        {
            Proc.Start();
        }
        catch (Exception ex)
        {
            Abort($"Failed to create Worker `{Conf.Worker}` in `{Conf.WorkingDir}`:\r\n{ex}");
        }

        Info($"Created Worker `{Conf.Worker}` in `{Conf.WorkingDir}`");

        if (RedirectMode)
        {
            Proc.BeginErrorReadLine();
            Proc.BeginOutputReadLine();
        }

        if (Conf.WorkerMemoryLimit != -1)
        {
            Libs.NewThread(MonitorMemory);
        }
    }
Example #8
0
    public void Start()
    {
        if (Proc == null && Conf.LastLineFile != null)
        {
            Libs.WriteLineToFile(Conf.LastLineFile, "", false);
        }

        Proc = new Process
        {
            StartInfo = Psi
        };

        if (RedirectMode)
        {
            Proc.OutputDataReceived += OutPut;
            Proc.ErrorDataReceived  += OutPut;
        }

        Proc.Exited += OnExit;
        Proc.EnableRaisingEvents = true;

        try
        {
            Proc.Start();
        }
        catch (Exception ex)
        {
            Abort($"Failed to create Worker `{Conf.Worker}` in `{Conf.WorkingDir}`:\r\n{ex}");
        }

        Info($"Created Worker `{Conf.Worker}` in `{Conf.WorkingDir}`");

        if (RedirectMode)
        {
            Proc.BeginErrorReadLine();
            Proc.BeginOutputReadLine();
        }

        if (Conf.WorkerMemoryLimit != -1)
        {
            Libs.NewThread(MonitorMemory);
        }
    }
Example #9
0
    public static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            ServiceBase.Run(new SimpleService());
            return(0);
        }

        string op = args[0];

        if (op == "version" || op == "--version" || op == "-v")
        {
            Libs.Print(VERSION);
            return(0);
        }

        if (!COMMANDS.Split('|').Contains(op) || (op == "create" && args.Length < 2))
        {
            Libs.Print(USAGE);
            return(1);
        }

        if (op == "create")
        {
            return(CreateProject(args[1]));
        }

        var err = conf.Read();

        if (err != null)
        {
            Libs.Print($"Configuration Error:\n{err}");
            return(1);
        }

        if (op == "test-worker")
        {
            return(TestWorker());
        }

        var sc     = SvcUtils.GetServiceController(conf.ServiceName);
        var status = (sc == null) ? "not installed" : sc.Status.ToString().ToLower();

        if (op == "check" || op == "status")
        {
            conf.ShowConfig();
            Libs.Print($"\nService status: {status}");
            return(0);
        }

        if (op == "log")
        {
            if (status != "running")
            {
                Libs.Print($"Service \"{conf.ServiceName}\" is not running");
                return(1);
            }
            Libs.NewThread(LoggingSvc);
            Console.ReadLine();
            return(0);
        }

        if (op == "install")
        {
            if (sc != null)
            {
                Libs.Print($"Service \"{conf.ServiceName}\" is already installed!");
                return(1);
            }

            return(InstallService(conf));
        }

        if (sc == null)
        {
            Libs.Print($"Service \"{conf.ServiceName}\" is not installed!");
            return(1);
        }

        if (op == "start")
        {
            return(StartService(sc));
        }

        if (op == "stop")
        {
            return(StopService(sc));
        }

        if (op == "restart")
        {
            StopService(sc);
            return(StartService(sc));
        }

        // op == "remove"
        return(RemoveService(sc));
    }