Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import os
  2. import json
  3. import importlib
  4. import inspect
  5. import boto3
  6.  
  7. SNS_ARN = os.environ.get('SNS_ARN', None)
  8.  
  9.  
  10. def import_and_get_task(task_path):
  11. """
  12. Given a modular path to a function, import that module
  13. and return the function.
  14. """
  15. module, function = task_path.rsplit('.', 1)
  16. app_module = importlib.import_module(module)
  17. app_function = getattr(app_module, function)
  18. return app_function
  19.  
  20.  
  21. def route_task(event, context):
  22. """
  23. Gets SNS Message, deserialises the message,
  24. imports the function, calls the function with args
  25. """
  26. record = event['Records'][0]
  27. message = json.loads(
  28. record['Sns']['Message']
  29. )
  30. func = import_and_get_task(
  31. message['task_path'])
  32. return func(
  33. *message['args'], **message['kwargs']
  34. )
  35.  
  36.  
  37. def send_async_task(task_path, *args, **kwargs):
  38. """
  39. Send a SNS message to a specified SNS topic
  40. Serialise the func path and arguments
  41. """
  42. client = boto3.client('sns')
  43. message = {
  44. 'task_path': task_path,
  45. 'args': args,
  46. 'kwargs': kwargs
  47. }
  48. return client.publish(
  49. TargetArn=SNS_ARN,
  50. Message=json.dumps(message),
  51. )
  52.  
  53.  
  54. def task():
  55. """
  56. Async task decorator for a function.
  57. Serialises and dispatches the task to SNS.
  58. Lambda subscribes to SNS topic and gets this message
  59. Lambda routes the message to the same function
  60.  
  61. Example:
  62.  
  63. @task()
  64. def my_async_func(*args, **kwargs):
  65. dosomething()
  66.  
  67. my_async_func.delay(*args, **kwargs)
  68.  
  69. """
  70. def _delay(func):
  71. def _delay_inner(*args, **kwargs):
  72. module_path = inspect.getmodule(func).__name__
  73. task_path = '{module_path}.{func_name}'.format(
  74. module_path=module_path,
  75. func_name=func.__name__
  76. )
  77. if SNS_ARN:
  78. return send_async_task(task_path, *args, **kwargs)
  79. return func(*args, **kwargs)
  80. return _delay_inner
  81.  
  82. def _wrap(func):
  83. func.delay = _delay(func)
  84. return func
  85.  
  86. return _wrap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement