public virtual void Invoke(CommandLineApplication app)
        {
            CommandOption nameOpt     = app.Option("-name", null, CommandOptionType.SingleValue);
            CommandOption timeoutOpt  = app.Option("-timeout", null, CommandOptionType.SingleValue);
            CommandOption intervalOpt = app.Option("-interval", null, CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                string process  = nameOpt.Value();
                string timeout  = timeoutOpt.Value();
                string interval = intervalOpt.Value();

                decimal timeoutMins  = -1;
                decimal intervalMins = -1;

                timeout.TryParseAny(out timeoutMins);
                interval.TryParseAny(out intervalMins);

                bool valid = Validate(process, timeoutMins, intervalMins);
                if (!valid)
                {
                    throw new ArgumentException("One of parameters was invalid.");
                }

                ProcessHandler handler = new ProcessHandler(process);
                bool processExist      = handler.Exist();
                if (!processExist)
                {
                    outHandler.Error(string.Format("Process '{0}' was not found.", process));
                }
                else
                {
                    decimal msecTimer = timeoutMins * msecInMins;
                    int msecInterval  = (int)(intervalMins * msecInMins);
                    if (msecInterval == 0)
                    {
                        msecInterval = msecInSec;
                    }

                    while (msecTimer > 0)
                    {
                        string statusMessage = string.Format("{0} is running. {1}sec left before killing.",
                                                             process,
                                                             msecTimer / msecInSec);

                        outHandler.Information(statusMessage);
                        msecTimer -= msecInterval;

                        Thread.Sleep(msecInterval);
                        if (!handler.Exist())
                        {
                            processExist = false;
                            break;
                        }
                    }

                    if (processExist)
                    {
                        handler.Kill();
                        outHandler.Information("Killing...");
                    }
                    outHandler.Information(string.Format("{0} was killed.", process));
                }
            });
        }
Beispiel #2
0
        public bool Process(string[] args)
        {
            if (args[0].Equals("?") || args[0].Equals("help"))
            {
                Help();
                return(true);
            }

            if (args[0].Equals("run") && args.Length > 1)
            {
                var app = args[1];

                var param = "";

                if (args.Length > 2)
                {
                    param = string.Join(" ", args.Skip(2));
                }

                var code = ProcessHandler.Run(app, param, true);
                Log.Entry(LogName, "--> Return code = " + code);

                return(true);
            }

            if (args[0].Equals("runexe") && args.Length > 1)
            {
                var app = args[1];

                var param = "";

                if (args.Length > 2)
                {
                    param = string.Join(" ", args.Skip(2));
                }

                ProcessHandler.RunEXE(app, param, true);
                return(true);
            }

            if (args[0].Equals("runexe") && args.Length > 1)
            {
                var app = args[1];

                var param = "";

                if (args.Length > 2)
                {
                    param = string.Join(" ", args.Skip(2));
                }

                ProcessHandler.RunEXE(app, param, true);
                return(true);
            }

            if (args[0].Equals("runclient") && args.Length > 1)
            {
                var app = args[1];

                var param = "";

                if (args.Length > 2)
                {
                    param = string.Join(" ", args.Skip(2));
                }

                ProcessHandler.RunClientEXE(app, param, true);
                return(true);
            }

            if (args[0].Equals("kill") && args.Length > 1)
            {
                ProcessHandler.Kill(args[1]);
                return(true);
            }

            if (args[0].Equals("killexe") && args.Length > 1)
            {
                ProcessHandler.KillEXE(args[1]);
                return(true);
            }

            if (args[0].Equals("killall") && args.Length > 1)
            {
                ProcessHandler.KillAll(args[1]);
                return(true);
            }

            if (args[0].Equals("killallexe") && args.Length > 1)
            {
                ProcessHandler.KillAllEXE(args[1]);
                return(true);
            }

            return(false);
        }