/// <summary>
        /// Launches a local app if not already running.
        /// </summary>
        /// <param name="appIdTuple"></param>
        public void  LaunchApp(AppIdTuple appIdTuple)
        {
            if (!(localApps.ContainsKey(appIdTuple)))
            {
                throw new NotALocalApp(appIdTuple, machineId);
            }

            var la       = localApps[appIdTuple];
            var appState = appsState[la.AppDef.AppIdTuple];

            // don't do anything if the app is already running
            if (la.launcher != null && la.launcher.Running)
            {
                return;
            }

            log.DebugFormat("Launching app {0}", la.AppDef.AppIdTuple);


            // launch the application
            appState.Started     = false;
            appState.StartFailed = false;
            appState.Killed      = false;

            la.watchers.Clear();

            la.launcher = launcherFactory.createLauncher(la.AppDef, rootForRelativePaths);

            try
            {
                la.launcher.Launch();
                appState.Started     = true;
                appState.Initialized = true; // a watcher can set it to false upon its creation if it works like an AppInitDetector

                //// instantiate app watchers
                //foreach( var watcherDef in la.AppDef.Watchers )
                //{
                //    var w = appWatcherFactory.create( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, watcherDef);
                //    la.watchers.Add( w );
                //}

                // instantiate init detector (also a watcher)
                {
                    // compatibility with InitialCondition="timeout 2.0" ... convert to XML definition <timeout>2.0</timeout>
                    {
                        if (!string.IsNullOrEmpty(la.AppDef.InitializedCondition))
                        {
                            string name;
                            string args;
                            AppInitializedDetectorFactory.ParseDefinitionString(la.AppDef.InitializedCondition, out name, out args);
                            string xmlString = string.Format("<{0}>{1}</{0}>", name, args);

                            var aid = appAppInitializedDetectorFactory.create(la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xmlString));
                            log.DebugFormat("Adding InitDetector {0}, pid {1}", aid.GetType().Name, la.launcher.ProcessId);
                            la.watchers.Add(aid);
                        }
                    }

                    foreach (var xml in la.AppDef.InitDetectors)
                    {
                        var aid = appAppInitializedDetectorFactory.create(la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xml));

                        log.DebugFormat("Adding InitDetector {0}, pid {1}", aid.GetType().Name, la.launcher.ProcessId);
                        la.watchers.Add(aid);
                    }
                }

                // instantiate window positioners
                foreach (var xml in la.AppDef.WindowPosXml)
                {
                    log.DebugFormat("Adding WindowsPositioner, pid {0}", la.launcher.ProcessId);
                    var wpo = new WindowPositioner(la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xml));
                    la.watchers.Add(wpo);
                }

                // instantiate autorestarter
                if (la.AppDef.RestartOnCrash)
                {
                    log.DebugFormat("Adding AutoRestarter, pid {0}", la.launcher.ProcessId);
                    var ar = new AutoRestarter(la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, new XElement("Autorestart"));
                    la.watchers.Add(ar);
                }
            }
            catch (Exception ex)  // app launching failed
            {
                log.ErrorFormat("Exception: App \"{0}\"start failure {1}", la.AppDef.AppIdTuple, ex);

                appState.StartFailed = true;
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Launches a local app if not already running.
        /// </summary>
        /// <param name="appIdTuple"></param>
        public void LaunchApp(AppIdTuple appIdTuple)
        {
            if( !(localApps.ContainsKey(appIdTuple) ))
            {
                throw new NotALocalApp(appIdTuple, machineId);
            }

            var la = localApps[appIdTuple];
            var appState = appsState[la.AppDef.AppIdTuple];

            // don't do anything if the app is already running
            if( la.launcher != null && la.launcher.Running )
            {
                return;
            }

            log.DebugFormat("Launching app {0}", la.AppDef.AppIdTuple);

            // launch the application
            appState.Started = false;
            appState.StartFailed = false;
            appState.Killed = false;

            la.watchers.Clear();

            la.launcher = launcherFactory.createLauncher( la.AppDef );

            try
            {
                la.launcher.Launch();
                appState.Started = true;
                appState.Initialized = true; // a watcher can set it to false upon its creation if it works like an AppInitDetector

                //// instantiate app watchers
                //foreach( var watcherDef in la.AppDef.Watchers )
                //{
                //    var w = appWatcherFactory.create( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, watcherDef);
                //    la.watchers.Add( w );
                //}

                // instantiate init detector (also a watcher)
                {
                    // compatibility with InitialCondition="timeout 2.0" ... convert to XML definition <timeout>2.0</timeout>
                    {
                        if( !string.IsNullOrEmpty( la.AppDef.InitializedCondition ) )
                        {
                            string name;
                            string args;
                            AppInitializedDetectorFactory.ParseDefinitionString( la.AppDef.InitializedCondition, out name, out args );
                            string xmlString = string.Format("<{0}>{1}</{0}>", name, args);

                            var aid = appAppInitializedDetectorFactory.create( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xmlString) );
                            log.DebugFormat("Adding InitDetector {0}, pid {1}", aid.GetType().Name, la.launcher.ProcessId );
                            la.watchers.Add( aid );
                        }
                    }

                    foreach( var xml in la.AppDef.InitDetectors )
                    {
                        var aid = appAppInitializedDetectorFactory.create( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xml));

                        log.DebugFormat("Adding InitDetector {0}, pid {1}", aid.GetType().Name, la.launcher.ProcessId );
                        la.watchers.Add( aid );
                    }
                }

                // instantiate window positioners
                foreach( var xml in la.AppDef.WindowPosXml )
                {
                    log.DebugFormat("Adding WindowsPositioner, pid {0}", la.launcher.ProcessId );
                    var wpo = new WindowPositioner( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, XElement.Parse(xml) );
                    la.watchers.Add( wpo );
                }

                // instantiate autorestarter
                if( la.AppDef.RestartOnCrash )
                {
                    log.DebugFormat("Adding AutoRestarter, pid {0}", la.launcher.ProcessId );
                    var ar = new AutoRestarter( la.AppDef, appsState[appIdTuple], la.launcher.ProcessId, new XElement("Autorestart") );
                    la.watchers.Add( ar );
                }

            }
            catch( Exception ex ) // app launching failed
            {
                log.ErrorFormat("Exception: App \"{0}\"start failure {1}", la.AppDef.AppIdTuple, ex);

                appState.StartFailed = true;
                throw;
            }
        }