Commit 3fd107cd authored by fred's avatar fred

signal & stuff

parent 2cd269c5
import asyncio
import datetime
import random
import signal
from django.core.management.base import BaseCommand
......@@ -16,10 +17,7 @@ class Command(BaseCommand):
try:
asyncio.run(self.main(), debug=True)
except KeyboardInterrupt:
if not self.play_task.done():
self.play_task.cancel()
if not self.recompute_slots_task.done():
self.recompute_slots_task.done()
pass
def get_playlist(self, zone, start_datetime, end_datetime):
current_datetime = start_datetime
......@@ -38,6 +36,8 @@ class Command(BaseCommand):
# jingle time, every ~20 minutes
playlist.append(random.choice(jingles))
self.last_jingle_datetime = current_datetime
current_datetime = start_datetime + sum(
[x.duration for x in playlist], datetime.timedelta(seconds=0))
remaining_time = (end_datetime - current_datetime)
track = Track.objects.filter(
......@@ -53,8 +53,6 @@ class Command(BaseCommand):
# 1st strategy: remove last track and try to get a track with
# exact remaining time
playlist = playlist[:-1]
current_datetime = start_datetime + sum(
[x.duration for x in playlist], datetime.timedelta(seconds=0))
track = Track.objects.filter(
nonstop_zones=zone,
duration__gte=remaining_time,
......@@ -65,15 +63,14 @@ class Command(BaseCommand):
if track:
# found a track
playlist.append(track)
current_datetime = start_datetime + sum(
[x.duration for x in playlist], datetime.timedelta(seconds=0))
else:
# fallback strategy: didn't find track of expected duration,
# reduce playlist further
adjustment_counter += 1
playlist = playlist[:-1]
current_datetime = start_datetime + sum(
[x.duration for x in playlist], datetime.timedelta(seconds=0))
current_datetime = start_datetime + sum(
[x.duration for x in playlist], datetime.timedelta(seconds=0))
print('computed playlist:')
current_datetime = start_datetime
......@@ -115,6 +112,8 @@ class Command(BaseCommand):
'- future tracks:', [x.title for x in self.playlist[self.playhead + 1:self.playhead + 3]])
cmd = 'sleep %s # %s' % (track.duration.seconds, track.title)
await self.player_process(cmd)
# TODO: detect "soft spot", to switch to another nonstop
# tranche
self.playhead += 1
elif slot.is_stream():
print(now, 'playing stream', slot.stream)
......@@ -204,7 +203,16 @@ class Command(BaseCommand):
await writer.drain()
writer.close()
def sigterm_handler(self):
print('got signal')
self.quit = True
self.play_task.cancel()
async def main(self):
loop = asyncio.get_running_loop()
loop.add_signal_handler(
signal.SIGTERM,
self.sigterm_handler)
now = datetime.datetime.now()
self.recompute_slots()
server = await asyncio.start_server(self.handle_connection, '127.0.0.1', 8888)
......@@ -212,7 +220,7 @@ class Command(BaseCommand):
asyncio.create_task(server.serve_forever())
self.recompute_slots_task = asyncio.create_task(self.recompute_slots_loop())
while True:
while not self.quit:
duration = (self.slot.end_datetime - now).seconds
print('next sure slot', duration, self.slot.end_datetime)
if duration < 2:
......@@ -230,5 +238,3 @@ class Command(BaseCommand):
except KeyboardInterrupt:
self.quit = True
break
self.quit = True
await self.recompute_slots_task
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment