custom_waiter.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. from enum import Enum
  7. import botocore.client
  8. import botocore.waiter
  9. import logging
  10. logging.getLogger('boto').setLevel(logging.CRITICAL)
  11. class WaitState(Enum):
  12. SUCCESS = 'success'
  13. FAILURE = 'failure'
  14. class CustomWaiter:
  15. """
  16. Base class for a custom waiter.
  17. Modified from:
  18. https://docs.aws.amazon.com/code-samples/latest/catalog/python-demo_tools-custom_waiter.py.html
  19. """
  20. def __init__(
  21. self, name: str, operation: str, argument: str,
  22. acceptors: dict, client: botocore.client, delay: int = 30, max_tries: int = 10,
  23. matcher='path'):
  24. """
  25. Subclasses should pass specific operations, arguments, and acceptors to
  26. their superclass.
  27. :param name: The name of the waiter. This can be any descriptive string.
  28. :param operation: The operation to wait for. This must match the casing of
  29. the underlying operation model, which is typically in
  30. CamelCase.
  31. :param argument: The dict keys used to access the result of the operation, in
  32. dot notation. For example, 'Job.Status' will access
  33. result['Job']['Status'].
  34. :param acceptors: The list of acceptors that indicate the wait is over. These
  35. can indicate either success or failure. The acceptor values
  36. are compared to the result of the operation after the
  37. argument keys are applied.
  38. :param client: The Boto3 client.
  39. :param delay: The number of seconds to wait between each call to the operation. Default to 30 seconds.
  40. :param max_tries: The maximum number of tries before exiting. Default to 10.
  41. :param matcher: The kind of matcher to use. Default to 'path'.
  42. """
  43. self.name = name
  44. self.operation = operation
  45. self.argument = argument
  46. self.client = client
  47. self.waiter_model = botocore.waiter.WaiterModel({
  48. 'version': 2,
  49. 'waiters': {
  50. name: {
  51. "delay": delay,
  52. "operation": operation,
  53. "maxAttempts": max_tries,
  54. "acceptors": [{
  55. "state": state.value,
  56. "matcher": matcher,
  57. "argument": argument,
  58. "expected": expected
  59. } for expected, state in acceptors.items()]
  60. }}})
  61. self.waiter = botocore.waiter.create_waiter_with_client(
  62. self.name, self.waiter_model, self.client)
  63. self._timeout = delay * max_tries
  64. def _wait(self, **kwargs):
  65. """
  66. Starts the botocore wait loop.
  67. :param kwargs: Keyword arguments that are passed to the operation being polled.
  68. """
  69. self.waiter.wait(**kwargs)
  70. @property
  71. def timeout(self):
  72. return self._timeout