protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { // Inputs var command = Command.Get(context); var checkExitCode = CheckExitCode.Get(context); var shellStream = ShellStream.Get(context); var shellExpectedPrompt = ShellExpectedPrompt.Get(context); // Set a timeout on the execution var task = ExecuteWithTimeout(context, cancellationToken); if (await Task.WhenAny(task, Task.Delay(TimeoutMS.Get(context), cancellationToken)) != task) { throw new TimeoutException(Resources.Timeout_Error); } if (task.Exception != null) { throw task.Exception; } // Outputs return((ctx) => { Result.Set(ctx, task.Result); }); }
private async Task <string> ExecuteWithTimeout(AsyncCodeActivityContext context, CancellationToken cancellationToken = default) { ShellStream shellStream; Regex expectedShellPromptRegex; IObjectContainer scopeObjectContainer = context.DataContext.GetProperties()[SSHConnectScope.ParentContainerPropertyTag] .GetValue(context.DataContext) as IObjectContainer; if (ShellStream.Expression == null) { if (scopeObjectContainer == null) { throw new ArgumentException("ShellStream was not provided and cannot be retrieved from the container."); } shellStream = scopeObjectContainer.Get <ShellStream>(); } else { shellStream = ShellStream.Get(context); } if (ShellExpectedPrompt.Expression == null) { if (scopeObjectContainer == null) { throw new ArgumentException("Shell Expected Prompt was not provided and cannot be retrieved from the container."); } expectedShellPromptRegex = scopeObjectContainer.Get <Regex>(); } else { expectedShellPromptRegex = new Regex(ShellExpectedPrompt.Get(context), RegexOptions.Compiled); } return(SSHHelpers.Execute(shellStream, expectedShellPromptRegex, Command.Get(context), TimeSpan.FromMilliseconds(SSHTimeoutMS.Get(context)), CheckExitCode.Get(context))); }