Пример #1
0
    private static int InstallService(Conf conf)
    {
        ServiceController sc;

        if ((sc = SvcUtils.CreateSvc(conf)) == null)
        {
            Libs.Print($"Failed to install Service \"{conf.ServiceName}\"");
            return(1);
        }

        var ss = $"Installed Service \"{conf.ServiceName}\"";

        Libs.Print(ss);
        Log(ss, false);
        Log("");

        var err = SvcUtils.SetSvcDescription(conf);

        if (err != null)
        {
            err = $"Failed to set description for Service \"{conf.ServiceName}\": {err}";
            Libs.Print(err);
            Libs.Print("Please run `svc remove` to remove the Service");
            Log(err);
            return(1);
        }

        return(StartService(sc));
    }
Пример #2
0
    protected override void OnStart(string[] args)
    {
        try
        {
            var svcDescription = SvcUtils.GetServiceDescriptionInSvcBin();
            var i   = svcDescription.LastIndexOf('<') + 1;
            var n   = svcDescription.Length - 1 - i;
            var cwd = svcDescription.Substring(i, n);
            Libs.SetCwd(cwd);
        }
        catch (Exception ex)
        {
            Libs.Dump($"Failed to set cwd from service's description:\r\n{ex}");
            Environment.Exit(1);
        }

        Conf = new Conf(false);

        try
        {
            Worker = new Worker(Conf, true);
            Worker.Start();
        }
        catch (Exception ex)
        {
            Conf.Abort($"Failed to start the worker:\r\n{ex}");
        }
    }
Пример #3
0
    private static void RemoveService()
    {
        stopService();

        if (!SvcUtils.DeleteSvc(Conf.ServiceName))
        {
            Abort($"Failed to remove Service \"{Conf.ServiceName}\"");
        }

        var ss = $"Removed Service \"{Conf.ServiceName}\"";

        Conf.LogFile("INFO", "");
        Conf.LogFile("INFO", ss);
        Exit(ss);
    }
Пример #4
0
    private static void ManageOneBySvrIdentity(string op, string arg1)
    {
        var info = SvcUtils.GetEasyService(arg1);
        var sc   = info.Sc;

        info.Cd();

        if (op == "log")
        {
            Libs.Abort("`log` command has been deprecated since v1.0.10");
        }

        // op = start|stop|restart|remove
        sc.Operate(op);
    }
Пример #5
0
    private static int RemoveService(ServiceController sc)
    {
        StopService(sc);
        if (!SvcUtils.DeleteSvc(conf.ServiceName))
        {
            Libs.Print($"Failed to remove Service \"{conf.ServiceName}\"");
            return(1);
        }

        var ss = $"Removed Service \"{conf.ServiceName}\"";

        Log("");
        Log(ss);
        Libs.Print(ss);
        return(0);
    }
Пример #6
0
    private static void ManageOneBySvrIdentity(string op, string arg1)
    {
        var info = SvcUtils.GetEasyService(arg1);
        var sc   = info.Sc;

        info.Cd();

        if (op == "log")
        {
            var conf   = new Conf();
            var status = sc.Status.ToString().ToLower();
            LoggingSvc(conf, status);
            return;
        }

        // op = start|stop|restart|remove
        sc.Operate(op);
    }
Пример #7
0
    private static void ManageOneByConfFile(string op)
    {
        var conf        = new Conf();
        var serviceName = conf.ServiceName;
        var sc          = SvcUtils.GetServiceController(serviceName);
        var status      = (sc == null) ? "not installed" : sc.Status.ToString().ToLower();

        if (op == "check" || op == "status")
        {
            conf.ShowConfig();
            Console.WriteLine($"\r\nService status: {status}");
            return;
        }

        if (op == "install")
        {
            if (sc != null)
            {
                Libs.Abort($"Service \"{serviceName}\" is already installed!");
            }

            SvcUtils.InstallService(conf);
            return;
        }

        if (op == "log")
        {
            LoggingSvc(conf, status);
            return;
        }

        // op: start|stop|restart|remove

        if (sc == null)
        {
            Libs.Abort($"Service \"{serviceName}\" is not installed!");
        }

        sc.Operate(op);
    }
Пример #8
0
    private static void CheckStage1()
    {
        if (Op == "create")
        {
            CreateProject();
        }

        Conf = new Conf(true);

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

        Sc     = SvcUtils.GetServiceController(Conf.ServiceName);
        Status = (Sc == null) ? "not installed" : Sc.Status.ToString().ToLower();

        if (Op == "check" || Op == "status")
        {
            Conf.ShowConfig();
            Exit($"\r\nService status: {Status}");
        }

        if (Op == "install" && Sc != null)
        {
            Abort($"Service \"{Conf.ServiceName}\" is already installed!");
        }

        if (Op != "install" && Sc == null)
        {
            Abort($"Service \"{Conf.ServiceName}\" is not installed!");
        }

        if (Op == "log" && Status != "running")
        {
            Abort($"Service \"{Conf.ServiceName}\" is not running");
        }
    }
Пример #9
0
    private static void InstallService()
    {
        if ((Sc = SvcUtils.CreateSvc(Conf)) == null)
        {
            Abort($"Failed to install Service \"{Conf.ServiceName}\"");
        }

        var ss = $"Installed Service \"{Conf.ServiceName}\"";

        Console.WriteLine(ss);
        Conf.LogFile("INFO", ss, false);
        Conf.LogFile("INFO", "");

        var err = SvcUtils.SetSvcDescription(Conf);

        if (err != null)
        {
            err = $"Failed to set description for Service \"{Conf.ServiceName}\": {err}";
            Conf.LogFile("ERROR", err);
            Abort($"{err}\r\nPlease run `svc remove` to remove the Service");
        }

        StartService();
    }
Пример #10
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 == "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));
    }
Пример #11
0
    public static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            SimpleService.RunService();
            return(0);
        }

        var op   = args[0];
        var opr  = $"|{op}|";
        var argc = args.Length - 1;
        var arg1 = argc == 1 ? args[1] : null;

        if (op == "version" || op == "--version" || op == "-v")
        {
            if (argc != 0)
            {
                Libs.Abort(USAGE);
            }

            Console.WriteLine(VERSION);
            return(0);
        }

        if (op == "create")
        {
            if (argc != 1)
            {
                Libs.Abort(USAGE);
            }

            CreateProject(arg1);
            return(0);
        }

        if (op == "list" || op == "ls")
        {
            if (argc != 0)
            {
                Libs.Abort(USAGE);
            }

            SvcUtils.ListAllEasyServices();
            return(0);
        }

        var commands = "|check|status|test-worker|install|stop|start|remove|restart|log|";

        if (!commands.Contains(opr) || argc > 1)
        {
            Libs.Abort(USAGE);
        }

        if (arg1 != null && (arg1.Contains('/') || arg1.Contains('\\')))
        {
            if (!Directory.Exists(arg1))
            {
                Libs.Abort($"Directory \"{arg1}\" not exists");
            }

            Libs.SetCwd(arg1);
            arg1 = null;
        }

        if (op == "test-worker")
        {
            if (arg1 != null && arg1 != "--popup")
            {
                Libs.Abort($"Directory argument \"{arg1}\" should contain '/' or '\\'");
            }

            TestWorker(arg1 == "--popup");
            return(0);
        }

        if ("|check|status|install|".Contains(opr) && arg1 != null)
        {
            Libs.Abort($"Directory argument \"{arg1}\" should contain '/' or '\\'");
        }

        if ("|restart|log|".Contains(opr) && arg1 == "all")
        {
            Libs.Abort(USAGE);
        }

        if (arg1 == null)
        {
            ManageOneByConfFile(op);
        }
        else if (arg1 != "all")
        {
            ManageOneBySvrIdentity(op, arg1);
        }
        else
        {
            SvcUtils.ManageAll(op);
        }

        return(0);
    }
Пример #12
0
 protected override void OnStart(string[] args)
 {
     SvcUtils.SetCwdInSvcBin();
     Worker = new Worker(SvcUtils.AddLog);
     Worker.Start();
 }