Asynchronous operations¶
Platform scans, data fetches, uptime tests, and remote activation actions return an Operation. Starting an action does not mean it has completed.
In the following examples, client is an AgentManagementClient created as described in The main AgentManagementClient class.
Queueing and paused executors¶
Platform scans, data fetches, and uptime tests share a queue for each platform. Operations start in the order they were queued, as execution slots become available. Remote activation actions use a separate execution path.
A start call can return a handle to an existing queued or running operation for the same work. Repeating a start call does not guarantee a new execution.
Pausing a platform’s executor prevents queued operations from starting. Operations already running can continue. The public API rejects new submissions while the executor is paused, before creating an operation; a repeated request may still return an existing operation.
Instance administrators can list executors with client.list_executors()
and pause or resume them with client.pause_executors()
and client.resume_executors().
By default, each returned AgentManagementExecutor exposes its id,
name, paused, active_count, parallelism and queued_count properties.
Use list_executors(as_objects=False) to retrieve dictionaries instead.
These are snapshots; call list_executors()
again to retrieve updated state.
Their state is shared with Settings > Executors in the Agent Management interface.
Resume the executor before retrying a rejected submission.
Pause and resume accept lists containing executor objects, opaque executor IDs, or both. The following example pauses two executors, then resumes the first one.
first_executor, second_executor = client.list_executors()[:2]
result = client.pause_executors([first_executor, second_executor])
print(result["updatedExecutorIds"], result["errors"])
result = client.resume_executors([first_executor])
print(result["updatedExecutorIds"], result["errors"])
Each action returns updatedExecutorIds and an errors dictionary keyed by executor ID. A failure on one executor does not prevent changes to the others. Inspect errors even when the request succeeds. Repeated IDs are processed once.
For the effects of executor pauses on manual and scheduled alert evaluation, see Working with triggered alerts.
Waiting for a result¶
platform = client.get_platform("production")
operation = platform.start_scan()
print(operation.id)
result = operation.wait_for_result()
print(result)
operation.wait_for_result()
waits for successful completion, then returns the result and available logs.
It raises an exception if the operation fails or is aborted.
If no result data is available, it returns a dictionary containing only logs
when logs are available, or None otherwise.
wait_for_completion() and wait_for_result() also wait while the operation is queued, including while its executor is paused. Neither method has a timeout parameter. To control how long your script waits, poll get_status()
and use abort() to request cancellation when needed.
Inspecting status¶
status = operation.get_status().get_raw()
print(status)
# Wait for a terminal state and inspect failures without raising:
status = operation.wait_for_completion(no_fail=True).get_raw()
print(status["status"], status.get("errorMessage"))
Status |
Meaning |
|---|---|
|
Waiting to start execution. |
|
Execution has started. |
|
Completed successfully. |
|
Completed successfully with warnings. Inspect the operation result. |
|
The operation failed. |
|
The operation was cancelled. |
The last four states are terminal. Inspect the operation result for warnings even after successful completion.
For operations submitted to the queue, queuedTimestamp records when the operation entered the queue, and startTimestamp records when execution began. These timestamps use integer milliseconds since the Unix epoch; startTimestamp is 0 until execution starts.
A queued operation may not yet have a running future. Use its Operation handle to inspect its status and request cancellation throughout its lifecycle.
Reading operation logs¶
get_status(include_logs=True) includes available log lines in status.logs and in the raw status’s logs field. Logs are omitted by default.
status = operation.get_status(include_logs=True)
for line in status.logs or []:
print(line)
get_result() and wait_for_result() also include the log lines in the returned dictionary’s logs entry.
operation.wait_for_completion(no_fail=True)
result = operation.get_result()
if result is not None:
for line in result["logs"] or []:
print(line)
The server reads operation logs from their files on disk, including any retained rotated files. The logs remain available after a server restart. They are deleted when the operation is removed from the history. If no file is available, the server can return logs from its task history while these remain in memory.
logs is None when no log is available or when you cannot read it. An empty list means that a log file exists but has no lines. Log rotation can remove the oldest lines.
Reading logs through a platform operation handle requires platform edit rights. Through an agent operation handle, it requires read access to every agent targeted by that operation. If these log permissions are not granted, the status and result remain readable with their usual permissions.
For log access in the interface and persistent platform and agent logs, see Accessing logs.
Retrieving a handle later¶
Save the platform ID, operation ID, and agent ID if the operation belongs to an agent.
operation = client.get_platform("production").get_operation("operation-id")
print(operation.get_status().get_raw())
agent_operation = client.get_agent("production", "support-agent").get_operation(
"agent-operation-id"
)
print(agent_operation.get_status().get_raw())
Searching operation history¶
result = platform.list_operations(current_only=True, limit=100)
for item in result.operations:
print(item.id, item.type, item.status)
operation = item.to_operation()
print("More operations matched:", result.has_more)
Use agent.list_operations() for one agent, or
client.list_operations() for readable operations across platforms. as_type="objects" returns operation handles. Type, status, trigger, and timestamp filters can restrict the selection.
A search returns at most 1000 operations by default. You can increase this limit to 10000. If has_more is true, use more specific filters or a shorter time range. Timestamp filters use integer milliseconds since the Unix epoch. The start timestamp is included; the end timestamp is excluded.
Requesting cancellation¶
operation = platform.get_operation("running-operation-id")
operation.abort()
status = operation.wait_for_completion(no_fail=True).get_raw()
print(status["status"])
abort() requests cancellation. It can remove an operation from the queue before a future has started, or interrupt an operation that is already running. Check the final status to determine the outcome.
Cancelling through an agent operation handle requires administration rights on that agent and every other agent targeted by the operation. Through a platform operation handle, cancellation requires platform edit rights, except for uptime tests requested for an explicit selection: those require administration rights on every selected agent. Uptime tests requested for all eligible agents require platform edit rights.
Reference documentation¶
Classes¶
An Agent Management asynchronous operation. |
|
The status of an Agent Management asynchronous operation. |
|
|
A Operation listing entry with local accessors and conversion to a resource handle. |
|
A bounded operation search result. |
Functions¶
|
Requests cancellation of the operation. |
|
Returns a handle to an agent. |
|
Returns a handle to an platform operation. |
|
Returns a handle to an operation for this agent. |
|
Returns a handle to an Agent Management platform. |
|
Returns the raw status of this operation. |
Returns the completed operation result, or None for operations without a result. |
|
Returns status information about this operation. |
|
|
Lists readable Agent Management operations matching the supplied filters. |
|
Lists operations launched for this platform. |
|
Lists operations launched for this agent. |
Starts an platform scan. |
|
Return a |
|
|
Waits for this operation to reach a terminal state. |
Waits for this operation to complete successfully and returns its result. |
