protected void ResetSessionWorker(RemoteCodeSession session, WorkerResetReason reason)
 {
     Logger.LogLine($"Worker Reset: {session.SessionId}");
     session.WorkerClient?.Close();
     Logger.LogLine($"Worker Reset: closed worker socket of {session.SessionId}");
     try
     {
         if (session.WorkerProcess?.HasExited == false)
         {
             session.WorkerProcess?.Kill();
             Logger.LogLine($"Worker Reset: killed worker process of {session.SessionId}");
             //notify client of worker destruction
             if (reason == WorkerResetReason.Expired &&
                 //don't notify if worker is no longer executing user code
                 session.LastAppState != RemoteAppState.NotRunning &&
                 session.LastAppState != RemoteAppState.Ended &&
                 session.LastAppState != RemoteAppState.Crashed
                 )
             {
                 clientService.SendExecutionState(session.SessionId, new ExecutionStateDto
                 {
                     SessionId = session.SessionId,
                     State     = RemoteAppState.Crashed,
                     Exception = ExceptionDto.FromException(new TimeoutException(
                                                                $"Your application takes longer than {SessionConstants.SessionIdleTimeout} seconds to execute and has been terminated."))
                 });
             }
         }
     }
     finally
     {
         session.WorkerClient?.Dispose();
         session.WorkerProcess?.Dispose();
         session.WorkerProcess = null;
         session.WorkerClient  = null;
     }
 }
Пример #2
0
        public void RunApplication(string sessionid, TcpClient client, byte[] assemblyBytes)
        {
            var networkStream    = client.GetStream();
            var outputRedirector = new ConsoleOutputService(sessionid, client);
            var inputRedirector  = new ConsoleInputService(sessionid, client);

            ExecutionStateDto execState;
            var assembly = Assembly.Load(assemblyBytes);

            try
            {
                execState = new ExecutionStateDto
                {
                    SessionId = sessionid,
                    State     = RemoteAppState.Running
                };
                networkStream.WriteByte((byte)MessageType.WorkerExecutionState);
                Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32);
                Logger.LogLine($"CLIENT: sent execution state {execState.State}");

                //redirect console
                Console.SetOut(outputRedirector);
                Console.SetIn(inputRedirector);

                //invoke main method
                var mainParms = assembly.EntryPoint.GetParameters();
                if (mainParms.Count() == 0)
                {
                    assembly.EntryPoint.Invoke(null, null);
                }
                else
                {
                    if (mainParms[0].ParameterType == typeof(string[]))
                    {
                        assembly.EntryPoint.Invoke(null, new string[] { null });
                    }
                    else
                    {
                        assembly.EntryPoint.Invoke(null, null);
                    }
                }

                //reset console redirection
                Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
                {
                    AutoFlush = true
                });
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));

                execState = new ExecutionStateDto
                {
                    SessionId = sessionid,
                    State     = RemoteAppState.Ended
                };
                networkStream.WriteByte((byte)MessageType.WorkerExecutionState);
                Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32);
                Logger.LogLine($"CLIENT: sent execution state {execState.State}");
            }
            catch (SocketException socketEx)
            {
                Logger.LogLine($"CLIENT Error: {socketEx.Message}");
            }
            catch (Exception ex)
            {
                execState = new ExecutionStateDto
                {
                    SessionId = sessionid,
                    State     = RemoteAppState.Crashed,
                    Exception = ExceptionDto.FromException(ex)
                };
                networkStream.WriteByte((byte)MessageType.WorkerExecutionState);
                Serializer.SerializeWithLengthPrefix(networkStream, execState, PrefixStyle.Fixed32);
            }
            finally
            {
            }
        }