-
Notifications
You must be signed in to change notification settings - Fork 113
add an option to start the local debugging server based on an env variable #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
18a8bb7
c761c44
83861da
f3dc137
4115008
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,36 +97,55 @@ public enum Lambda { | |
// for testing and internal use | ||
@discardableResult | ||
internal static func run(configuration: Configuration = .init(), factory: @escaping HandlerFactory) -> Result<Int, Error> { | ||
Backtrace.install() | ||
var logger = Logger(label: "Lambda") | ||
logger.logLevel = configuration.general.logLevel | ||
let _run = { (configuration: Configuration, factory: @escaping HandlerFactory) -> Result<Int, Error> in | ||
Backtrace.install() | ||
var logger = Logger(label: "Lambda") | ||
logger.logLevel = configuration.general.logLevel | ||
|
||
var result: Result<Int, Error>! | ||
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { eventLoop in | ||
let lifecycle = Lifecycle(eventLoop: eventLoop, logger: logger, configuration: configuration, factory: factory) | ||
#if DEBUG | ||
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in | ||
logger.info("intercepted signal: \(signal)") | ||
lifecycle.shutdown() | ||
} | ||
#endif | ||
|
||
lifecycle.start().flatMap { | ||
lifecycle.shutdownFuture | ||
}.whenComplete { lifecycleResult in | ||
var result: Result<Int, Error>! | ||
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { eventLoop in | ||
let lifecycle = Lifecycle(eventLoop: eventLoop, logger: logger, configuration: configuration, factory: factory) | ||
#if DEBUG | ||
signalSource.cancel() | ||
let signalSource = trap(signal: configuration.lifecycle.stopSignal) { signal in | ||
logger.info("intercepted signal: \(signal)") | ||
lifecycle.shutdown() | ||
} | ||
#endif | ||
eventLoop.shutdownGracefully { error in | ||
if let error = error { | ||
preconditionFailure("Failed to shutdown eventloop: \(error)") | ||
|
||
lifecycle.start().flatMap { | ||
lifecycle.shutdownFuture | ||
}.whenComplete { lifecycleResult in | ||
#if DEBUG | ||
signalSource.cancel() | ||
#endif | ||
eventLoop.shutdownGracefully { error in | ||
if let error = error { | ||
preconditionFailure("Failed to shutdown eventloop: \(error)") | ||
} | ||
} | ||
result = lifecycleResult | ||
} | ||
result = lifecycleResult | ||
} | ||
|
||
logger.info("shutdown completed") | ||
return result | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no changes here ^^, just setting a |
||
|
||
logger.info("shutdown completed") | ||
return result | ||
// start local server for debugging in DEBUG mode only | ||
#if DEBUG | ||
if Lambda.env("LOCAL_SERVER_ENABLED").flatMap(Bool.init) ?? false { | ||
do { | ||
return try Lambda.withLocalServer { | ||
_run(configuration, factory) | ||
} | ||
} catch { | ||
return .failure(error) | ||
} | ||
} else { | ||
return _run(configuration, factory) | ||
} | ||
#else | ||
return _run(configuration, factory) | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the actual change 👀 hopefully this does not feel too "dangerous" since it behind both |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so I've internalized the feedback:
yet still, looking tat this has me worried. It's a bit too magic... some magical variable etc...
So... Is there another way? I think there might be, what about this:let isDebugMode = debugModeInDebugBuilds && _isDebugAssertConfiguration() // this exists in all SwiftsGains:no flags to set, just rely on "debug vs release build" semanticsnoone should ever run debug builds on lambda anywaypossible to override if one wants toWDYT? @tomerd @fabianfettThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ktoso
iiuc the
if #DEBUG
as implemented in this PR gives you the same results as theisDebugMode
line above. the PR also adds an env variable gate as extra caution so if people make a mistake and deploy a debug version to aws its not enabled which would be very bad! it makes things very explicit.The reason I landed on an env variable gate is that it is something you can turn on/off in Xcode easily - which is the goal of this feature: the ability to debug Lambda locally in the IDE.
cc @fabianfett
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so @fabianfett and myself spoke a but and he pointed out that most iOS devs will not know how to set env variables in Xcode which means this is not a great solution for them. however, we can potentially do something more horrible which is leaning on an env variable Xcode itself sets likeXCODE_VERSION_ACTUAL
which will make this only work in Xcode unless the developers decides to use this env variables somewhere else which is highly unlikelymeh this won't work - those env variables are not exposed to the targets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another protection we can do is check for parent pid and only allow this if parent != 1 which normally means debugger, but need to further research if viable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Scratch my entire idea about _isDebugAssertConfiguration is devolves into the same as the
#if debug
stuff.To be honest not in love with any of the automatic ways... hope you can figure out something that makes folks happy 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spoke with an Xcode core engineer, and he recommended the env variable way. will double check with a few others too