-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
1185 lines (1006 loc) · 46.3 KB
/
Copy pathworker.py
File metadata and controls
1185 lines (1006 loc) · 46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
collectkit worker - background polling process.
Polls the database for chat sessions needing a reply, aggregates fragmented
user messages, invokes the LangGraph agent, and sends replies.
Usage:
python -m src.worker
Note: New Relic is initialized via newrelic-admin in railway.toml.
"""
import newrelic.agent
import os
import sys
import time
import signal
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage, AIMessage
from langsmith import Client as LangSmithClient
# Load environment before other imports
load_dotenv()
# Setup structured logging BEFORE any logger usage
from .logging_config import setup_logging, get_logger
setup_logging()
logger = get_logger(__name__)
# Import metrics and health modules
from .metrics import metrics
from .health import start_health_server, set_background_thread_status
# Thread-safe shutdown event (replaces shutdown_flag)
shutdown_event = threading.Event()
def handle_shutdown(signum, frame):
"""Handle SIGTERM/SIGINT for graceful shutdown."""
signal_name = signal.Signals(signum).name
logger.info("Shutdown signal received", extra={
"event": "shutdown_signal",
"signal": signal_name,
})
shutdown_event.set()
from .startup import validate_env # re-exported for callers
def setup_langsmith():
"""Setup and validate LangSmith tracing if configured."""
tracing_enabled = os.getenv("LANGCHAIN_TRACING_V2", "").lower() == "true"
api_key = os.getenv("LANGCHAIN_API_KEY")
project = os.getenv("LANGCHAIN_PROJECT", "my-bot")
if tracing_enabled and api_key:
try:
client = LangSmithClient()
logger.info("LangSmith tracing enabled", extra={
"event": "langsmith_enabled",
"project": project,
})
return True
except Exception as e:
logger.warning("LangSmith tracing connection failed", extra={
"event": "langsmith_failed",
"error": str(e),
})
return False
else:
logger.info("LangSmith tracing disabled", extra={
"event": "langsmith_disabled",
})
return False
from psycopg_pool import ConnectionPool
from langgraph.checkpoint.postgres import PostgresSaver
from .database_pg import (
get_sessions_needing_reply,
lock_session,
unlock_session,
get_unprocessed_messages_with_images,
mark_messages_processed,
save_bot_response,
get_database_url,
clear_chat_history,
clear_chat_session,
clear_langgraph_checkpoints,
clear_ptp_records,
clear_scheduled_tasks,
set_human_handoff,
fetch_borrower_data,
get_due_scheduled_tasks,
mark_task_completed,
mark_task_failed,
mark_ptps_fulfilled,
mark_expired_ptps_missed,
update_borrower_paid,
update_all_days_late,
get_borrowers_for_payment_check,
get_pending_scheduled_task_for_borrower,
get_followup_eligible_borrowers,
save_followup_message,
update_followup_sent,
set_session_needs_retry,
reset_error_count,
cleanup_paid_borrower_memory,
is_excluded_number,
ensure_ptp_reminders_table,
)
from .timezone_utils import now_local
from .image_analyzer import analyze_image
from .payment_checker import check_payment_status
from .agent import build_graph
from .messaging import get_messaging_adapter
def send_whatsapp_message(phone_number: str, message: str) -> dict:
"""Send a message via the configured messaging adapter.
Kept as a thin wrapper so call sites read naturally. The return shape
matches the legacy Mimin contract for backward compatibility.
"""
return dict(get_messaging_adapter().send_text(phone_number, message))
from .guardrails import validate_input, validate_output
from .followup_messages import get_followup_message, get_payment_confirmed_message
from .ptp_reminder import process_ptp_reminders
# Configuration
from .config import get_config
_cfg = get_config()
POLL_INTERVAL = 2 # seconds between polls
DEBOUNCE_SECONDS = 4 # seconds to wait after last message before replying
RESET_COMMAND = "../new" # Command to reset conversation
SCHEDULER_POLL_INTERVAL = 60 # seconds between scheduled task checks
# Scheduled-job hours, in the configured local timezone (see TIMEZONE env var).
# Override via PTP_CHECK_HOUR, BULK_SYNC_HOURS, FOLLOWUP_HOUR, PTP_REMINDER_HOUR.
PTP_CHECK_HOUR_LOCAL = _cfg.ptp_check_hour # default 23 (11 PM)
BULK_SYNC_HOURS_LOCAL = _cfg.bulk_sync_hours # default [7, 19]
FOLLOWUP_HOUR_LOCAL = _cfg.followup_hour # default 9
PTP_REMINDER_HOUR_LOCAL = _cfg.ptp_reminder_hour # default 9
BULK_SYNC_CONCURRENT_WORKERS = 3 # parallel API calls for payment checking
BULK_SYNC_API_DELAY = 0.1 # seconds between API calls (reduced due to parallelism)
FOLLOWUP_API_DELAY = 1 # seconds between follow-up sends (rate limiting)
# A/B Test - control group labels (no chatbot responses)
# Configure via env: CONTROL_GROUP_LABELS=NB1,RB1,LB1
_control_labels_raw = os.getenv("CONTROL_GROUP_LABELS", "")
CONTROL_GROUP_LABELS = frozenset(
label.strip() for label in _control_labels_raw.split(",") if label.strip()
)
def handle_guardrail_failure(phone_number: str, guardrail_name: str) -> None:
"""Handle guardrail failure by triggering silent human handoff."""
logger.warning("Guardrail triggered", extra={
"event": "guardrail_triggered",
"phone_number": phone_number,
"guardrail": guardrail_name,
})
metrics.increment("guardrail_triggers", tags={"type": guardrail_name})
set_human_handoff(phone_number, f"Guardrail: {guardrail_name}")
mark_messages_processed(phone_number)
@newrelic.agent.background_task()
def process_ptp_expiry() -> None:
"""
Check for expired PTPs and mark them as MISSED.
Called once daily (hour from PTP_CHECK_HOUR, local time). No outbound message is sent.
"""
try:
count = mark_expired_ptps_missed()
if count > 0:
logger.info("PTP expiry processed", extra={
"event": "ptp_expiry",
"count": count,
})
metrics.increment("ptp_expired", value=count)
except Exception as e:
logger.error("PTP expiry processing failed", extra={
"event": "ptp_expiry_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "ptp_expiry"})
@newrelic.agent.background_task()
def process_paid_borrower_cleanup() -> None:
"""
Clear stale bot memory for PAID (paid) borrowers.
Called daily alongside PTP expiry (hour from PTP_CHECK_HOUR, local time).
Clears LangGraph checkpoints, chat_sessions, and scheduled_tasks
so the bot starts fresh if the borrower returns with a new billing cycle.
Preserves chat_history (audit) and ptp (analytics).
"""
try:
result = cleanup_paid_borrower_memory()
if any(result.values()):
logger.info("Paid borrower memory cleanup completed", extra={
"event": "paid_cleanup",
**result,
})
metrics.increment("paid_cleanup", value=result["checkpoints"])
except Exception as e:
logger.error("Paid borrower cleanup failed", extra={
"event": "paid_cleanup_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "paid_cleanup"})
@newrelic.agent.background_task()
def process_scheduled_tasks() -> None:
"""
Process all due scheduled tasks (payment verifications).
For each due task:
1. Check payment status via payment link
2. Send appropriate follow-up message
3. Mark task as completed/failed
"""
try:
due_tasks = get_due_scheduled_tasks()
if not due_tasks:
return
logger.info("Processing scheduled tasks", extra={
"event": "scheduled_tasks_start",
"count": len(due_tasks),
})
for task in due_tasks:
# Check for shutdown before processing each task
if shutdown_event.is_set():
logger.info("Scheduled tasks interrupted by shutdown", extra={
"event": "scheduled_tasks_shutdown",
})
break
try:
logger.info("Processing scheduled task", extra={
"event": "scheduled_task_start",
"task_id": task.id,
"task_type": task.task_type,
"phone_number": task.phone_number,
})
if task.task_type == "payment_check":
# Check payment status
result = check_payment_status(task.customer_number)
status = result["status"]
# Compose message based on result
if status == "paid":
# Mark any pending PTPs as fulfilled
fulfilled_count = mark_ptps_fulfilled(task.phone_number)
if fulfilled_count > 0:
logger.info("PTPs marked as fulfilled", extra={
"event": "ptp_fulfilled",
"phone_number": task.phone_number,
"count": fulfilled_count,
})
# Update borrower status to PAID
if update_borrower_paid(task.phone_number):
logger.info("Borrower marked as paid", extra={
"event": "borrower_paid",
"phone_number": task.phone_number,
})
message = get_payment_confirmed_message()
task_result = "Payment confirmed"
elif status == "needs_human":
# Silent escalate to human CS for manual verification
set_human_handoff(task.phone_number, "Payment verification inconclusive")
mark_task_completed(task.id, f"Silent handoff - verification inconclusive: {result['raw_response'][:100]}")
logger.info("Scheduled task escalated to human", extra={
"event": "scheduled_task_handoff",
"task_id": task.id,
"phone_number": task.phone_number,
})
metrics.increment("scheduled_tasks", tags={"status": "handoff"})
continue # Skip message sending, go to next task
else:
# Fallback for any unexpected status
logger.error("Unexpected payment check status", extra={
"event": "scheduled_task_unexpected_status",
"task_id": task.id,
"status": status,
})
mark_task_failed(task.id, f"Unexpected status: {status}")
metrics.increment("scheduled_tasks", tags={"status": "failed"})
continue
# Send follow-up message via WhatsApp
send_result = send_whatsapp_message(task.phone_number, message)
if send_result["success"]:
# Save bot response to chat history
save_bot_response(task.phone_number, message, sender='bot-task')
mark_task_completed(task.id, task_result)
logger.info("Scheduled task completed", extra={
"event": "scheduled_task_completed",
"task_id": task.id,
"phone_number": task.phone_number,
"status": "paid",
"mimin_id": send_result["message_id"],
})
metrics.increment("scheduled_tasks", tags={"status": "completed"})
metrics.increment("payment_checks", tags={"status": "paid"})
else:
# Message saved but not sent
save_bot_response(task.phone_number, message, sender='bot-task')
mark_task_failed(task.id, f"WhatsApp send failed: {send_result['error']}")
logger.error("Scheduled task message send failed", extra={
"event": "scheduled_task_send_failed",
"task_id": task.id,
"phone_number": task.phone_number,
"error": send_result["error"],
})
metrics.increment("scheduled_tasks", tags={"status": "send_failed"})
else:
# Unknown task type
mark_task_failed(task.id, f"Unknown task type: {task.task_type}")
logger.warning("Unknown task type", extra={
"event": "scheduled_task_unknown_type",
"task_id": task.id,
"task_type": task.task_type,
})
except Exception as e:
logger.error("Scheduled task processing error", extra={
"event": "scheduled_task_error",
"task_id": task.id,
"error": str(e),
}, exc_info=True)
mark_task_failed(task.id, str(e)[:200])
metrics.increment("errors", tags={"type": "scheduled_task"})
except Exception as e:
logger.error("Failed to fetch scheduled tasks", extra={
"event": "scheduled_tasks_fetch_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "scheduled_tasks_fetch"})
def _check_single_borrower_payment(borrower: dict) -> dict:
"""
Check payment status for a single borrower (thread-safe helper).
Args:
borrower: Dict with phone_number, customer_number, customer_name
Returns:
Dict with original borrower data + payment_status
"""
try:
result = check_payment_status(borrower["customer_number"])
return {
**borrower,
"payment_status": result["status"],
"error": None,
}
except Exception as e:
return {
**borrower,
"payment_status": "error",
"error": str(e),
}
@newrelic.agent.background_task()
def process_bulk_borrower_sync(current_hour: int) -> None:
"""
Bulk sync borrower data. Runs at the hours listed in BULK_SYNC_HOURS (local time, default 7 and 19).
Step 1: Recalculate days_late and status for all unpaid borrowers
Step 2: Check payment status via API (CONCURRENT) with priority-based filtering:
- 7 AM: Check ALL borrowers with outstanding bills
- 7 PM: Check only HIGH priority (pending PTP or chatted in last 7 days)
Step 3: Process paid borrowers sequentially (DB updates + WhatsApp)
Payment check logic:
1. Get borrowers based on priority (high priority only at 7 PM, all at 7 AM)
2. For each borrower, check payment status via API (5 concurrent workers)
3. If PAID:
a. Update borrowers table (status=PAID, billing_amount=0)
b. Update ptp table (mark PENDING as FULFILLED)
c. If has pending scheduled_task: complete task + send message
d. If NO pending scheduled_task: silent update (no message)
4. If NOT PAID: skip (no action needed)
Args:
current_hour: Current hour in the configured local timezone (e.g. 7 or 19)
"""
try:
sync_start_time = time.time()
# === STEP 1: Update days_late (fast, no API calls) ===
updated_count, status_changed = update_all_days_late()
logger.info("Bulk sync days_late updated", extra={
"event": "bulk_sync_days_late",
"updated": updated_count,
"status_changed": status_changed,
})
# === STEP 2: Check payment status (CONCURRENT API calls) ===
# Priority-based: 7 AM checks all, 7 PM checks only high priority
if current_hour == 7:
priority = "all"
else:
priority = "high"
borrowers = get_borrowers_for_payment_check(priority=priority)
if not borrowers:
logger.info("Bulk sync completed - no outstanding bills", extra={
"event": "bulk_sync_no_borrowers",
})
return
total_borrowers = len(borrowers)
logger.info("Bulk sync payment check started", extra={
"event": "bulk_sync_payment_check_start",
"count": total_borrowers,
"priority": priority,
"workers": BULK_SYNC_CONCURRENT_WORKERS,
})
# Counters for summary
checked = 0
errors = 0
paid_borrowers = [] # Collect paid borrowers for sequential processing
# Phase 1: Concurrent API calls to check payment status
start_time = time.time()
with ThreadPoolExecutor(max_workers=BULK_SYNC_CONCURRENT_WORKERS) as executor:
# Submit all tasks
future_to_borrower = {
executor.submit(_check_single_borrower_payment, b): b
for b in borrowers
}
# Process results as they complete
for future in as_completed(future_to_borrower):
# Check for shutdown
if shutdown_event.is_set():
logger.info("Bulk sync shutdown requested", extra={
"event": "bulk_sync_shutdown",
})
executor.shutdown(wait=False, cancel_futures=True)
break
try:
result = future.result()
checked += 1
if result["error"]:
errors += 1
logger.error("Bulk sync payment check error", extra={
"event": "bulk_sync_check_error",
"phone_number": result["phone_number"],
"error": result["error"],
})
elif result["payment_status"] == "paid":
paid_borrowers.append(result)
# Progress logging every 500 borrowers
if checked % 500 == 0:
elapsed = time.time() - start_time
rate = checked / elapsed if elapsed > 0 else 0
logger.info("Bulk sync progress", extra={
"event": "bulk_sync_progress",
"checked": checked,
"total": total_borrowers,
"percent": checked * 100 // total_borrowers,
"rate": round(rate, 1),
})
except Exception as e:
errors += 1
original = future_to_borrower[future]
logger.error("Bulk sync future error", extra={
"event": "bulk_sync_future_error",
"phone_number": original.get("phone_number", "unknown"),
"error": str(e),
})
api_elapsed = time.time() - start_time
logger.info("Bulk sync API phase completed", extra={
"event": "bulk_sync_api_completed",
"duration_ms": round(api_elapsed * 1000),
"checked": checked,
"paid": len(paid_borrowers),
"errors": errors,
})
# Phase 2: Sequential processing of paid borrowers (DB updates + WhatsApp)
if shutdown_event.is_set():
logger.info("Bulk sync DB updates skipped due to shutdown", extra={
"event": "bulk_sync_db_skipped",
})
return
messages_sent = 0
for borrower in paid_borrowers:
if shutdown_event.is_set():
logger.info("Bulk sync DB updates interrupted", extra={
"event": "bulk_sync_db_interrupted",
})
break
phone_number = borrower["phone_number"]
customer_name = borrower["customer_name"]
try:
logger.info("Bulk sync payment confirmed", extra={
"event": "bulk_sync_payment_confirmed",
"phone_number": phone_number,
"customer_name": customer_name,
})
# Update borrower status to PAID
if update_borrower_paid(phone_number):
logger.debug("Borrower updated to PAID", extra={
"phone_number": phone_number,
})
# Mark any pending PTPs as fulfilled
fulfilled_count = mark_ptps_fulfilled(phone_number)
if fulfilled_count > 0:
logger.debug("PTPs fulfilled in bulk sync", extra={
"phone_number": phone_number,
"count": fulfilled_count,
})
# Check if there's a pending scheduled task
pending_task = get_pending_scheduled_task_for_borrower(phone_number)
if pending_task:
# Has pending task: complete it and send message
message = get_payment_confirmed_message()
send_result = send_whatsapp_message(phone_number, message)
if send_result["success"]:
save_bot_response(phone_number, message, sender='bot-task')
mark_task_completed(pending_task.id, "Payment confirmed (bulk sync)")
messages_sent += 1
logger.info("Bulk sync task completed with message", extra={
"event": "bulk_sync_task_completed",
"task_id": pending_task.id,
"phone_number": phone_number,
"mimin_id": send_result["message_id"],
})
else:
save_bot_response(phone_number, message, sender='bot-task')
mark_task_failed(pending_task.id, f"WhatsApp send failed: {send_result['error']}")
logger.error("Bulk sync task message send failed", extra={
"event": "bulk_sync_task_send_failed",
"task_id": pending_task.id,
"phone_number": phone_number,
"error": send_result["error"],
})
# Rate limit WhatsApp sends
time.sleep(BULK_SYNC_API_DELAY)
else:
# No pending task: silent update (no message)
logger.debug("Bulk sync silent update", extra={
"phone_number": phone_number,
})
except Exception as e:
logger.error("Bulk sync paid borrower processing error", extra={
"event": "bulk_sync_borrower_error",
"phone_number": phone_number,
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "bulk_sync_borrower"})
# Log final summary
total_elapsed = time.time() - sync_start_time
logger.info("Bulk sync completed", extra={
"event": "bulk_sync_completed",
"duration_ms": round(total_elapsed * 1000),
"checked": checked,
"paid": len(paid_borrowers),
"sent": messages_sent,
"errors": errors,
})
metrics.record_duration("bulk_sync_duration", total_elapsed * 1000)
metrics.increment("bulk_sync_paid", value=len(paid_borrowers))
except Exception as e:
logger.error("Bulk sync failed", extra={
"event": "bulk_sync_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "bulk_sync"})
@newrelic.agent.background_task()
def process_morning_followup() -> None:
"""
Send 9 AM follow-up messages to eligible borrowers.
Runs once a day, Monday-Saturday only (hour from FOLLOWUP_HOUR, local time).
Targets late borrowers (days_late > 0) who interacted within 24 hours
but haven't paid yet.
Exclusions:
- Non-borrowers (not in borrowers table)
- Human handoff sessions
- Borrowers with pending PTP (unless PTP is due today)
- Borrowers who already paid (PAID)
- Borrowers not yet late (days_late <= 0)
"""
try:
followup_start_time = time.time()
# Get eligible borrowers
borrowers = get_followup_eligible_borrowers()
if not borrowers:
logger.info("No eligible borrowers for follow-up", extra={
"event": "followup_no_borrowers",
})
return
logger.info("Morning follow-up started", extra={
"event": "followup_start",
"count": len(borrowers),
})
# Counters for summary
sent_count = 0
failed_count = 0
for borrower in borrowers:
# Check for shutdown before processing each borrower
if shutdown_event.is_set():
logger.info("Follow-up interrupted by shutdown", extra={
"event": "followup_shutdown",
})
break
phone_number = borrower["phone_number"]
customer_name = borrower["customer_name"]
days_late = borrower["days_late"]
billing_amount = borrower["billing_amount"]
try:
# Generate appropriate message
message = get_followup_message(
customer_name=customer_name,
days_late=days_late,
billing_amount=billing_amount,
)
# Send via WhatsApp
send_result = send_whatsapp_message(phone_number, message)
if send_result["success"]:
# Save to chat_history with [FOLLOWUP] prefix
save_followup_message(phone_number, message)
# Mark followup sent to prevent duplicates on worker restart
update_followup_sent(phone_number)
sent_count += 1
logger.info("Follow-up sent", extra={
"event": "followup_sent",
"phone_number": phone_number,
"days_late": days_late,
"mimin_id": send_result["message_id"],
})
else:
failed_count += 1
logger.error("Follow-up send failed", extra={
"event": "followup_send_failed",
"phone_number": phone_number,
"error": send_result["error"],
})
except Exception as e:
failed_count += 1
logger.error("Follow-up processing error", extra={
"event": "followup_error",
"phone_number": phone_number,
"error": str(e),
}, exc_info=True)
# Rate limiting: wait between sends
time.sleep(FOLLOWUP_API_DELAY)
# Log summary
elapsed = time.time() - followup_start_time
logger.info("Morning follow-up completed", extra={
"event": "followup_completed",
"duration_ms": round(elapsed * 1000),
"sent": sent_count,
"failed": failed_count,
})
metrics.increment("followup_sent", value=sent_count)
metrics.increment("followup_failed", value=failed_count)
except Exception as e:
logger.error("Morning follow-up failed", extra={
"event": "followup_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "followup"})
# ============================================================================
# BACKGROUND TASK THREAD
# ============================================================================
BACKGROUND_POLL_INTERVAL = 10 # seconds between background task checks
@newrelic.agent.background_task()
def background_task_loop():
"""
Background thread for slow/blocking scheduled tasks.
Runs bulk sync, morning follow-up, scheduled tasks, and PTP expiry
in a separate thread so they do NOT block the main chat polling loop.
This ensures the chatbot remains responsive even when processing
10,000+ borrowers during bulk operations.
"""
logger.info("Background task thread started", extra={
"event": "background_thread_started",
})
set_background_thread_status(True)
# Track timing (local to this thread - thread-safe)
last_scheduler_check = 0
last_ptp_check_date = None
last_bulk_sync_hour = None # Tuple of (date, hour) to track last bulk sync
last_followup_date = None
last_ptp_reminder_date = None
while not shutdown_event.is_set():
try:
current_time = time.time()
current_local = now_local()
current_date = current_local.date()
# 1. Scheduled tasks (every SCHEDULER_POLL_INTERVAL seconds)
if current_time - last_scheduler_check >= SCHEDULER_POLL_INTERVAL:
process_scheduled_tasks()
last_scheduler_check = current_time
# 2. PTP expiry (daily at PTP_CHECK_HOUR, local time)
if (current_local.hour == PTP_CHECK_HOUR_LOCAL and
last_ptp_check_date != current_date):
process_ptp_expiry()
process_paid_borrower_cleanup()
last_ptp_check_date = current_date
# 3. Bulk sync (at BULK_SYNC_HOURS, local time)
if (current_local.hour in BULK_SYNC_HOURS_LOCAL and
last_bulk_sync_hour != (current_date, current_local.hour)):
process_bulk_borrower_sync(current_hour=current_local.hour)
last_bulk_sync_hour = (current_date, current_local.hour)
# 4. Morning follow-up (9 AM Mon-Sat, only during 9:00-9:59 window)
is_weekday = current_local.weekday() < 6 # Mon-Sat
if (is_weekday and
current_local.hour == FOLLOWUP_HOUR_LOCAL and
last_followup_date != current_date):
process_morning_followup()
last_followup_date = current_date
# 5. PTP reminders (9 AM daily - due today, D-1, missed)
if (current_local.hour == PTP_REMINDER_HOUR_LOCAL and
last_ptp_reminder_date != current_date):
process_ptp_reminders()
last_ptp_reminder_date = current_date
except Exception as e:
logger.error("Background task loop error", extra={
"event": "background_loop_error",
"error": str(e),
}, exc_info=True)
metrics.increment("errors", tags={"type": "background_loop"})
# Sleep between checks (responsive to shutdown via wait with timeout)
shutdown_event.wait(timeout=BACKGROUND_POLL_INTERVAL)
set_background_thread_status(False)
logger.info("Background task thread stopped", extra={
"event": "background_thread_stopped",
})
@newrelic.agent.background_task()
def process_session(phone_number: str, graph) -> None:
"""
Process a single user session.
1. Lock session (status = 'processing')
2. Fetch unprocessed messages
3. Aggregate messages into single prompt
4. Invoke LangGraph agent
5. Save bot response to chat_history
6. Mark user messages as processed
7. Unlock session (status = 'idle')
"""
session_start_time = time.time()
# 1. Lock session (atomic - prevents double-processing)
if not lock_session(phone_number):
logger.debug("Session already being processed", extra={
"phone_number": phone_number,
})
return
try:
# 2. Get unprocessed messages (including image data)
messages = get_unprocessed_messages_with_images(phone_number)
message_ids = [msg["id"] for msg in messages] if messages else []
if not messages:
logger.warning("No unprocessed messages found", extra={
"phone_number": phone_number,
})
return
# 3. Aggregate messages into single prompt (with image analysis)
combined_parts = []
image_count = 0
for msg in messages:
text = msg["message_content"]
image_data = msg.get("image_data")
# Check if this is an image message
if image_data and text.startswith("[Image]"):
image_count += 1
caption = text.replace("[Image]", "").strip()
logger.info("Analyzing image", extra={
"event": "image_analysis_start",
"phone_number": phone_number,
"image_count": image_count,
})
# Analyze image using vision LLM
analysis = analyze_image(image_data, caption=caption)
# Build enhanced text with analysis
analysis_text = f"{text}\n\n[Image Analysis]\n- Summary: {analysis['summary']}"
if analysis['ocr_text']:
analysis_text += f"\n- Detected text: {analysis['ocr_text']}"
combined_parts.append(analysis_text)
else:
combined_parts.append(text)
combined_text = "\n\n".join(combined_parts)
if image_count > 0:
logger.info("Images processed", extra={
"event": "images_processed",
"phone_number": phone_number,
"count": image_count,
})
metrics.increment("images_analyzed", value=image_count)
logger.info("Processing session", extra={
"event": "session_processing",
"phone_number": phone_number,
"message_count": len(messages),
})
# Check bot exclusion list (before borrower lookup for efficiency)
if is_excluded_number(phone_number):
logger.info("Excluded number skipped", extra={
"event": "excluded_number_skipped",
"phone_number": phone_number,
})
metrics.increment("sessions_skipped", tags={"reason": "excluded"})
mark_messages_processed(phone_number)
return # Exit early, no reply
# Fetch borrower data (also serves as registration check)
borrower = fetch_borrower_data(phone_number)
if not borrower:
logger.info("Non-borrower message skipped", extra={
"event": "non_borrower_skipped",
"phone_number": phone_number,
})
metrics.increment("sessions_skipped", tags={"reason": "non_borrower"})
mark_messages_processed(phone_number) # Mark as processed so we don't keep picking them up
return # Exit early, no reply
# Check for control group (A/B test - no chatbot responses)
if CONTROL_GROUP_LABELS and borrower.label in CONTROL_GROUP_LABELS:
logger.info("Control group message skipped", extra={
"event": "control_group_skipped",
"phone_number": phone_number,
"label": borrower.label,
})
metrics.increment("sessions_skipped", tags={"reason": "control_group"})
mark_messages_processed(phone_number)
return
# Check for reset command
if combined_text.strip().lower() == RESET_COMMAND:
logger.info("Reset command received", extra={
"event": "reset_command",
"phone_number": phone_number,
})
# Clear chat history
deleted_count = clear_chat_history(phone_number)
# Clear chat session
clear_chat_session(phone_number)
# Clear LangGraph checkpoints
clear_langgraph_checkpoints(phone_number)
# Clear PTP records
ptp_count = clear_ptp_records(phone_number)
# Clear scheduled tasks
task_count = clear_scheduled_tasks(phone_number)
logger.info("Reset completed", extra={
"event": "reset_completed",
"phone_number": phone_number,
"messages_cleared": deleted_count,
"ptp_cleared": ptp_count,
"tasks_cleared": task_count,
})
metrics.increment("resets")
return # Skip normal agent processing
# === INPUT GUARDRAIL ===
input_ok, guardrail_name = validate_input(combined_text)
if not input_ok:
handle_guardrail_failure(phone_number, guardrail_name)
return
# 4. Invoke LangGraph agent with run metadata
config = {
"configurable": {"thread_id": phone_number},
"metadata": {
"phone_number": phone_number,
"session_id": phone_number,
"message_count": len(messages),
# Business metadata for LangSmith filtering/analysis
"borrower_status": borrower.status,
"days_late": borrower.days_late,
"billing_amount": borrower.billing_amount,
"has_image": any("[Image]" in m.get("message_content", "") for m in messages),
},
"run_name": f"chat-{phone_number[-4:]}", # Last 4 digits for privacy
"tags": ["production", borrower.status],
}
state = {
"messages": [HumanMessage(content=combined_text)],
"user_phone": phone_number,
"borrower_details": None,
}
result = graph.invoke(state, config)
# 5. Extract bot response (find last AIMessage with content)
bot_response = None
for msg in reversed(result["messages"]):
if isinstance(msg, AIMessage) and msg.content:
bot_response = msg.content
break
if bot_response:
# === OUTPUT GUARDRAIL ===
output_ok, guardrail_name = validate_output(bot_response)
if not output_ok:
handle_guardrail_failure(phone_number, guardrail_name)
return
# 6. Save bot response to chat_history
save_bot_response(phone_number, bot_response)
# 7. Send via Mimin.io WhatsApp API