o s'i@sdZddlZddlmZddlmZddlmZddlmZddl m Z z ddl m Z d Z Wney>ed d Z YnwGd d d ZdS)zf Notification Manager for Python Scheduler Handles popup notifications and future email notifications N)datetime)Optional)logger)SchedulerDatabase)SchedulerConfig) ToastNotifierTz6win10toast not available, popup notifications disabledFc @seZdZdZdedefddZ ddeded ed e e d e ef d d Z ded ed e e d e ede f ddZ dededede efddZdedefddZddZdS)NotificationManagerz(Manages notifications for job executionsdbconfigcCs@||_||_tr tnd|_|jot|_td|jddS)Nz*NotificationManager initialized (enabled: )) r r TOAST_AVAILABLErtoasterenable_popup_notificationsenabledrinfo)selfr r r2c:\PYTHON\Scheduler\daemon\notification_manager.py__init__s  zNotificationManager.__init__N execution_idjob_namestatusduration exit_codec Cs|jsdSd}|dkr|jdrd}n|dkr!|jdr!d}n |dkr-|jd r-d}|s1dS|||||\}}} tj|j|||| fdd } | 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_timeout)targetargsdaemon)rr get_build_notification_content threadingThread_send_toast_notificationstart) rrrrrrZ should_notifytitlemessage icon_paththreadrrrnotify_job_result s(    z%NotificationManager.notify_job_resultreturncCs|dkrd|}|rd|dd}nd}d}n3|dkr1d |}|dur,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.rzJob Timeout - z3Job exceeded timeout limit. Process was terminated.z Job Status - zStatus: r)rrrrrr&r'r(rrrr!Is(      z/NotificationManager._build_notification_contentr&r'r(c Cs|jsdSz/|jdd}|jj||||dd|jj|dd|d|td t d |WdSt yo}z/t d |z|jj|dd |d|t |d Wn YWYd}~dSWYd}~dSd}~ww)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(rthreadedZpopupsentz: )rnotification_typerr'sent_atz&Sent popup notification for execution z#Failed to send popup notification: r)rr2rr'error) r r r show_toastr create_notificationrnowrr Exceptionr4str)rrr&r'r(rerrrr$jsD     z,NotificationManager._send_toast_notificationc Cs|jr|jstd|dSz|jdd}|jj|||ddtd|WdStyG}zt d|WYd}~dSd}~ww) z Send a custom notification (not tied to a job execution) Args: title: Notification title message: Notification message z)Custom notification not sent (disabled): Nr-r.T)r&r/rr0zSent custom notification: z$Failed to send custom notification: ) rr rdebugr r r5rr8r4)rr&r'rr:rrrsend_custom_notifications z,NotificationManager.send_custom_notificationcCs|jddddS)zSend a test notificationz$Python Scheduler - Test NotificationzMThis is a test notification from Python Scheduler. Notifications are working!)r&r'N)r<)rrrrtest_notifications z%NotificationManager.test_notification)NN)__name__ __module__ __qualname____doc__rrrintr9rfloatr*tupler!r$r<r=rrrrrs. ) ! 4 r)rAr"rtypingrlogurur core.databaser core.configrZ win10toastrr ImportErrorwarningrrrrrs