mirror of
https://github.com/navidrome/navidrome.git
synced 2026-02-27 12:26:11 -05:00
Add the TaskQueueService interface with CreateQueue, Enqueue, GetTaskStatus, and CancelTask methods plus QueueConfig struct.
154 lines
4.5 KiB
Python
154 lines
4.5 KiB
Python
# Code generated by ndpgen. DO NOT EDIT.
|
|
#
|
|
# This file contains client wrappers for the TaskQueue host service.
|
|
# It is intended for use in Navidrome plugins built with extism-py.
|
|
#
|
|
# IMPORTANT: Due to a limitation in extism-py, you cannot import this file directly.
|
|
# The @extism.import_fn decorators are only detected when defined in the plugin's
|
|
# main __init__.py file. Copy the needed functions from this file into your plugin.
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
import extism
|
|
import json
|
|
|
|
|
|
class HostFunctionError(Exception):
|
|
"""Raised when a host function returns an error."""
|
|
pass
|
|
|
|
|
|
@extism.import_fn("extism:host/user", "taskqueue_createqueue")
|
|
def _taskqueue_createqueue(offset: int) -> int:
|
|
"""Raw host function - do not call directly."""
|
|
...
|
|
|
|
|
|
@extism.import_fn("extism:host/user", "taskqueue_enqueue")
|
|
def _taskqueue_enqueue(offset: int) -> int:
|
|
"""Raw host function - do not call directly."""
|
|
...
|
|
|
|
|
|
@extism.import_fn("extism:host/user", "taskqueue_gettaskstatus")
|
|
def _taskqueue_gettaskstatus(offset: int) -> int:
|
|
"""Raw host function - do not call directly."""
|
|
...
|
|
|
|
|
|
@extism.import_fn("extism:host/user", "taskqueue_canceltask")
|
|
def _taskqueue_canceltask(offset: int) -> int:
|
|
"""Raw host function - do not call directly."""
|
|
...
|
|
|
|
|
|
def taskqueue_create_queue(name: str, config: Any) -> None:
|
|
"""CreateQueue creates a named task queue with the given configuration.
|
|
Zero-value fields in config use sensible defaults.
|
|
If a queue with the same name already exists, returns an error.
|
|
On startup, this also recovers any stale "running" tasks from a previous crash.
|
|
|
|
Args:
|
|
name: str parameter.
|
|
config: Any parameter.
|
|
|
|
Raises:
|
|
HostFunctionError: If the host function returns an error.
|
|
"""
|
|
request = {
|
|
"name": name,
|
|
"config": config,
|
|
}
|
|
request_bytes = json.dumps(request).encode("utf-8")
|
|
request_mem = extism.memory.alloc(request_bytes)
|
|
response_offset = _taskqueue_createqueue(request_mem.offset)
|
|
response_mem = extism.memory.find(response_offset)
|
|
response = json.loads(extism.memory.string(response_mem))
|
|
|
|
if response.get("error"):
|
|
raise HostFunctionError(response["error"])
|
|
|
|
|
|
|
|
def taskqueue_enqueue(queue_name: str, payload: bytes) -> str:
|
|
"""Enqueue adds a task to the named queue. Returns the task ID.
|
|
payload is opaque bytes passed back to the plugin on execution.
|
|
|
|
Args:
|
|
queue_name: str parameter.
|
|
payload: bytes parameter.
|
|
|
|
Returns:
|
|
str: The result value.
|
|
|
|
Raises:
|
|
HostFunctionError: If the host function returns an error.
|
|
"""
|
|
request = {
|
|
"queueName": queue_name,
|
|
"payload": payload,
|
|
}
|
|
request_bytes = json.dumps(request).encode("utf-8")
|
|
request_mem = extism.memory.alloc(request_bytes)
|
|
response_offset = _taskqueue_enqueue(request_mem.offset)
|
|
response_mem = extism.memory.find(response_offset)
|
|
response = json.loads(extism.memory.string(response_mem))
|
|
|
|
if response.get("error"):
|
|
raise HostFunctionError(response["error"])
|
|
|
|
return response.get("result", "")
|
|
|
|
|
|
def taskqueue_get_task_status(task_id: str) -> str:
|
|
"""GetTaskStatus returns the status of a task: "pending", "running",
|
|
"completed", "failed", or "cancelled".
|
|
|
|
Args:
|
|
task_id: str parameter.
|
|
|
|
Returns:
|
|
str: The result value.
|
|
|
|
Raises:
|
|
HostFunctionError: If the host function returns an error.
|
|
"""
|
|
request = {
|
|
"taskId": task_id,
|
|
}
|
|
request_bytes = json.dumps(request).encode("utf-8")
|
|
request_mem = extism.memory.alloc(request_bytes)
|
|
response_offset = _taskqueue_gettaskstatus(request_mem.offset)
|
|
response_mem = extism.memory.find(response_offset)
|
|
response = json.loads(extism.memory.string(response_mem))
|
|
|
|
if response.get("error"):
|
|
raise HostFunctionError(response["error"])
|
|
|
|
return response.get("result", "")
|
|
|
|
|
|
def taskqueue_cancel_task(task_id: str) -> None:
|
|
"""CancelTask cancels a pending task. Returns error if already
|
|
running, completed, or failed.
|
|
|
|
Args:
|
|
task_id: str parameter.
|
|
|
|
Raises:
|
|
HostFunctionError: If the host function returns an error.
|
|
"""
|
|
request = {
|
|
"taskId": task_id,
|
|
}
|
|
request_bytes = json.dumps(request).encode("utf-8")
|
|
request_mem = extism.memory.alloc(request_bytes)
|
|
response_offset = _taskqueue_canceltask(request_mem.offset)
|
|
response_mem = extism.memory.find(response_offset)
|
|
response = json.loads(extism.memory.string(response_mem))
|
|
|
|
if response.get("error"):
|
|
raise HostFunctionError(response["error"])
|
|
|