public void Dispose(bool useTaskkill) { lock (this) { if (_status == SandboxStatus.Exited || _status == SandboxStatus.ShuttingDown) { return; } _status = SandboxStatus.ShuttingDown; } try { if (!_process.HasExited) { Log.Information("Terminating command line"); _process.CancelErrorRead(); _process.CancelOutputRead(); _process.Taskkill(false, useTaskkill, 5); } //in case our command is still waiting _endOfCommandEvent.Set(); _endOfCommandEvent.Dispose(); _process.Dispose(); _buffer.Complete(); } catch (Exception e) { Log.Error("Error while shutting down process {0}: {1}", _process.ProcessName, e.Message); } _status = SandboxStatus.Exited; }
public override async Task <SandboxStatus> GetSandboxStatusAsync(string clientKey) { using (var conn = CreateConnection()) { var results = await conn.QueryAsync <SandboxStatus>( $"SELECT datname as Name, 0 as Code, 'ONLINE' Description FROM pg_database WHERE datname = \'{_databaseNameBuilder.SandboxNameForKey(clientKey)}\';", commandTimeout : CommandTimeout) .ConfigureAwait(false); return(results.SingleOrDefault() ?? SandboxStatus.ErrorStatus()); } }
public override async Task <SandboxStatus> GetSandboxStatusAsync(string clientKey) { using (var conn = CreateConnection()) { var results = await conn.QueryAsync <SandboxStatus>( $"SELECT name as Name, state as Code, state_desc as Description FROM sys.databases WHERE name = @DbName;", new { DbName = _databaseNameBuilder.SandboxNameForKey(clientKey) }, commandTimeout : CommandTimeout) .ConfigureAwait(false); return(results.SingleOrDefault() ?? SandboxStatus.ErrorStatus()); } }
public CommandLineSandbox(string startupWorkingDirectory = null) { var si = _process.StartInfo; si.FileName = "cmd"; if (startupWorkingDirectory != null) { si.WorkingDirectory = startupWorkingDirectory; } si.UseShellExecute = false; si.RedirectStandardInput = true; si.RedirectStandardOutput = true; si.RedirectStandardError = true; si.CreateNoWindow = true; si.Arguments = "/k"; _process.EnableRaisingEvents = true; _process.Exited += (s, e) => { Dispose(); }; _process.Start(); _process.ErrorDataReceived += (sender, e) => _buffer.Post(new ShellOutput(e.Data, ShellOutputType.Error)); _process.BeginErrorReadLine(); _process.OutputDataReceived += (sender, e) => _buffer.Post(new ShellOutput(e.Data)); _process.BeginOutputReadLine(); ExecutionDataflowBlockOptions options = new ExecutionDataflowBlockOptions() { SingleProducerConstrained = false }; _actionBlock = new ActionBlock <ShellOutput>((Action <ShellOutput>)OutputReceived, options); DataflowLinkOptions flowOptions = new DataflowLinkOptions() { PropagateCompletion = true }; _buffer.LinkTo(_actionBlock, flowOptions); //Don't remember the IDisposable, we do not need to unlink _status = SandboxStatus.Idle; }
public CommandResult ExecuteCommand(string command) { lock (this) { if (_status != SandboxStatus.Idle) { throw new InvalidOperationException("The current command line is not in idle state."); } _status = SandboxStatus.Busy; } CommandResult result = new CommandResult() { Command = command }; SendCommand(command); lock (this) { if (_status != SandboxStatus.Busy) { throw new TaskCanceledException("The process ended during command execution"); } result.ReturnCode = _lastErrorLevel; if (_lastErrorLevel != 0) { ResetErrorLevel(); } result.ErrorCount = _errorCount; _lastErrorLevel = 0; _errorCount = 0; _status = SandboxStatus.Idle; _awaitingResult = false; return(result); } }