TimeoutHandlerWrapper for graceful timeout management in handlers #1983
fredericgboutin-yapla
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I just want to share this little handler wrapper that I created like... 2 years ago. We are using it in production since then and it basically make it so that we get a specialized
TimeoutExceptionthat our scripts can try/catch on.In our situation, it is valuable so we can send logs (read: Sentry) or suspend a specific processing so it can be picked-up later on.
I'm sharing it because I'm pretty convinced there is real value in there for people processing SQS messages a lot with lambda functions in PHP, like we do.
Dependency
So yes, it uses SebastianBergmann\Invoker - even though it is from phpunit, it basically just make use of the
pcntlextension in a convenient way. The code is simple, well isolated and straight forward so, I don't see any problem having this dependency in the non-dev section of my composer file but yeah it is a bit awkward.Handler
Unit-test
Usage
The real kicker here is that we created an abstract class to help us better manage SQS message handling, taking into account to properly
markAsFailedremaining records on timeout and tomarkAsFailedthe current and all remaining messages on any other exception when dealing with a FIFO queue - as per AWS recommandation. - but I'm getting a bit out of my way here, that last subject would be better off to keep for another thread 😅Caveat
The big caveat here ❗ is that since the timeout exception is raised asynchronously (really, that's kind of rare in PHP, I know) it means the original code processing something was interrupted. From a probabilistic point of view, chances are the script stopped on a lengthy call to the DB or an external API.
But yeah, it forces the programmer to think about its processing being stop in the middle. At least, the
catchsection offers an opportunity to set a status, to cleanup some data, etc. compared to a brutal lambda timeout.Beta Was this translation helpful? Give feedback.
All reactions