Skip to content

Commit

Permalink
🐛(back) fix sync_medialive_video command
Browse files Browse the repository at this point in the history
The sync_medialive_video is failing when medialive channel exists,
but its associated video does not.
When a channel does not have a video, it can't be used.
Therefore, we should delete the stack of this unused medialive channel.
  • Loading branch information
polyhb committed Jun 14, 2023
1 parent 3b51a50 commit 13b181a
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 277 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- Blacklist the refresh token on the frontend side (#2265)
- Legale pages not accessible from url (#2266)
- Remove old svg from the back to the front application (#1485)
- Sync medialive command deletes medialive stack when video not found

### Changed

Expand Down
22 changes: 11 additions & 11 deletions src/backend/marsha/core/api/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
from rest_framework.response import Response

from marsha.core.defaults import ENDED, JITSI
from marsha.core.utils.medialive_utils import (
ManifestMissingException,
create_live_stream,
create_mediapackage_harvest_job,
delete_aws_element_stack,
delete_mediapackage_channel,
start_live_channel,
stop_live_channel,
update_id3_tags,
wait_medialive_channel_is_created,
)
from marsha.websocket.utils import channel_layers_utils

from .. import defaults, forms, permissions, serializers, storage
Expand All @@ -46,17 +57,6 @@
)
from ..utils import jitsi_utils, time_utils
from ..utils.api_utils import validate_signature
from ..utils.medialive_utils import (
ManifestMissingException,
create_live_stream,
create_mediapackage_harvest_job,
delete_aws_element_stack,
delete_mediapackage_channel,
start_live_channel,
stop_live_channel,
update_id3_tags,
wait_medialive_channel_is_created,
)
from ..utils.time_utils import to_timestamp
from ..utils.xmpp_utils import close_room, create_room, reopen_room_for_vod
from .base import APIViewMixin, BulkDestroyModelMixin, ObjectPkMixin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from django.utils import timezone

from marsha.core.utils.medialive_utils import (
delete_mediapackage_channel,
delete_medialive_stack,
list_medialive_channels,
medialive_client,
mediapackage_client,
)
from marsha.core.utils.time_utils import to_datetime

Expand Down Expand Up @@ -42,33 +40,4 @@ def handle(self, *args, **options):
created_at + timedelta(seconds=settings.NB_SECONDS_LIVING_DEV_STACK)
<= now
):
self.stdout.write(
f"Cleaning stack with name {medialive_channel['Name']}"
)

if medialive_channel["State"] == "RUNNING":
self.stdout.write(
"Medialive channel is running, we must stop it first."
)
channel_waiter = medialive_client.get_waiter("channel_stopped")
medialive_client.stop_channel(ChannelId=medialive_channel["Id"])
channel_waiter.wait(ChannelId=medialive_channel["Id"])

medialive_client.delete_channel(ChannelId=medialive_channel["Id"])
input_waiter = medialive_client.get_waiter("input_detached")
for medialive_input in medialive_channel["InputAttachments"]:
input_waiter.wait(InputId=medialive_input["InputId"])
medialive_client.delete_input(
InputId=medialive_input["InputId"]
)

try:
# the mediapackage channel can already be deleted when the dev stack
# have ngrok up and running.
delete_mediapackage_channel(medialive_channel["Name"])
except mediapackage_client.exceptions.NotFoundException:
pass

self.stdout.write(
f"Stack with name {medialive_channel['Name']} deleted"
)
delete_medialive_stack(medialive_channel, self.stdout)
37 changes: 24 additions & 13 deletions src/backend/marsha/core/management/commands/sync_medialive_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

from marsha.core.defaults import IDLE, RUNNING, STOPPED
from marsha.core.models import Video
from marsha.core.utils.medialive_utils import list_medialive_channels, update_id3_tags
from marsha.core.utils.medialive_utils import (
delete_medialive_stack,
list_medialive_channels,
update_id3_tags,
)
from marsha.core.utils.time_utils import to_timestamp


Expand Down Expand Up @@ -85,17 +89,24 @@ def handle(self, *args, **options):
# the channel name contains the environment, the primary key and the created_at
# stamp. Here we want to use the primary key
_environment, live_pk, _stamp = medialive_channel["Name"].split("_")
live = Video.objects.get(pk=live_pk)
self.stdout.write(f"Checking video {live.id}")
channel_state = medialive_channel.get("State").casefold()

# If the live state in not sync with media channel state,
# we manually update to avoid soft lock
live_state = live.live_state.casefold()
if not self.is_channel_sync_with_video(live_state, channel_state):
try:
live = Video.objects.get(pk=live_pk)
self.stdout.write(f"Checking video {live.id}")
channel_state = medialive_channel.get("State").casefold()

# If the live state in not sync with media channel state,
# we manually update to avoid soft lock
live_state = live.live_state.casefold()
if not self.is_channel_sync_with_video(live_state, channel_state):
self.stdout.write(
f"""Video {live.id} not sync: (video) """
f"""{live_state} != {channel_state} (medialive)"""
)
self.update_video_state(live, channel_state)
except Video.DoesNotExist:
# live exists in AWS but not our DB
self.stdout.write(
f"""
Video {live.id} not sync: (video) {live_state} != {channel_state} (medialive)
"""
f"""Channel {medialive_channel["Name"]} is """
f"""attached to a video {live_pk} that does not exist"""
)
self.update_video_state(live, channel_state)
delete_medialive_stack(medialive_channel, self.stdout)
Loading

0 comments on commit 13b181a

Please sign in to comment.