| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b55522a commit 26f9f37
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Diagnostics; | ||
| 3 | + using System.Threading.Tasks; | ||
| 4 | + | ||
| 5 | + namespace MockLambdaRuntime | ||
| 6 | + { | ||
| 7 | + internal static class DebuggerExtensions | ||
| 8 | + { | ||
| 9 | + /// <summary> | ||
| 10 | + /// Tries to wait for the debugger to attach by inspecting <see cref="Debugger.IsAttached"/> property in a loop. | ||
| 11 | + /// </summary> | ||
| 12 | + /// <param name="queryInterval"><see cref="TimeSpan"/> representing the frequency of inspection.</param> | ||
| 13 | + /// <param name="timeout"><see cref="TimeSpan"/> representing the timeout for the operation.</param> | ||
| 14 | + /// <returns><c>True</c> if debugger was attached, false if timeout occured.</returns> | ||
| 15 | + public static bool TryWaitForAttaching(TimeSpan queryInterval, TimeSpan timeout) | ||
| 16 | + { | ||
| 17 | + var stopwatch = Stopwatch.StartNew(); | ||
| 18 | + | ||
| 19 | + while (!Debugger.IsAttached) | ||
| 20 | + { | ||
| 21 | + if (stopwatch.Elapsed > timeout) | ||
| 22 | + { | ||
| 23 | + return false; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + Task.Delay(queryInterval).Wait(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + return true; | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | + using System.Collections.Generic; | ||
| 2 | 3 | using System.IO; | |
| 3 | 4 | using System.Reflection; | |
| 4 | 5 | using System.Runtime.Loader; | |
@@ -9,50 +10,79 @@ namespace MockLambdaRuntime | |||
| 9 | 10 | { | |
| 10 | 11 | class Program | |
| 11 | 12 | { | |
| 13 | + private const string WaitForDebuggerFlag = "--debugger-spin-wait"; | ||
| 14 | + private const bool WaitForDebuggerFlagDefaultValue = false; | ||
| 15 | + | ||
| 12 | 16 | /// Task root of lambda task | |
| 13 | 17 | static string lambdaTaskRoot = EnvHelper.GetOrDefault("LAMBDA_TASK_ROOT", "/var/task"); | |
| 14 | 18 | ||
| 19 | + private static readonly TimeSpan _debuggerStatusQueryInterval = TimeSpan.FromMilliseconds(50); | ||
| 20 | + private static readonly TimeSpan _debuggerStatusQueryTimeout = TimeSpan.FromMinutes(10); | ||
| 21 | + | ||
| 15 | 22 | /// Program entry point | |
| 16 | 23 | static void Main(string[] args) | |
| 17 | 24 | { | |
| 18 | 25 | AssemblyLoadContext.Default.Resolving += OnAssemblyResolving; | |
| 19 | 26 | ||
| 20 | - var handler = GetFunctionHandler(args); | ||
| 21 | - var body = GetEventBody(args); | ||
| 22 | - | ||
| 23 | - var lambdaContext = new MockLambdaContext(handler, body); | ||
| 24 | - | ||
| 25 | - var userCodeLoader = new UserCodeLoader(handler, InternalLogger.NO_OP_LOGGER); | ||
| 26 | - userCodeLoader.Init(Console.Error.WriteLine); | ||
| 27 | - | ||
| 28 | - var lambdaContextInternal = new LambdaContextInternal(lambdaContext.RemainingTime, | ||
| 29 | - LogAction, new Lazy<CognitoClientContextInternal>(), | ||
| 30 | - lambdaContext.RequestId, | ||
| 31 | - new Lazy<string>(lambdaContext.Arn), | ||
| 32 | - new Lazy<string>(string.Empty), | ||
| 33 | - new Lazy<string>(string.Empty), | ||
| 34 | - Environment.GetEnvironmentVariables()); | ||
| 35 | - | ||
| 36 | - Exception lambdaException = null; | ||
| 37 | - | ||
| 38 | - LogRequestStart(lambdaContext); | ||
| 39 | 27 | try | |
| 40 | 28 | { | |
| 41 | - userCodeLoader.Invoke(lambdaContext.InputStream, lambdaContext.OutputStream, lambdaContextInternal); | ||
| 29 | + var shouldWaitForDebugger = GetShouldWaitForDebuggerFlag(args, out var positionalArgs); | ||
| 30 | + | ||
| 31 | + var handler = GetFunctionHandler(positionalArgs); | ||
| 32 | + var body = GetEventBody(positionalArgs); | ||
| 33 | + | ||
| 34 | + if (shouldWaitForDebugger) | ||
| 35 | + { | ||
| 36 | + Console.Error.WriteLine("Waiting for the debugger to attach..."); | ||
| 37 | + | ||
| 38 | + if (!DebuggerExtensions.TryWaitForAttaching( | ||
| 39 | + _debuggerStatusQueryInterval, | ||
| 40 | + _debuggerStatusQueryTimeout)) | ||
| 41 | + { | ||
| 42 | + Console.Error.WriteLine("Timeout. Proceeding without debugger."); | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + var lambdaContext = new MockLambdaContext(handler, body); | ||
| 47 | + | ||
| 48 | + var userCodeLoader = new UserCodeLoader(handler, InternalLogger.NO_OP_LOGGER); | ||
| 49 | + userCodeLoader.Init(Console.Error.WriteLine); | ||
| 50 | + | ||
| 51 | + var lambdaContextInternal = new LambdaContextInternal(lambdaContext.RemainingTime, | ||
| 52 | + LogAction, new Lazy<CognitoClientContextInternal>(), | ||
| 53 | + lambdaContext.RequestId, | ||
| 54 | + new Lazy<string>(lambdaContext.Arn), | ||
| 55 | + new Lazy<string>(string.Empty), | ||
| 56 | + new Lazy<string>(string.Empty), | ||
| 57 | + Environment.GetEnvironmentVariables()); | ||
| 58 | + | ||
| 59 | + Exception lambdaException = null; | ||
| 60 | + | ||
| 61 | + LogRequestStart(lambdaContext); | ||
| 62 | + try | ||
| 63 | + { | ||
| 64 | + userCodeLoader.Invoke(lambdaContext.InputStream, lambdaContext.OutputStream, lambdaContextInternal); | ||
| 65 | + } | ||
| 66 | + catch (Exception ex) | ||
| 67 | + { | ||
| 68 | + lambdaException = ex; | ||
| 69 | + } | ||
| 70 | + LogRequestEnd(lambdaContext); | ||
| 71 | + | ||
| 72 | + if (lambdaException == null) | ||
| 73 | + { | ||
| 74 | + Console.WriteLine(lambdaContext.OutputText); | ||
| 75 | + } | ||
| 76 | + else | ||
| 77 | + { | ||
| 78 | + Console.Error.WriteLine(lambdaException); | ||
| 79 | + } | ||
| 42 | 80 | } | |
| 43 | - catch (Exception ex) | ||
| 44 | - { | ||
| 45 | - lambdaException = ex; | ||
| 46 | - } | ||
| 47 | - LogRequestEnd(lambdaContext); | ||
| 48 | 81 | ||
| 49 | - if (lambdaException == null) | ||
| 50 | - { | ||
| 51 | - Console.WriteLine(lambdaContext.OutputText); | ||
| 52 | - } | ||
| 53 | - else | ||
| 82 | + // Catch all unhandled exceptions from runtime, to prevent user from hanging on them while debugging | ||
| 83 | + catch (Exception ex) | ||
| 54 | 84 | { | |
| 55 | - Console.Error.WriteLine(lambdaException); | ||
| 85 | + Console.Error.WriteLine($"\nUnhandled exception occured in runner:\n{ex}"); | ||
| 56 | 86 | } | |
| 57 | 87 | } | |
| 58 | 88 | ||
@@ -68,6 +98,33 @@ private static void LogAction(string text) | |||
| 68 | 98 | Console.Error.WriteLine(text); | |
| 69 | 99 | } | |
| 70 | 100 | ||
| 101 | + /// <summary> | ||
| 102 | + /// Extracts "waitForDebugger" flag from args. Returns other unprocessed arguments. | ||
| 103 | + /// </summary> | ||
| 104 | + /// <param name="args">Args to look through</param> | ||
| 105 | + /// <param name="unprocessed">Arguments except for the "waitForDebugger" ones</param> | ||
| 106 | + /// <returns>"waitForDebugger" flag value</returns> | ||
| 107 | + private static bool GetShouldWaitForDebuggerFlag(string[] args, out string[] unprocessed) | ||
| 108 | + { | ||
| 109 | + var flagValue = WaitForDebuggerFlagDefaultValue; | ||
| 110 | + | ||
| 111 | + var unprocessedList = new List<string>(); | ||
| 112 | + | ||
| 113 | + foreach (var argument in args) | ||
| 114 | + { | ||
| 115 | + if (argument == WaitForDebuggerFlag) | ||
| 116 | + { | ||
| 117 | + flagValue = true; | ||
| 118 | + continue; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + unprocessedList.Add(argument); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + unprocessed = unprocessedList.ToArray(); | ||
| 125 | + return flagValue; | ||
| 126 | + } | ||
| 127 | + | ||
| 71 | 128 | static void LogRequestStart(MockLambdaContext context) | |
| 72 | 129 | { | |
| 73 | 130 | Console.Error.WriteLine($"START RequestId: {context.RequestId} Version: {context.FunctionVersion}"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Diagnostics; | ||
| 3 | + using System.Threading.Tasks; | ||
| 4 | + | ||
| 5 | + namespace MockLambdaRuntime | ||
| 6 | + { | ||
| 7 | + internal static class DebuggerExtensions | ||
| 8 | + { | ||
| 9 | + /// <summary> | ||
| 10 | + /// Tries to wait for the debugger to attach by inspecting <see cref="Debugger.IsAttached"/> property in a loop. | ||
| 11 | + /// </summary> | ||
| 12 | + /// <param name="queryInterval"><see cref="TimeSpan"/> representing the frequency of inspection.</param> | ||
| 13 | + /// <param name="timeout"><see cref="TimeSpan"/> representing the timeout for the operation.</param> | ||
| 14 | + /// <returns><c>True</c> if debugger was attached, false if timeout occured.</returns> | ||
| 15 | + public static bool TryWaitForAttaching(TimeSpan queryInterval, TimeSpan timeout) | ||
| 16 | + { | ||
| 17 | + var stopwatch = Stopwatch.StartNew(); | ||
| 18 | + | ||
| 19 | + while (!Debugger.IsAttached) | ||
| 20 | + { | ||
| 21 | + if (stopwatch.Elapsed > timeout) | ||
| 22 | + { | ||
| 23 | + return false; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + Task.Delay(queryInterval).Wait(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + return true; | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | + using System.Collections.Generic; | ||
| 2 | 3 | using System.IO; | |
| 3 | 4 | using System.Reflection; | |
| 4 | 5 | using System.Runtime.Loader; | |
@@ -9,50 +10,79 @@ namespace MockLambdaRuntime | |||
| 9 | 10 | { | |
| 10 | 11 | class Program | |
| 11 | 12 | { | |
| 13 | + private const string WaitForDebuggerFlag = "--debugger-spin-wait"; | ||
| 14 | + private const bool WaitForDebuggerFlagDefaultValue = false; | ||
| 15 | + | ||
| 12 | 16 | /// Task root of lambda task | |
| 13 | 17 | static string lambdaTaskRoot = EnvHelper.GetOrDefault("LAMBDA_TASK_ROOT", "/var/task"); | |
| 14 | 18 | ||
| 19 | + private static readonly TimeSpan _debuggerStatusQueryInterval = TimeSpan.FromMilliseconds(50); | ||
| 20 | + private static readonly TimeSpan _debuggerStatusQueryTimeout = TimeSpan.FromMinutes(10); | ||
| 21 | + | ||
| 15 | 22 | /// Program entry point | |
| 16 | 23 | static void Main(string[] args) | |
| 17 | 24 | { | |
| 18 | 25 | AssemblyLoadContext.Default.Resolving += OnAssemblyResolving; | |
| 19 | 26 | ||
| 20 | - var handler = GetFunctionHandler(args); | ||
| 21 | - var body = GetEventBody(args); | ||
| 22 | - | ||
| 23 | - var lambdaContext = new MockLambdaContext(handler, body); | ||
| 24 | - | ||
| 25 | - var userCodeLoader = new UserCodeLoader(handler, InternalLogger.NO_OP_LOGGER); | ||
| 26 | - userCodeLoader.Init(Console.Error.WriteLine); | ||
| 27 | - | ||
| 28 | - var lambdaContextInternal = new LambdaContextInternal(lambdaContext.RemainingTime, | ||
| 29 | - LogAction, new Lazy<CognitoClientContextInternal>(), | ||
| 30 | - lambdaContext.RequestId, | ||
| 31 | - new Lazy<string>(lambdaContext.Arn), | ||
| 32 | - new Lazy<string>(string.Empty), | ||
| 33 | - new Lazy<string>(string.Empty), | ||
| 34 | - Environment.GetEnvironmentVariables()); | ||
| 35 | - | ||
| 36 | - Exception lambdaException = null; | ||
| 37 | - | ||
| 38 | - LogRequestStart(lambdaContext); | ||
| 39 | 27 | try | |
| 40 | 28 | { | |
| 41 | - userCodeLoader.Invoke(lambdaContext.InputStream, lambdaContext.OutputStream, lambdaContextInternal); | ||
| 29 | + var shouldWaitForDebugger = GetShouldWaitForDebuggerFlag(args, out var positionalArgs); | ||
| 30 | + | ||
| 31 | + var handler = GetFunctionHandler(positionalArgs); | ||
| 32 | + var body = GetEventBody(positionalArgs); | ||
| 33 | + | ||
| 34 | + if (shouldWaitForDebugger) | ||
| 35 | + { | ||
| 36 | + Console.Error.WriteLine("Waiting for the debugger to attach..."); | ||
| 37 | + | ||
| 38 | + if (!DebuggerExtensions.TryWaitForAttaching( | ||
| 39 | + _debuggerStatusQueryInterval, | ||
| 40 | + _debuggerStatusQueryTimeout)) | ||
| 41 | + { | ||
| 42 | + Console.Error.WriteLine("Timeout. Proceeding without debugger."); | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + var lambdaContext = new MockLambdaContext(handler, body); | ||
| 47 | + | ||
| 48 | + var userCodeLoader = new UserCodeLoader(handler, InternalLogger.NO_OP_LOGGER); | ||
| 49 | + userCodeLoader.Init(Console.Error.WriteLine); | ||
| 50 | + | ||
| 51 | + var lambdaContextInternal = new LambdaContextInternal(lambdaContext.RemainingTime, | ||
| 52 | + LogAction, new Lazy<CognitoClientContextInternal>(), | ||
| 53 | + lambdaContext.RequestId, | ||
| 54 | + new Lazy<string>(lambdaContext.Arn), | ||
| 55 | + new Lazy<string>(string.Empty), | ||
| 56 | + new Lazy<string>(string.Empty), | ||
| 57 | + Environment.GetEnvironmentVariables()); | ||
| 58 | + | ||
| 59 | + Exception lambdaException = null; | ||
| 60 | + | ||
| 61 | + LogRequestStart(lambdaContext); | ||
| 62 | + try | ||
| 63 | + { | ||
| 64 | + userCodeLoader.Invoke(lambdaContext.InputStream, lambdaContext.OutputStream, lambdaContextInternal); | ||
| 65 | + } | ||
| 66 | + catch (Exception ex) | ||
| 67 | + { | ||
| 68 | + lambdaException = ex; | ||
| 69 | + } | ||
| 70 | + LogRequestEnd(lambdaContext); | ||
| 71 | + | ||
| 72 | + if (lambdaException == null) | ||
| 73 | + { | ||
| 74 | + Console.WriteLine(lambdaContext.OutputText); | ||
| 75 | + } | ||
| 76 | + else | ||
| 77 | + { | ||
| 78 | + Console.Error.WriteLine(lambdaException); | ||
| 79 | + } | ||
| 42 | 80 | } | |
| 43 | - catch (Exception ex) | ||
| 44 | - { | ||
| 45 | - lambdaException = ex; | ||
| 46 | - } | ||
| 47 | - LogRequestEnd(lambdaContext); | ||
| 48 | 81 | ||
| 49 | - if (lambdaException == null) | ||
| 50 | - { | ||
| 51 | - Console.WriteLine(lambdaContext.OutputText); | ||
| 52 | - } | ||
| 53 | - else | ||
| 82 | + // Catch all unhandled exceptions from runtime, to prevent user from hanging on them while debugging | ||
| 83 | + catch (Exception ex) | ||
| 54 | 84 | { | |
| 55 | - Console.Error.WriteLine(lambdaException); | ||
| 85 | + Console.Error.WriteLine($"\nUnhandled exception occured in runner:\n{ex}"); | ||
| 56 | 86 | } | |
| 57 | 87 | } | |
| 58 | 88 | ||
@@ -68,6 +98,33 @@ private static void LogAction(string text) | |||
| 68 | 98 | Console.Error.WriteLine(text); | |
| 69 | 99 | } | |
| 70 | 100 | ||
| 101 | + /// <summary> | ||
| 102 | + /// Extracts "waitForDebugger" flag from args. Returns other unprocessed arguments. | ||
| 103 | + /// </summary> | ||
| 104 | + /// <param name="args">Args to look through</param> | ||
| 105 | + /// <param name="unprocessed">Arguments except for the "waitForDebugger" ones</param> | ||
| 106 | + /// <returns>"waitForDebugger" flag value</returns> | ||
| 107 | + private static bool GetShouldWaitForDebuggerFlag(string[] args, out string[] unprocessed) | ||
| 108 | + { | ||
| 109 | + var flagValue = WaitForDebuggerFlagDefaultValue; | ||
| 110 | + | ||
| 111 | + var unprocessedList = new List<string>(); | ||
| 112 | + | ||
| 113 | + foreach (var argument in args) | ||
| 114 | + { | ||
| 115 | + if (argument == WaitForDebuggerFlag) | ||
| 116 | + { | ||
| 117 | + flagValue = true; | ||
| 118 | + continue; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + unprocessedList.Add(argument); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + unprocessed = unprocessedList.ToArray(); | ||
| 125 | + return flagValue; | ||
| 126 | + } | ||
| 127 | + | ||
| 71 | 128 | static void LogRequestStart(MockLambdaContext context) | |
| 72 | 129 | { | |
| 73 | 130 | Console.Error.WriteLine($"START RequestId: {context.RequestId} Version: {context.FunctionVersion}"); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments