Beispiel #1
0
        int _softKillSeqIndex = -1;                                       // current killseq item just being processed

        public SoftKiller(AppDef appDef)
        {
            this._appDef = appDef;
            this._proc   = null;

            parseXml();
        }
Beispiel #2
0
        Dictionary <string, string> BuildVars(AppDef appDef, Dictionary <string, string> internalVars, Dictionary <string, string> extraVars)
        {
            // start with externally defined (global) internal vars
            var res = new Dictionary <string, string>(internalVars);

            // add the local variables from appdef
            foreach (var kv in appDef.LocalVarsToSet)
            {
                res[kv.Key] = kv.Value;
            }

            // process explicitly specified variables
            foreach (var kv in extraVars)
            {
                // add extra var
                if (!String.IsNullOrEmpty(kv.Value))
                {
                    res[kv.Key] = kv.Value;
                }
                else                 // uset the var if empty value if provided
                {
                    res.Remove(kv.Key);
                }
            }

            return(res);
        }
Beispiel #3
0
        public WindowPoppedUpInitDetector(LocalApp app, XElement xml)
        {
            _app       = app;
            _appState  = _app.AppState;
            _appDef    = _app.RecentAppDef;
            _processId = _app.ProcessId;

            try
            {
                parseArgs(xml);
            }
            catch
            {
                throw new InvalidAppInitDetectorArguments(Name, xml.ToString());
            }

            if (_titleRegExp is null)
            {
                throw new InvalidAppInitDetectorArguments(Name, xml.ToString());
            }

            _appState.Initialized = false; // will be set to true as soon as the exit code condition is met

            log.DebugFormat("WindowPoppedUpInitDetector: Waiting for window with titleRegExp {0}, appid {1}, pid {2}", _titleRegExpString, _appDef.Id, _app.ProcessId);
        }
Beispiel #4
0
 public MainWindowStyler(LocalApp app)
 {
     _app = app;
     //_appState = _app.AppState;
     _appDef   = _app.RecentAppDef;
     _appState = _app.AppState;
 }
Beispiel #5
0
        bool AreAllDepsSatisfied( AppDef appDef )
        {
            // pokud jsou vsechny aplikace na kterych ta nase zavisi spousteny
            bool allDepsSatisfied = true;

            if (appDef.Dependencies != null)
            {
                foreach (var depName in appDef.Dependencies)
                {
                    AppIdTuple depId = AppIdTuple.fromString(depName, appDef.Id.MachineId);

                    if (!_appsState.ContainsKey(depId))
                    {
                        // throw exception "Unknown dependency"
                        throw new UnknownDependencyException(depName);
                    }

                    var dep = _appsState[depId];
                    if (!dep.Initialized)
                    {
                        allDepsSatisfied = false;
                        break;
                    }

                }
            }
            return allDepsSatisfied;
        }
Beispiel #6
0
        public CrashWatcher(LocalApp app)
        {
            _app      = app;
            _appState = _app.AppState;
            _appDef   = _app.RecentAppDef;

            _state = eState.WaitingForCrash;
        }
Beispiel #7
0
        int MAX_TRIES        = -1;  // how many times to try restarting before giving up; -1 = forever

        /// <param name="vars">what env/macro vars to set for a process; null=no change</param>
        public AppRestarter(LocalApp app, bool waitBeforeRestart, Dictionary <string, string>?vars)
        {
            _app      = app;
            _appDef   = _app.RecentAppDef;
            _appState = _app.AppState;
            _vars     = vars;

            parseXml();

            Reset(waitBeforeRestart);
        }
Beispiel #8
0
 public static AppState GetDefault(AppDef ad)
 {
     return(new AppState()
     {
         Initialized = false,
         Running = false,
         Started = false,
         Dying = false,
         //Disabled = ad.Disabled
         lastChange = DateTime.MinValue
     });
 }
Beispiel #9
0
            public override void Execute(AppDef appDef, Process proc)
            {
                base.Execute(appDef, proc);

                string keys = X.getStringAttr(xel, "Keys", "", ignoreCase: true);

                #if Windows
                IntPtr h = proc.MainWindowHandle;
                SetForegroundWindow(h);
                System.Windows.Forms.SendKeys.SendWait(keys);
                #endif
            }
Beispiel #10
0
        //public bool TryGet( AppIdTuple appId, out AppState appState )
        //{
        //	appState = default(AppState);
        //	return false;
        //}

        /// <summary>
        /// Adds a new local app record if not yet existing
        /// </summary>
        /// <returns>null if already exisitng, new rec if just added</returns>
        public LocalApp?AddIfMissing(AppDef appDef, string?planName)
        {
            LocalApp?la;

            if (!_apps.TryGetValue(appDef.Id, out la))
            {
                la = new LocalApp(appDef, _sharedContext);
                _apps[appDef.Id] = la;
                return(la);
            }
            return(null);            // not added, already existing
        }
Beispiel #11
0
 /// <summary>
 /// Sends updated app def to their respective agent
 /// This happens when the appdef changes because of plan switch etc.
 /// </summary>
 /// <param name="appDef"></param>
 void SendAppDefAddedOrUpdated(AppDef ad)
 {
     foreach (var cl in _server.Clients)
     {
         if (cl.Name == ad.Id.MachineId || cl.IsGui)
         {
             var m = new Net.AppDefsMessage(new AppDef[1] {
                 ad
             }, incremental: true);
             _server.SendToSingle(m, cl.Name);
         }
     }
 }
Beispiel #12
0
        /// <summary>
        /// Creates new local app record if not exist yet.
        /// Otherwise updates the UpcomingAppDef.
        /// </summary>
        /// <param name="ad"></param>
        /// <param name="planName"></param>
        /// <returns></returns>
        public LocalApp AddOrUpdate(AppDef ad)
        {
            LocalApp?la;

            if (_apps.TryGetValue(ad.Id, out la))
            {
                la.UpdateAppDef(ad);
            }
            else
            {
                la           = new LocalApp(ad, _sharedContext);
                _apps[ad.Id] = la;
            }
            return(la);
        }
Beispiel #13
0
 public void AddOrUpdate(AppDef newAppDef)
 {
     // check for change, fire change cb if there is
     if (_appDefs.TryGetValue(newAppDef.Id, out var existingRec))
     {
         if (existingRec != newAppDef)
         {
             _appDefs[newAppDef.Id] = newAppDef;
             Updated?.Invoke(newAppDef);
         }
     }
     else
     {
         _appDefs[newAppDef.Id] = newAppDef;
         Added?.Invoke(newAppDef);
     }
 }
        // Checks within a single plan only
        // Does not find cross-plan circular dependencies...
        void CheckDependenciesCircular(PlanDef planDef, AppDef ad, Dictionary <AppIdTuple, bool> depsUsed)
        {
            if (ad.Dependencies is not null)
            {
                foreach (var depName in ad.Dependencies)
                {
                    var depId = new AppIdTuple(depName);
                    if (depsUsed.ContainsKey(depId))
                    {
                        throw new CircularDependencyException($"{planDef.Name}: {ad.Id}: Circular dependency {depName} found.");
                    }
                    // remember this dep
                    depsUsed[depId] = true;

                    // check it recursively
                    var depAppDef = planDef.AppDefs.FirstOrDefault(x => x.Id == depId);
                    if (depAppDef is not null)
                    {
                        CheckDependenciesCircular(planDef, depAppDef, depsUsed);
                    }
                }
            }
        }
Beispiel #15
0
        //<TimeOut>2.0</TimeOut>
        public TimeOutInitDetector(LocalApp app, XElement xml)
        {
            _app           = app;
            this._appState = app.AppState;
            this._appDef   = app.RecentAppDef;


            try
            {
                var timeString = xml.Value;

                _timeOut = Double.Parse(timeString, CultureInfo.InvariantCulture);
            }
            catch
            {
                throw new InvalidAppInitDetectorArguments(Name, xml.ToString());
            }

            _appState.Initialized = false; // will be set to true as soon as the exit code condition is met

            _initialTicks = DateTime.UtcNow.Ticks;
            log.DebugFormat("TimeOutInitDetector: Waiting {0} sec, appid {1}", _timeOut, _appDef.Id);
        }
Beispiel #16
0
        public Launcher(AppDef appDef, SharedContext sharedContext, Dictionary <string, string>?extraVars = null)
        {
            this.ctrl = sharedContext.Client;
            if (ctrl == null)
            {
                throw new ArgumentNullException("ctrl", "Valid network-bound Dirigent Control required");
            }
            this._appDef   = appDef;
            _sharedContext = sharedContext;

            _relativePathsRoot = sharedContext.RootForRelativePaths;

            this._planName = appDef.PlanName;
            this._masterIP = _sharedContext.Client.MasterIP;

            _extraVars         = extraVars ?? new();
            this._internalVars = BuildVars(appDef, _sharedContext.InternalVars, _extraVars);

            //_cmdRepo = new CommandRepository( ctrl );
            //DirigentCommandRegistrator.Register( _cmdRepo );

            _softKiller = new SoftKiller(appDef);

            // handle the KillSoftly flag for backward compatibility
            if (appDef.KillSoftly)
            {
                if (_softKiller.IsDefined)
                {
                    log.ErrorFormat("{0}: KillSoftly can't be used together with SoftKill! Using just the SoftKill...", appDef.Id);
                }
                else                 // add a single Close action to the soft kill seq, with default timeout
                {
                    _softKiller.AddClose();
                }
            }
        }
Beispiel #17
0
 public virtual void Execute(AppDef appDef, Process proc)
 {
     // starts counting
     sw.Restart();
 }
Beispiel #18
0
 public AppDefaults(AppDef appDef)
 {
     AppDef = appDef;
 }
Beispiel #19
0
 public AppDef(Dirigent.AppDef x)
 {
     id     = x.Id.ToString();
     groups = x.Groups;
 }
Beispiel #20
0
            public override void Execute(AppDef appDef, Process proc)
            {
                base.Execute(appDef, proc);

                proc.CloseMainWindow();
            }
        AppDef readAppElement(XElement e)
        {
            AppDef a;

            // first load templates
            var templateName = X.getStringAttr(e, "Template");

            if (!string.IsNullOrEmpty(templateName))
            {
                XElement?te = (from t in doc?.Element("Shared")?.Elements("AppTemplate")
                               where t.Attribute("Name")?.Value == templateName
                               select t).FirstOrDefault();

                if (te == null)
                {
                    // FIXME: tog that template is missing
                    var msg = String.Format("Template '{0}' not found", templateName);
                    log.ErrorFormat(msg);
                    throw new ConfigurationErrorException(msg);
                    //a = new AppDef();
                }
                else
                {
                    a = readAppElement(te);
                }
            }
            else
            {
                a = new AppDef();
            }

            // read element content into memory, apply defaults
            var x = new
            {
                Id                       = e.Attribute("AppIdTuple")?.Value,
                ExeFullPath              = e.Attribute("ExeFullPath")?.Value,
                StartupDir               = e.Attribute("StartupDir")?.Value,
                CmdLineArgs              = e.Attribute("CmdLineArgs")?.Value,
                StartupOrder             = e.Attribute("StartupOrder")?.Value,
                Disabled                 = e.Attribute("Disabled")?.Value,
                Volatile                 = e.Attribute("Volatile")?.Value,
                ReusePrevVars            = e.Attribute("ReusePrevVars")?.Value,
                LeaveRunningWithPrevVars = e.Attribute("LeaveRunningWithPrevVars")?.Value,
                RestartOnCrash           = e.Attribute("RestartOnCrash")?.Value,
                AdoptIfAlreadyRunning    = e.Attribute("AdoptIfAlreadyRunning")?.Value,
                PriorityClass            = e.Attribute("PriorityClass")?.Value,
                InitCondition            = e.Attribute("InitCondition")?.Value,
                SeparationInterval       = e.Attribute("SeparationInterval")?.Value,
                MinKillingTime           = e.Attribute("MinKillingTime")?.Value,
                Dependecies              = e.Attribute("Dependencies")?.Value,
                KillTree                 = e.Attribute("KillTree")?.Value,
                KillSoftly               = e.Attribute("KillSoftly")?.Value,
                WindowStyle              = e.Attribute("WindowStyle")?.Value,
                WindowPos                = e.Elements("WindowPos"),
                Restarter                = e.Element("Restarter"),
                SoftKill                 = e.Element("SoftKill"),
                Env                      = e.Element("Env"),
                InitDetectors            = e.Element("InitDetectors")?.Elements(),
                Groups                   = e.Attribute("Groups")?.Value,
            };

            // then overwrite templated values with current content
            if (x.Id != null)
            {
                a.Id = new AppIdTuple(x.Id);
            }
            if (x.ExeFullPath != null)
            {
                a.ExeFullPath = x.ExeFullPath;
            }
            if (x.StartupDir != null)
            {
                a.StartupDir = x.StartupDir;
            }
            if (x.CmdLineArgs != null)
            {
                a.CmdLineArgs = x.CmdLineArgs;
            }
            if (x.StartupOrder != null)
            {
                a.StartupOrder = int.Parse(x.StartupOrder);
            }
            if (x.Disabled != null)
            {
                a.Disabled = (int.Parse(x.Disabled) != 0);
            }
            if (x.Volatile != null)
            {
                a.Volatile = (int.Parse(x.Volatile) != 0);
            }
            if (x.ReusePrevVars != null)
            {
                a.ReusePrevVars = (int.Parse(x.ReusePrevVars) != 0);
            }
            if (x.LeaveRunningWithPrevVars != null)
            {
                a.LeaveRunningWithPrevVars = (int.Parse(x.LeaveRunningWithPrevVars) != 0);
            }
            if (x.RestartOnCrash != null)
            {
                a.RestartOnCrash = (int.Parse(x.RestartOnCrash) != 0);
            }
            if (x.AdoptIfAlreadyRunning != null)
            {
                a.AdoptIfAlreadyRunning = (int.Parse(x.AdoptIfAlreadyRunning) != 0);
            }
            if (x.PriorityClass != null)
            {
                a.PriorityClass = x.PriorityClass;
            }
            if (x.InitCondition != null)
            {
                a.InitializedCondition = x.InitCondition;
            }
            if (x.SeparationInterval != null)
            {
                a.SeparationInterval = double.Parse(x.SeparationInterval, CultureInfo.InvariantCulture);
            }
            if (x.MinKillingTime != null)
            {
                a.MinKillingTime = double.Parse(x.MinKillingTime, CultureInfo.InvariantCulture);
            }
            if (x.Dependecies != null)
            {
                var deps = new List <string>();
                foreach (var d in x.Dependecies.Split(';'))
                {
                    var stripped = d.Trim();
                    if (stripped != "")
                    {
                        deps.Add(d);
                    }
                }
                a.Dependencies = deps;
            }

            if (x.KillTree != null)
            {
                a.KillTree = (int.Parse(x.KillTree) != 0);
            }

            if (!String.IsNullOrEmpty(x.KillSoftly))
            {
                a.KillSoftly = (int.Parse(x.KillSoftly) != 0);
            }

            if (x.WindowStyle != null)
            {
                if (x.WindowStyle.ToLower() == "minimized")
                {
                    a.WindowStyle = EWindowStyle.Minimized;
                }
                else if (x.WindowStyle.ToLower() == "maximized")
                {
                    a.WindowStyle = EWindowStyle.Maximized;
                }
                else if (x.WindowStyle.ToLower() == "normal")
                {
                    a.WindowStyle = EWindowStyle.Normal;
                }
                else if (x.WindowStyle.ToLower() == "hidden")
                {
                    a.WindowStyle = EWindowStyle.Hidden;
                }
            }

            if (x.WindowPos != null)
            {
                foreach (var elem in x.WindowPos)
                {
                    a.WindowPosXml.Add(elem.ToString());
                }
            }

            if (x.Restarter != null)
            {
                a.RestarterXml = x.Restarter.ToString();
            }

            if (x.SoftKill != null)
            {
                a.SoftKillXml = x.SoftKill.ToString();
            }

            if (x.Env != null)
            {
                foreach (var elem in x.Env.Descendants())
                {
                    if (elem.Name == "Set")
                    {
                        // add/overwite variable
                        var variable = elem.Attribute("Variable")?.Value;
                        var value    = elem.Attribute("Value")?.Value;

                        if (!string.IsNullOrEmpty(variable) && value != null)
                        {
                            a.EnvVarsToSet[variable] = value;
                        }
                    }

                    if (elem.Name == "Local")
                    {
                        // add/overwite variable
                        var variable = elem.Attribute("Variable")?.Value;
                        var value    = elem.Attribute("Value")?.Value;

                        if (!string.IsNullOrEmpty(variable) && value != null)
                        {
                            a.LocalVarsToSet[variable] = value;
                        }
                    }

                    if (elem.Name == "Path")
                    {
                        // extend
                        var toAppend = elem.Attribute("Append")?.Value;
                        if (!string.IsNullOrEmpty(toAppend))
                        {
                            if (String.IsNullOrEmpty(a.EnvVarPathToAppend))
                            {
                                a.EnvVarPathToAppend = toAppend;
                            }
                            else
                            {
                                a.EnvVarPathToAppend = a.EnvVarPathToAppend + ";" + toAppend;
                            }
                        }

                        var toPrepend = elem.Attribute("Prepend")?.Value;
                        if (!string.IsNullOrEmpty(toPrepend))
                        {
                            if (String.IsNullOrEmpty(a.EnvVarPathToPrepend))
                            {
                                a.EnvVarPathToPrepend = toPrepend;
                            }
                            else
                            {
                                a.EnvVarPathToPrepend = toPrepend + ";" + a.EnvVarPathToPrepend;
                            }
                        }
                    }
                }
            }

            if (x.InitDetectors != null)
            {
                foreach (var elem in x.InitDetectors)
                {
                    a.InitDetectors.Add(elem.ToString());
                }
            }

            if (!string.IsNullOrWhiteSpace(x.Groups))
            {
                if (!string.IsNullOrEmpty(a.Groups))
                {
                    a.Groups += ";";
                }
                a.Groups += x.Groups;
            }

            return(a);
        }