|
| 1 | +Polling for Events |
| 2 | +================== |
| 3 | + |
| 4 | +There are often situations where an API request will trigger a |
| 5 | +long-running operation (e.g. Instance shutdown) that will run |
| 6 | +after the request has been made. These operations are tracked |
| 7 | +through `Linode Account Events`_ which reflect the target entity, |
| 8 | +progress, and status of these operations. |
| 9 | + |
| 10 | +.. _Linode Account Events: https://www.linode.com/docs/api/account/#events-list |
| 11 | + |
| 12 | +There are often cases where you would like for your application to |
| 13 | +halt until these operations have succeeded. The most reliable and |
| 14 | +efficient way to achieve this is by using the :py:class:`EventPoller` |
| 15 | +object. |
| 16 | + |
| 17 | +Polling on Basic Operations |
| 18 | +--------------------------- |
| 19 | + |
| 20 | +In order to poll for an operation, we must create an :py:class:`EventPoller` |
| 21 | +object *before* the endpoint that triggers the operation has been called. |
| 22 | + |
| 23 | +Assuming a :py:class:`LinodeClient` object has already been created with the name |
| 24 | +"client" and an :py:class:`Instance` object has already been created with the name "my_instance", |
| 25 | +an :py:class:`EventPoller` can be created using the |
| 26 | +:meth:`LinodeClient.polling.event_poller_create(...) <PollingGroup.event_poller_create>` |
| 27 | +method:: |
| 28 | + |
| 29 | + poller = client.polling.event_poller_create( |
| 30 | + "linode", # The type of the target entity |
| 31 | + "linode_shutdown", # The action to poll for |
| 32 | + entity_id=my_instance.id, # The ID of your Linode Instance |
| 33 | + ) |
| 34 | + |
| 35 | +Valid values for the `type` and `action` fields can be found in the `Events Response Documentation`_. |
| 36 | + |
| 37 | +.. _Events Response Documentation: https://www.linode.com/docs/api/account/#events-list__responses |
| 38 | + |
| 39 | +From here, we can send the request to trigger the long-running operation:: |
| 40 | + |
| 41 | + my_instance.shutdown() |
| 42 | + |
| 43 | +To wait for this operation to finish, we can call the |
| 44 | +:meth:`poller.wait_for_next_event_finished(...) <EventPoller.wait_for_next_event_finished>` |
| 45 | +method:: |
| 46 | + |
| 47 | + poller.wait_for_next_event_finished() |
| 48 | + |
| 49 | +The :py:class:`timeout` (default 240) and :py:class:`interval` (default 5) arguments can optionally be used to configure the timeout |
| 50 | +and poll frequency for this operation. |
| 51 | + |
| 52 | +Bringing this together, we get the following:: |
| 53 | + |
| 54 | + from linode_api4 import LinodeClient, Instance |
| 55 | + |
| 56 | + # Construct a client |
| 57 | + client = LinodeClient("MY_LINODE_TOKEN") |
| 58 | + |
| 59 | + # Fetch an existing Linode Instance |
| 60 | + my_instance = client.load(Instance, 12345) |
| 61 | + |
| 62 | + # Create the event poller |
| 63 | + poller = client.polling.event_poller_create( |
| 64 | + "linode", # The type of the target entity |
| 65 | + "linode_shutdown", # The action to poll for |
| 66 | + entity_id=my_instance.id, # The ID of your Linode Instance |
| 67 | + ) |
| 68 | + |
| 69 | + # Shutdown the Instance |
| 70 | + my_instance.shutdown() |
| 71 | + |
| 72 | + # Wait until the event has finished |
| 73 | + poller.wait_for_next_event_finished() |
| 74 | + |
| 75 | + print("Linode has been successfully shutdown!") |
| 76 | + |
| 77 | +Polling for an Entity to be Free |
| 78 | +-------------------------------- |
| 79 | + |
| 80 | +In many cases, certain operations cannot be run until any other operations running on a resource have |
| 81 | +been completed. To ensure these operation are run reliably and do not encounter conflicts, |
| 82 | +you can use the |
| 83 | +:meth:`LinodeClient.polling.wait_for_entity_free(...) <PollingGroup.wait_for_entity_free>` method |
| 84 | +to wait until a resource has no running or queued operations. |
| 85 | + |
| 86 | +For example:: |
| 87 | + |
| 88 | + # Construct a client |
| 89 | + client = LinodeClient("MY_LINODE_TOKEN") |
| 90 | + |
| 91 | + # Load an existing instance |
| 92 | + my_instance = client.load(Instance, 12345) |
| 93 | + |
| 94 | + # Wait until the Linode is not busy |
| 95 | + client.polling.wait_for_entity_free( |
| 96 | + "linode", |
| 97 | + my_instance.id |
| 98 | + ) |
| 99 | + |
| 100 | + # Boot the Instance |
| 101 | + my_instance.boot() |
| 102 | + |
| 103 | +The :py:class:`timeout` (default 240) and :py:class:`interval` (default 5) arguments can optionally be used to configure the timeout |
| 104 | +and poll frequency for this operation. |
0 commit comments