Skip to content

Commit

Permalink
NAS-131763 / 25.04 / Improve app status reporting (#14668)
Browse files Browse the repository at this point in the history
* Add crashed/created status

* Account for crashed/created status in workload details

* Refactor app state logic

* If we have containers in starting phase, app is still deploying
  • Loading branch information
sonicaj authored Oct 16, 2024
1 parent a42a10e commit d2e7460
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/middlewared/middlewared/plugins/apps/ix_apps/query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from collections import defaultdict
from dataclasses import dataclass
from pkg_resources import parse_version

Expand Down Expand Up @@ -85,19 +86,22 @@ def list_apps(
workloads = translate_resources_to_desired_workflow(app_resources)
# When we stop docker service and start it again - the containers can be in exited
# state which means we need to account for this.
state = 'STOPPED'
exited_containers = 0
state = AppState.STOPPED
workload_stats = defaultdict(int)
workloads_len = len(workloads['container_details'])
for container in workloads['container_details']:
if container['state'] == ContainerState.STARTING.value:
state = AppState.DEPLOYING.value
break
elif container['state'] == ContainerState.RUNNING.value:
state = AppState.RUNNING.value
elif container['state'] == ContainerState.EXITED.value:
exited_containers += 1
else:
if exited_containers != 0 and exited_containers == len(workloads['container_details']):
state = AppState.CRASHED.value
workload_stats[container['state']] += 1

if workload_stats[ContainerState.CRASHED.value]:
state = AppState.CRASHED
elif workload_stats[ContainerState.CREATED.value] or workload_stats[ContainerState.STARTING.value]:
state = AppState.DEPLOYING
elif 0 < workloads_len == sum(
workload_stats[k.value] for k in (ContainerState.RUNNING, ContainerState.EXITED)
) and workload_stats[ContainerState.RUNNING.value]:
state = AppState.RUNNING

state = state.value

app_metadata = metadata[app_name]
active_workloads = get_default_workload_values() if state == 'STOPPED' else workloads
Expand Down Expand Up @@ -199,6 +203,10 @@ def translate_resources_to_desired_workflow(app_resources: dict) -> dict:
state = ContainerState.STARTING.value
else:
state = ContainerState.RUNNING.value
elif container['State']['Status'].lower() == 'created':
state = ContainerState.CREATED.value
elif container['State']['Status'] == 'exited' and container['State']['ExitCode'] != 0:
state = ContainerState.CRASHED.value
else:
state = 'exited'

Expand Down
2 changes: 2 additions & 0 deletions src/middlewared/middlewared/plugins/apps/ix_apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class AppState(enum.Enum):


class ContainerState(enum.Enum):
CRASHED = 'crashed'
CREATED = 'created'
EXITED = 'exited'
RUNNING = 'running'
STARTING = 'starting'
Expand Down

0 comments on commit d2e7460

Please sign in to comment.