:_i) dZddlZddlmZddlmZddlmZddlmZddl m Z  ddl m Z d Z n#e$rejd d Z YnwxYwGd d ZdS)zk Notification Manager for Python Scheduler Handles popup notifications and email notifications via Outlook N)datetime)Optional)logger)SchedulerDatabase)SchedulerConfig) ToastNotifierTz6win10toast not available, popup notifications disabledFc eZdZdZdedefdZ ddededed e e d e ef d Z deded e e d e ed e f d Z dededede efdZdedefdZdedefdZdededefdZdZdS)NotificationManagerz(Manages notifications for job executionsdbconfigc||_||_trtnd|_|jot|_tjd|jddS)Nz*NotificationManager initialized (enabled: )) r r TOAST_AVAILABLErtoasterenable_popup_notificationsenabledrinfo)selfr r s 2C:\PYTHON\Scheduler\daemon\notification_manager.py__init__zNotificationManager.__init__sW *9C}t 8L_  PPPPQQQQQN execution_idjob_namestatusduration exit_codec|jsdSd}|dkr|jdrd}nE|dkr|jdrd}n"|dkr|jd rd}|sdS|||||\}}} t j|j|||| fd } | dS) a2 Send notification for job execution result Args: execution_id: Execution ID job_name: Job name status: Execution status ('completed', 'failed', 'timeout', etc.) duration: Duration in seconds exit_code: Process exit code NF completedz"notifications.notify_on_completionTfailedznotifications.notify_on_failuretimeoutznotifications.notify_on_timeouttargetargsdaemon)rr get_build_notification_content threadingThread_send_toast_notificationstart) rrrrrr should_notifytitlemessage icon_paththreads rnotify_job_resultz%NotificationManager.notify_job_result s|  F [ T[__5Y%Z%Z  MM x  DKOO4U$V$V  MM y T[__5V%W%W  M  F%)$D$D fh % % !w !0w :     rreturnc|dkrd|}|rd|dd}nd}d}n5|dkrd |}|d |d }nd }d}n|d kr d|}d}d}n d|}d|}d}|||fS)z$Build notification title and messagerzJob Completed - zCompleted successfully in z.1fszCompleted successfullyNrz Job Failed - zFailed with exit code z. Check logs for details.zFailed. Check logs for details.r zJob Timeout - z3Job exceeded timeout limit. Process was terminated.z Job Status - zStatus: )rrrrrr,r-r.s rr&z/NotificationManager._build_notification_contentIs [ 1x11E 3FxFFFF2II x  .H..E$W9WWW;II y /X//EKGII/H..E)))GIgy((rr,r-r.c |jsdS |jdd}|j||||d|j|dd|d|t j tj d |dS#t$rb}tj d | |j|dd |d|t| n#Yn xYwYd}~dSYd}~dSd}~wwxYw)z Send Windows toast notification Args: execution_id: Execution ID for logging title: Notification title message: Notification message icon_path: Optional icon path N$notifications.popup_duration_seconds T)r,msgr.rthreadedpopupsentz: )rnotification_typerr-sent_atz&Sent popup notification for execution z#Failed to send popup notification: r)rr<rr-error) rr r% show_toastr create_notificationrnowrr Exceptionr>str)rrr,r-r.res rr)z,NotificationManager._send_toast_notificationjs|  F$ {'MrRRH L # ##! $    G ' ')") --G--  (    KOOO P P P P P    LBqBB C C C ++!-&-#$1111a&& ,  s0BB C?C:51C'&C:'C+)C::C?cT|jr|jstjd|dS |jdd}|j|||dtjd|dS#t$r"}tj d|Yd}~dSd}~wwxYw) z Send a custom notification (not tied to a job execution) Args: title: Notification title message: Notification message z)Custom notification not sent (disabled): Nr6r7T)r,r8rr9zSent custom notification: z$Failed to send custom notification: ) rrrdebugr r%r?rrBr>)rr,r-rrDs rsend_custom_notificationz,NotificationManager.send_custom_notifications| 4<  LLULL M M M F E{'MrRRH L # #! $    KrB)rrOrPrQrS connectorrDs rrKz,NotificationManager._send_email_notifications F J J J J J J((**I  "" V$$G$$GGGGG TUUUUU F F F LDDD E E E E E E E E E Fsrgs ++++++''''''((((((OOFNKLLLOOO W W W W W W W W W W s/A A