Async task decorator so that running Args: func (function): the function to be wrapped Further requirements: func must be an independent top-level function. i.e. not a class method or an anonymous function service (str): either 'lambda' o
(*args, **kwargs)
| 365 | # Wrapper written to take optional arguments |
| 366 | # http://chase-seibert.github.io/blog/2013/12/17/python-decorator-optional-parameter.html |
| 367 | def task(*args, **kwargs): |
| 368 | """Async task decorator so that running |
| 369 | |
| 370 | Args: |
| 371 | func (function): the function to be wrapped |
| 372 | Further requirements: |
| 373 | func must be an independent top-level function. |
| 374 | i.e. not a class method or an anonymous function |
| 375 | service (str): either 'lambda' or 'sns' |
| 376 | remote_aws_lambda_function_name (str): the name of a remote lambda function to call with this task |
| 377 | remote_aws_region (str): the name of a remote region to make lambda/sns calls against |
| 378 | |
| 379 | Returns: |
| 380 | A replacement function that dispatches func() to |
| 381 | run asynchronously through the service in question |
| 382 | """ |
| 383 | |
| 384 | func = None |
| 385 | if len(args) == 1 and callable(args[0]): |
| 386 | func = args[0] |
| 387 | |
| 388 | if not kwargs: # Default Values |
| 389 | service = "lambda" |
| 390 | lambda_function_name_arg = None |
| 391 | aws_region_arg = None |
| 392 | |
| 393 | else: # Arguments were passed |
| 394 | service = kwargs.get("service", "lambda") |
| 395 | lambda_function_name_arg = kwargs.get("remote_aws_lambda_function_name") |
| 396 | aws_region_arg = kwargs.get("remote_aws_region") |
| 397 | |
| 398 | capture_response = kwargs.get("capture_response", False) |
| 399 | |
| 400 | def func_wrapper(func): |
| 401 | task_path = get_func_task_path(func) |
| 402 | |
| 403 | @wraps(func) |
| 404 | def _run_async(*args, **kwargs): |
| 405 | """ |
| 406 | This is the wrapping async function that replaces the function |
| 407 | that is decorated with @task. |
| 408 | Args: |
| 409 | These are just passed through to @task's func |
| 410 | |
| 411 | Assuming a valid service is passed to task() and it is run |
| 412 | inside a Lambda process (i.e. AWS_LAMBDA_FUNCTION_NAME exists), |
| 413 | it dispatches the function to be run through the service variable. |
| 414 | Otherwise, it runs the task synchronously. |
| 415 | |
| 416 | Returns: |
| 417 | In async mode, the object returned includes state of the dispatch. |
| 418 | For instance |
| 419 | |
| 420 | When outside of Lambda, the func passed to @task is run and we |
| 421 | return the actual value. |
| 422 | """ |
| 423 | lambda_function_name = lambda_function_name_arg or os.environ.get("AWS_LAMBDA_FUNCTION_NAME") |
| 424 | aws_region = aws_region_arg or os.environ.get("AWS_REGION") |
no test coverage detected