75
75
from types import UnionType
76
76
from typing import Any , Callable , TypeVar , Union , get_args , get_origin
77
77
78
- _log = logging .getLogger (__name__ )
78
+ _logger = logging .getLogger (__name__ )
79
79
80
80
81
81
DataclassT = TypeVar ("DataclassT" , bound = "FromDictMixin" )
@@ -346,7 +346,7 @@ def start(self, task: Callable[..., None], **kwargs: Any) -> None:
346
346
"""
347
347
self .task = task
348
348
self ._task_name = task .__name__
349
- _log .info (
349
+ _logger .info (
350
350
"Starting scheduler for task '%s' to execute every %d seconds and %s" ,
351
351
self ._task_name ,
352
352
self ._config .interval ,
@@ -365,18 +365,18 @@ def stop(self) -> None:
365
365
"""Stop the scheduler."""
366
366
if self ._thread is not None :
367
367
if self ._thread .is_alive ():
368
- _log .info ("Stopping scheduler for task '%s'" , self ._task_name )
368
+ _logger .info ("Stopping scheduler for task '%s'" , self ._task_name )
369
369
self ._stop_event .set ()
370
370
if not self ._stop_event .is_set ():
371
- _log .error (
371
+ _logger .error (
372
372
"Failed to stop scheduler for task '%s'" , self ._task_name
373
373
)
374
374
else :
375
- _log .warning (
375
+ _logger .warning (
376
376
"Attempted to stop scheduler for task '%s', but no active thread was found." ,
377
377
self ._task_name ,
378
378
)
379
- _log .info ("Scheduler successfully stopped" )
379
+ _logger .info ("Scheduler successfully stopped" )
380
380
381
381
def _run_task (self , kwargs : dict [str , Any ]) -> None :
382
382
"""Run the scheduled task.
@@ -389,19 +389,21 @@ def _run_task(self, kwargs: dict[str, Any]) -> None:
389
389
elapsed = self ._execute_task (kwargs )
390
390
self ._pace (elapsed )
391
391
else :
392
- _log .info (
392
+ _logger .info (
393
393
"Waiting for first interval before sending the first notification."
394
394
)
395
395
self ._pace (0 )
396
396
while not self ._should_stop ():
397
397
elapsed = self ._execute_task (kwargs )
398
398
self ._pace (elapsed )
399
- _log .info ("Scheduler stopping: stop condition met." )
399
+ _logger .info ("Scheduler stopping: stop condition met." )
400
400
self .stop ()
401
401
402
402
def _should_stop (self ) -> bool :
403
403
"""Return True if the scheduler should stop."""
404
- _log .debug ("Checking if scheduler for task '%s' should stop." , self ._task_name )
404
+ _logger .debug (
405
+ "Checking if scheduler for task '%s' should stop." , self ._task_name
406
+ )
405
407
return self ._stop_event .is_set () or (
406
408
self ._config .duration is not None
407
409
and self ._start_time is not None
@@ -422,14 +424,14 @@ def _execute_task(self, kwargs: dict[str, Any]) -> float:
422
424
if self .task :
423
425
self .task (** kwargs )
424
426
except Exception as e : # pylint: disable=broad-except
425
- _log .error (
427
+ _logger .error (
426
428
"Error occurred during scheduled execution of %s: %s" ,
427
429
self ._task_name ,
428
430
e ,
429
431
)
430
432
finally :
431
433
task_elapsed = time .time () - task_start_time
432
- _log .debug (
434
+ _logger .debug (
433
435
"Execution of task '%s' completed in %.2f seconds." ,
434
436
self ._task_name ,
435
437
task_elapsed ,
@@ -465,7 +467,9 @@ def _pace(self, elapsed_task_time: float) -> None:
465
467
actual_sleep = max (0 , sleep_duration - elapsed_task_time )
466
468
if self ._stop_event .is_set ():
467
469
return
468
- _log .info ("Sleeping for %.2f seconds before next task execution." , actual_sleep )
470
+ _logger .info (
471
+ "Sleeping for %.2f seconds before next task execution." , actual_sleep
472
+ )
469
473
self ._stop_event .wait (actual_sleep )
470
474
471
475
@@ -503,15 +507,17 @@ def send_with_retry(
503
507
for attempt in range (retries + 1 ):
504
508
try :
505
509
send_func (** kwargs )
506
- _log .info ("Successfully sent notification on attempt %d" , attempt + 1 )
510
+ _logger .info (
511
+ "Successfully sent notification on attempt %d" , attempt + 1
512
+ )
507
513
return
508
514
except Exception as e : # pylint: disable=broad-except
509
515
last_exception = e
510
- _log .error ("Attempt %d failed: %s" , attempt + 1 , e )
516
+ _logger .error ("Attempt %d failed: %s" , attempt + 1 , e )
511
517
if attempt < retries - 1 :
512
518
linear_backoff = backoff_factor * (attempt + 1 )
513
519
time .sleep (min (max_sleep , linear_backoff ))
514
- _log .error ("Failed to send notification after %d retries" , retries )
520
+ _logger .error ("Failed to send notification after %d retries" , retries )
515
521
raise NotificationSendError (
516
522
"Notification failed after all retry attempts." ,
517
523
last_exception = last_exception ,
@@ -520,17 +526,17 @@ def send_with_retry(
520
526
def start_scheduler (self ) -> None :
521
527
"""Start the scheduler if configured."""
522
528
if self ._scheduler :
523
- _log .info ("Starting scheduler for %s" , self .__class__ .__name__ )
529
+ _logger .info ("Starting scheduler for %s" , self .__class__ .__name__ )
524
530
self ._scheduler .start (self .send )
525
531
else :
526
- _log .warning ("No scheduler config provided. Cannot start scheduler." )
532
+ _logger .warning ("No scheduler config provided. Cannot start scheduler." )
527
533
528
534
def stop_scheduler (self ) -> None :
529
535
"""Stop the running scheduler."""
530
536
if not self ._scheduler :
531
- _log .warning ("No active scheduler to stop." )
537
+ _logger .warning ("No active scheduler to stop." )
532
538
return
533
- _log .info ("Stopping scheduler for notification: %s" , self .__class__ .__name__ )
539
+ _logger .info ("Stopping scheduler for notification: %s" , self .__class__ .__name__ )
534
540
self ._scheduler .stop ()
535
541
536
542
@abstractmethod
@@ -559,7 +565,7 @@ def __init__(self, config: EmailConfig) -> None:
559
565
super ().__init__ ()
560
566
self ._config : EmailConfig = config
561
567
if self ._config .scheduler :
562
- _log .debug (
568
+ _logger .debug (
563
569
"EmailNotification configured with scheduler: %s" ,
564
570
self ._config .scheduler ,
565
571
)
@@ -625,9 +631,9 @@ def _attach_files(self, msg: EmailMessage, attachments: list[str]) -> None:
625
631
)
626
632
except OSError as e :
627
633
failed_attachments .append (file )
628
- _log .error ("Failed to attach file %s: %s" , file , e )
634
+ _logger .error ("Failed to attach file %s: %s" , file , e )
629
635
if failed_attachments :
630
- _log .warning (
636
+ _logger .warning (
631
637
"The following attachments could not be added: %s" , failed_attachments
632
638
)
633
639
@@ -676,9 +682,9 @@ def _connect_and_send(
676
682
server .starttls ()
677
683
server .login (str (smtp_settings ["user" ]), str (smtp_settings ["password" ]))
678
684
server .send_message (msg )
679
- _log .info ("Email sent successfully to %s" , to_emails )
685
+ _logger .info ("Email sent successfully to %s" , to_emails )
680
686
except SMTPException as e :
681
- _log .error ("Failed to send email: %s" , e )
687
+ _logger .error ("Failed to send email: %s" , e )
682
688
raise
683
689
684
690
0 commit comments