protected internal override void WriteLocalWrapperScript(Path launchDst, Path pidFile
                                                                     , TextWriter pout)
            {
                string exitCodeFile = ContainerLaunch.GetExitCodeFile(pidFile.ToString());
                string tmpFile      = exitCodeFile + ".tmp";

                pout.WriteLine("#!/usr/bin/env bash");
                pout.WriteLine("bash \"" + this.sessionScriptPath.ToString() + "\"");
                pout.WriteLine("rc=$?");
                pout.WriteLine("echo $rc > \"" + tmpFile + "\"");
                pout.WriteLine("mv -f \"" + tmpFile + "\" \"" + exitCodeFile + "\"");
                pout.WriteLine("exit $rc");
            }
Esempio n. 2
0
        /// <summary>Recover an already existing container.</summary>
        /// <remarks>
        /// Recover an already existing container. This is a blocking call and returns
        /// only when the container exits.  Note that the container must have been
        /// activated prior to this call.
        /// </remarks>
        /// <param name="user">the user of the container</param>
        /// <param name="containerId">The ID of the container to reacquire</param>
        /// <returns>The exit code of the pre-existing container</returns>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"></exception>
        public virtual int ReacquireContainer(string user, ContainerId containerId)
        {
            Path pidPath = GetPidFilePath(containerId);

            if (pidPath == null)
            {
                Log.Warn(containerId + " is not active, returning terminated error");
                return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
            }
            string pid = null;

            pid = ProcessIdFileReader.GetProcessId(pidPath);
            if (pid == null)
            {
                throw new IOException("Unable to determine pid for " + containerId);
            }
            Log.Info("Reacquiring " + containerId + " with pid " + pid);
            while (IsContainerProcessAlive(user, pid))
            {
                Sharpen.Thread.Sleep(1000);
            }
            // wait for exit code file to appear
            string   exitCodeFile = ContainerLaunch.GetExitCodeFile(pidPath.ToString());
            FilePath file         = new FilePath(exitCodeFile);
            int      sleepMsec    = 100;
            int      msecLeft     = 2000;

            while (!file.Exists() && msecLeft >= 0)
            {
                if (!IsContainerActive(containerId))
                {
                    Log.Info(containerId + " was deactivated");
                    return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
                }
                Sharpen.Thread.Sleep(sleepMsec);
                msecLeft -= sleepMsec;
            }
            if (msecLeft < 0)
            {
                throw new IOException("Timeout while waiting for exit code from " + containerId);
            }
            try
            {
                return(System.Convert.ToInt32(FileUtils.ReadFileToString(file).Trim()));
            }
            catch (FormatException e)
            {
                throw new IOException("Error parsing exit code from pid " + pid, e);
            }
        }