]> git.0d.be Git - django-panik-nonstop.git/commitdiff
stamina: debug print to stderr
authorFrédéric Péters <fpeters@0d.be>
Tue, 14 Jul 2020 04:00:48 +0000 (06:00 +0200)
committerFrédéric Péters <fpeters@0d.be>
Tue, 14 Jul 2020 04:08:09 +0000 (06:08 +0200)
nonstop/management/commands/stamina.py

index 5f47066aa1c4dcdae59982df34872ad8c1c97299..d64b1231defbe21a2a61121ed409209209fcbaa6 100644 (file)
@@ -3,6 +3,7 @@ import datetime
 import json
 import random
 import signal
+import sys
 
 import requests
 
@@ -101,13 +102,13 @@ class Command(BaseCommand):
                 current_datetime = start_datetime + sum(
                         [x.duration for x in playlist], datetime.timedelta(seconds=0))
 
-        print('computed playlist: (computation time: %ss)' % (datetime.datetime.now() - t0))
+        print('computed playlist: (computation time: %ss)' % (datetime.datetime.now() - t0), file=sys.stderr)
         current_datetime = start_datetime
         for track in playlist:
-            print('   ', current_datetime, track.duration, track.title)
+            print('   ', current_datetime, track.duration, track.title, file=sys.stderr)
             current_datetime += track.duration
-        print('   ', current_datetime, '---')
-        print('   adjustment_counter:', adjustment_counter)
+        print('   ', current_datetime, '---', file=sys.stderr)
+        print('   adjustment_counter:', adjustment_counter, file=sys.stderr)
 
         return playlist
 
@@ -152,7 +153,7 @@ class Command(BaseCommand):
                 cmd.append(item.stream.url)
             else:
                 cmd.append(item.file_path())
-        print('cmd:', cmd)
+        print('cmd:', cmd, file=sys.stderr)
         if isinstance(cmd, str):
             self.player = await asyncio.create_subprocess_shell(
                     cmd,
@@ -185,7 +186,7 @@ class Command(BaseCommand):
                 except IndexError:
                     break
                 self.current_track_start_datetime = now
-                print(now, track.title, track.duration)
+                print(now, track.title, track.duration, file=sys.stderr)
                 record_task = None
                 if isinstance(track, Track):  # not jingles
                     record_task = asyncio.create_task(self.record_nonstop_line(track, datetime.datetime.now()))
@@ -197,23 +198,23 @@ class Command(BaseCommand):
                     break
                 self.playhead += 1
         elif slot.is_stream():
-            print(now, 'playing stream', slot.stream)
+            print(now, 'playing stream', slot.stream, file=sys.stderr)
             if slot.jingle_id:
                 await self.player_process(slot.jingle, timeout=60)
-            print('timeout at', (slot.end_datetime - now).total_seconds())
+            print('timeout at', (slot.end_datetime - now).total_seconds(), file=sys.stderr)
             await self.player_process(slot, timeout=(slot.end_datetime - now).total_seconds())
         else:
             if hasattr(slot, 'episode'):
-                print(now, 'playing sound', slot.episode)
+                print(now, 'playing sound', slot.episode, file=sys.stderr)
             else:
-                print(now, 'playing random')
+                print(now, 'playing random', file=sys.stderr)
             if slot.jingle_id:
                 await self.player_process(slot.jingle, timeout=60)
             await self.player_process(slot)
 
     def recompute_playlist(self):
         current_track = self.playlist[self.playhead]
-        print('recompute_playlist, from', current_track.title, self.current_track_start_datetime + current_track.duration, 'to', self.slot.end_datetime)
+        print('recompute_playlist, from', current_track.title, self.current_track_start_datetime + current_track.duration, 'to', self.slot.end_datetime, file=sys.stderr)
         playlist = self.get_playlist(self.slot,
                 self.current_track_start_datetime + current_track.duration, self.slot.end_datetime)
         if playlist:
@@ -266,7 +267,7 @@ class Command(BaseCommand):
 
     def recompute_slots(self):
         now = datetime.datetime.now()
-        # print(now, 'recompute_slots')
+        # print(now, 'recompute_slots', file=sys.stderr)
         diffusion = self.get_current_diffusion()
         if diffusion:
             self.slot = diffusion
@@ -298,7 +299,7 @@ class Command(BaseCommand):
 
     async def recompute_slots_loop(self):
         now = datetime.datetime.now()
-        print(now, 'recompute_slots_loop')
+        print(now, 'recompute_slots_loop', file=sys.stderr)
         sleep = (60 - now.second) % 10  # adjust to awake at :00
         while not self.quit:
             await asyncio.sleep(sleep)
@@ -308,18 +309,18 @@ class Command(BaseCommand):
             expected_slot = self.slot
             if current_slot != expected_slot:
                 now = datetime.datetime.now()
-                print(now, 'unexpected change', current_slot, 'vs', expected_slot)
+                print(now, 'unexpected change', current_slot, 'vs', expected_slot, file=sys.stderr)
                 if isinstance(current_slot, Nonstop) and isinstance(expected_slot, Nonstop):
                     # ask for a softstop, i.e. finish the track then switch.
                     self.softstop = True
                 else:
                     # interrupt nonstop
-                    print('interrupting nonstop')
+                    print('interrupting nonstop', file=sys.stderr)
                     self.play_task.cancel()
             elif current_slot.end_datetime > expected_slot.end_datetime:
                 now = datetime.datetime.now()
                 print(now, 'change in end time, from %s to %s' %
-                        (current_slot.end_datetime, expected_slot.end_datetime))
+                        (current_slot.end_datetime, expected_slot.end_datetime), file=sys.stderr)
                 if expected_slot.end_datetime - datetime.datetime.now() > datetime.timedelta(minutes=5):
                     # more than 5 minutes left, recompute playlist
                     self.recompute_playlist()
@@ -333,9 +334,9 @@ class Command(BaseCommand):
             try:
                 message = data.decode().strip()
             except UnicodeDecodeError:
-                print('got invalid message %r' % message)
+                print('got invalid message %r' % message, file=sys.stderr)
                 continue
-            print('got message: %r' % message)
+            print('got message: %r' % message, file=sys.stderr)
             if message == 'status':
                 response = {'slot': str(self.slot)}
                 if isinstance(self.slot, Nonstop):
@@ -384,7 +385,7 @@ class Command(BaseCommand):
         writer.close()
 
     def sigterm_handler(self):
-        print('got signal')
+        print('got signal', file=sys.stderr)
         self.quit = True
         self.play_task.cancel()
 
@@ -405,7 +406,7 @@ class Command(BaseCommand):
             self.recompute_slots_task = asyncio.create_task(self.recompute_slots_loop())
             while not self.quit:
                 duration = (self.slot.end_datetime - now).seconds
-                print('next sure slot', duration, self.slot.end_datetime)
+                print('next sure slot', duration, self.slot.end_datetime, file=sys.stderr)
                 if duration < 2:
                     # next slot is very close, wait for it
                     await asyncio.sleep(duration)
@@ -415,7 +416,7 @@ class Command(BaseCommand):
                     await self.play_task
                     self.recompute_slots()
                 except asyncio.CancelledError as exc:
-                    print('exc:', exc)
+                    print('exc:', exc, file=sys.stderr)
                     if self.player and self.player.returncode is None:  # not finished
                         self.player.kill()
                 except KeyboardInterrupt: