Skip to content

Commit b48206e

Browse files
cleanup
1 parent ee7d892 commit b48206e

3 files changed

Lines changed: 29 additions & 16 deletions

File tree

manager/director/apps/sites/consumers.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@ async def connect(self):
3333
return
3434
async with self.close_on_error():
3535
site_id = int(self.scope["url_route"]["kwargs"]["site_id"])
36-
site = await find_site(site_id, user)
36+
self.site: Site = await find_site(site_id, user)
3737

3838
# Listen for events for this site
3939
await self.channel_layer.group_add(
40-
site.channels_group_name(),
40+
self.site.channels_group_name(),
4141
self.channel_name,
4242
)
4343

4444
await self.accept()
45+
await self.send_site_info(self.site)
46+
47+
async def operation_updated(self, event: dict[str, Any]) -> None:
48+
"""Handle an operation update event."""
49+
await self.send_site_info(self.site)
50+
51+
async def send_site_info(self, site: Site) -> None:
52+
"""Send the site information to the client."""
4553
info = await self.gather_site_info(site)
4654
await self.send(text_data=f"<code id='site-info'>\n{json.dumps(info, indent=4)}</code>")
4755

@@ -74,21 +82,18 @@ def gather_site_info(self, site: Site) -> dict[str, Any]:
7482
"actions": [],
7583
}
7684
for action in op.list_actions_in_order():
77-
operation_info["actions"].append(
78-
{
79-
"slug": action.slug,
80-
"name": action.name,
81-
"started_time": (
82-
action.started_time.isoformat()
83-
if action.started_time is not None
84-
else None
85-
),
86-
"result": action.result,
87-
"user_message": action.user_message,
88-
}
89-
)
85+
action_info = {
86+
"slug": action.slug,
87+
"name": action.name,
88+
"started_time": (
89+
action.started_time.isoformat() if action.started_time is not None else None
90+
),
91+
"result": action.result,
92+
"user_message": action.user_message,
93+
}
9094
if self.scope["user"].is_superuser:
91-
operation_info["actions"][-1]["message"] = action.message
95+
action_info["message"] = action.message
96+
operation_info["actions"].append(action_info)
9297

9398
info["operation"] = operation_info
9499

manager/director/apps/sites/operations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ async def send_operation_updated_message(site: Site) -> None:
181181
"""Broadcast that the operation status has been updated."""
182182
await get_channel_layer().group_send(
183183
site.channels_group_name(),
184+
# dispatch to the consumer's `operation_updated` method
184185
{"type": "operation.updated"},
185186
)
186187

@@ -190,5 +191,6 @@ async def send_site_updated_message(site: Site) -> None:
190191
"""Broadcast that the site metadata has been updated."""
191192
await get_channel_layer().group_send(
192193
site.channels_group_name(),
194+
# dispatch to the consumer's `site_updated` method
193195
{"type": "site.updated"},
194196
)

manager/director/apps/sites/tasks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def create_site(operation_id: int) -> None:
1717
wrapper.register_action("Creating Docker service", actions.update_docker_service)
1818

1919

20+
@shared_task
21+
def restart_site_process(operation_id: int) -> None:
22+
with auto_run_operation_wrapper(operation_id) as wrapper:
23+
wrapper.register_action("Restarting Docker service", actions.update_docker_service)
24+
25+
2026
@shared_task
2127
def delete_site(operation_id: int) -> None:
2228
site = Site.objects.get(operation__id=operation_id)

0 commit comments

Comments
 (0)