Skip to content

Commit

Permalink
fix(tasks): Nicely handle vanished tasks
Browse files Browse the repository at this point in the history
Some tasks can deseappear from the API with time. So we must handle this case.
  • Loading branch information
lcaflc committed Nov 19, 2024
1 parent 81df66a commit 0ff653d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/pvecontrol/cluster.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from proxmoxer import ProxmoxAPI

from pvecontrol.node import PVENode
Expand All @@ -23,6 +25,7 @@ def _initstatus(self):

self.tasks = []
for task in self._api.cluster.tasks.get():
logging.debug("Get task informations: %s"%(str(task)))
self.tasks.append(PVETask(self._api, task["upid"]))

def refresh(self):
Expand Down
30 changes: 21 additions & 9 deletions src/pvecontrol/task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
import proxmoxer.core
from proxmoxer.tools import Tasks

from pvecontrol.node import NodeStatus
Expand All @@ -7,6 +8,7 @@
class TaskRunningStatus(Enum):
running = 0
stopped = 1
vanished = 2


class PVETask:
Expand Down Expand Up @@ -34,22 +36,32 @@ def _initstatus(self):
# This is bugguy. replace with a catch / except ?
# if self.node != NodeStatus.online:
# return
status = self._api.nodes(self.node).tasks(self.upid).status.get()
for k in status:
if k == "status":
self.runningstatus = TaskRunningStatus[status["status"]]
elif k == "endtime":
self.endtime = status["endtime"]
elif k == "exitstatus":
self.exitstatus = status["exitstatus"]

try:
status = self._api.nodes(self.node).tasks(self.upid).status.get()
# Some taks informations can be vanished over time (tasks status files removed from the node filesystem)
# In this case API return an error and we consider this tasks vanished and don't get more informations
except proxmoxer.core.ResourceException:
self.runningstatus = TaskRunningStatus["vanished"]
self.endtime = 0
self.exitstatus = "UNK"
else:
for k in status:
if k == "status":
self.runningstatus = TaskRunningStatus[status["status"]]
elif k == "endtime":
self.endtime = status["endtime"]
elif k == "exitstatus":
self.exitstatus = status["exitstatus"]

def log(self, limit = 0, start = 0):
return(self._api.nodes(self.node).tasks(self.upid).log.get(limit=limit, start=start))

def running(self):
return(self.runningstatus == TaskRunningStatus.running)

def vanished(self):
return(self.runningstatus == TaskRunningStatus.vanished)

def refresh(self):
self._initstatus()

Expand Down
4 changes: 4 additions & 0 deletions src/pvecontrol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def print_taskstatus(task):
def print_task(proxmox, upid, follow = False, wait = False, show_logs = False):
task = proxmox.find_task(upid)
logging.debug("Task: %s", task)
# Vanished tasks don't have any more information available in the API
if task.vanished():
print_taskstatus(task)
return

if task.running() and (follow or wait):
print_taskstatus(task)
Expand Down

0 comments on commit 0ff653d

Please sign in to comment.