Skip to content

Docs: Correction of code sample using idempotent_function in combination with Batch utility #1745

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

Closed
1 task done
kmkhr opened this issue Nov 23, 2022 · 3 comments
Closed
1 task done
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@kmkhr
Copy link
Contributor

kmkhr commented Nov 23, 2022

What were you searching in the docs?

I was looking into ways to combine Idempotency utility and Batch utility.

Is this related to an existing documentation section?

https://awslabs.github.io/aws-lambda-powertools-python/2.3.1/utilities/idempotency/#idempotent_function-decorator

How can we improve?

The code sample using batch_processor decorator has the following calling order, where save_inprogress is called before register_lambda_context is called.

batch_processor -> idempotent_function -> save_inprogress -> record_handler -> idempotent_function -> save_inprogress -> dummy -> register_lambda_context

Got a suggestion in mind?

Shouldn't we change to using context manager instead of decorator?
Also, shouldn't the dummy call be after register_lambda_context?

def handler(event, context):
    config.register_lambda_context(context) # see Lambda timeouts section
    # `data` parameter must be called as a keyword argument to work
    dummy("hello", "universe", data="test")
    batch = event["Records"]
    with processor(records=batch, handler=record_handler):
        processor.process()
    return processor.response()

Acknowledgment

  • I understand the final update might be different from my proposed suggestion, or refused.
@kmkhr kmkhr added documentation Improvements or additions to documentation triage Pending triage from maintainers labels Nov 23, 2022
@heitorlessa heitorlessa self-assigned this Nov 24, 2022
@heitorlessa
Copy link
Contributor

hey @kt-hr great catch on register_lambda_context order - fixing that now.

As to your question on context manager, there is no need here unless you want to inspect the response or handle when the entire batch fails differently - see here.

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Nov 24, 2022
@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Nov 24, 2022
@github-actions
Copy link
Contributor

This is now released under 2.4.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Nov 24, 2022
@kmkhr
Copy link
Contributor Author

kmkhr commented Nov 24, 2022

hi @heitorlessa thanks for the quick correction.

I understood the difference between context manager and decorator in the batch utility. However, @idempotent_function is processed before register_lambda_context in decorator, which conflicts with the following sentence in the documentation.

If you are only using the @idempotent_function decorator to guard isolated parts of your code, you must use register_lambda_context available in the idempotency config object to benefit from this protection.

Furthermore, when the code sample is run, lambda outputs the following warnings on every cold start.

Couldn't determine the remaining time left. 
Did you call register_lambda_context on IdempotencyConfig?

To suppress this warning, I have purposely described a code sample using context manger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants