@@ -417,7 +417,6 @@ def create_pusher_task_handler():
417
417
pusher_connected = False
418
418
419
419
# Transcript
420
- transcript_ws = None
421
420
segment_buffers = []
422
421
in_progress_conversation_id = None
423
422
@@ -431,11 +430,11 @@ async def transcript_consume():
431
430
nonlocal websocket_active
432
431
nonlocal segment_buffers
433
432
nonlocal in_progress_conversation_id
434
- nonlocal transcript_ws
433
+ nonlocal pusher_ws
435
434
nonlocal pusher_connected
436
435
while websocket_active or len (segment_buffers ) > 0 :
437
436
await asyncio .sleep (1 )
438
- if transcript_ws and len (segment_buffers ) > 0 :
437
+ if pusher_connected and pusher_ws and len (segment_buffers ) > 0 :
439
438
try :
440
439
# 102|data
441
440
data = bytearray ()
@@ -444,17 +443,16 @@ async def transcript_consume():
444
443
bytes (json .dumps ({"segments" : segment_buffers , "memory_id" : in_progress_conversation_id }),
445
444
"utf-8" ))
446
445
segment_buffers = [] # reset
447
- await transcript_ws .send (data )
446
+ await pusher_ws .send (data )
448
447
except websockets .exceptions .ConnectionClosed as e :
449
448
print (f"Pusher transcripts Connection closed: { e } " , uid )
450
- transcript_ws = None
451
449
pusher_connected = False
452
- await reconnect ()
453
450
except Exception as e :
454
451
print (f"Pusher transcripts failed: { e } " , uid )
452
+ if pusher_connected is False :
453
+ await connect ()
455
454
456
455
# Audio bytes
457
- audio_bytes_ws = None
458
456
audio_buffers = bytearray ()
459
457
audio_bytes_enabled = bool (get_audio_bytes_webhook_seconds (uid )) or is_audio_bytes_app_enabled (uid )
460
458
@@ -465,52 +463,56 @@ def audio_bytes_send(audio_bytes):
465
463
async def audio_bytes_consume ():
466
464
nonlocal websocket_active
467
465
nonlocal audio_buffers
468
- nonlocal audio_bytes_ws
466
+ nonlocal pusher_ws
469
467
nonlocal pusher_connected
470
468
while websocket_active or len (audio_buffers ) > 0 :
471
469
await asyncio .sleep (1 )
472
- if audio_bytes_ws and len (audio_buffers ) > 0 :
470
+ if pusher_connected and pusher_ws and len (audio_buffers ) > 0 :
473
471
try :
474
472
# 101|data
475
473
data = bytearray ()
476
474
data .extend (struct .pack ("I" , 101 ))
477
475
data .extend (audio_buffers .copy ())
478
476
audio_buffers = bytearray () # reset
479
- await audio_bytes_ws .send (data )
477
+ await pusher_ws .send (data )
480
478
except websockets .exceptions .ConnectionClosed as e :
481
479
print (f"Pusher audio_bytes Connection closed: { e } " , uid )
482
- audio_bytes_ws = None
483
480
pusher_connected = False
484
- await reconnect ()
485
481
except Exception as e :
486
482
print (f"Pusher audio_bytes failed: { e } " , uid )
483
+ if pusher_connected is False :
484
+ await connect ()
487
485
488
- async def reconnect ():
486
+ async def connect ():
489
487
nonlocal pusher_connected
490
488
nonlocal pusher_connect_lock
489
+ nonlocal pusher_ws
491
490
async with pusher_connect_lock :
492
491
if pusher_connected :
493
492
return
494
- await connect ()
493
+ # drain
494
+ if pusher_ws :
495
+ try :
496
+ await pusher_ws .close ()
497
+ pusher_ws = None
498
+ except Exception as e :
499
+ print (f"Pusher draining failed: { e } " , uid )
500
+ # connect
501
+ await _connect ()
495
502
496
- async def connect ():
503
+ async def _connect ():
497
504
nonlocal pusher_ws
498
- nonlocal transcript_ws
499
- nonlocal audio_bytes_ws
500
- nonlocal audio_bytes_enabled
501
505
nonlocal pusher_connected
502
506
503
507
try :
504
508
pusher_ws = await connect_to_trigger_pusher (uid , sample_rate )
505
509
pusher_connected = True
506
- transcript_ws = pusher_ws
507
- if audio_bytes_enabled :
508
- audio_bytes_ws = pusher_ws
509
510
except Exception as e :
510
511
print (f"Exception in connect: { e } " )
511
512
512
513
async def close (code : int = 1000 ):
513
- await pusher_ws .close (code )
514
+ if pusher_ws :
515
+ await pusher_ws .close (code )
514
516
515
517
return (connect , close ,
516
518
transcript_send , transcript_consume ,
0 commit comments