
    Ҟi                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ  ed	d
      d        Z ed	d      d        Zy)u]   
Celery worker для отправки запланированных уведомлений
    )shared_task)Session)datetime)SessionLocal)ScheduledNotificationNotificationCustomerTemplateUserIndustryTemplateIndustryTemplate)enqueue_notification)get_settingsTz9app.workers.scheduled_sender.send_scheduled_notifications)bindnamec                    t               }	 t        j                         }t               }|j                  }|j                  t              j                  t        j                  |k  t        j                  dk(        j                  t        j                  j                               j                  |      j                         }d}|D ]V  }	 |j                  t              j                  t        j                  |j                   k(        j#                         }|sd|_	        \d}	d}
|j$                  r]|j                  t&              j                  t&        j                  |j$                  k(        j#                         }	|	r|	j(                  nd}
|j*                  r|j                  t,              j                  t,        j                  |j*                  k(        j#                         }|r|j.                  r|j.                  }
n]|r[|j                  t0              j                  t0        j                  |j2                  k(        j#                         }|r|j(                  }
|
s	d|_	        t5        |j6                  |j$                  |j                   |j8                  xs i d      }|j;                  |       |j=                          t?        j@                  |j                         d|_	        t        j                         |_!        |j                  |_"        |dz  }Y |jK                          d
||jM                         d|jO                          S # tF        $ r-}tI        d|j                   d	|        d|_	        Y d}~d}~ww xY w# tF        $ r6}tI        d|        dtQ        |      dcY d}~|jO                          S d}~ww xY w# |jO                          w xY w)u   
    Отправка запланированных уведомлений.
    Запускается каждую минуту через Celery Beat.
    pendingr   failedN)	tenant_idtemplate_idcustomer_idpayloadstatussent   z%Error sending scheduled notification z: success)r   
sent_count
checked_atz'Error in send_scheduled_notifications: error)r   r   ))r   r   utcnowr   "SCHEDULED_NOTIFICATIONS_BATCH_SIZEqueryr   filterscheduled_atr   order_byasclimitallr	   idr   firstr   r
   contentuser_template_idr   custom_contentr   industry_template_idr   r   r   addflushr   delaysent_atnotification_id	Exceptionprintcommit	isoformatclosestr)selfdbnowsettings
batch_sizescheduled_listr   	scheduledcustomertemplater*   user_templateindustry_templatenotificationes                  6/var/www/notifications/app/workers/scheduled_sender.pysend_scheduled_notificationsrG      sI    .Bboo
  >@@
"78??!..#5!((I5
 ((5599;
<UU:=Nssu 	
 
' A	I@88H-44KK9#8#88%'   '/I$  ((!xx188 y'<'<< eg  3;h..G --$&HH-A$B$I$I,//93M3MM%eg " %)E)E"/">">&,.HH5E,F,M,M,//=3U3UU-%' * -&7&?&?G'/I$  ,'11 ) 5 5 ) 5 5%--3$  |$
 %**<??; $*	 $,OO$5	!,8OO	)a
yA	F 			  $--/
 	
)  =ill^2aSQR#+	   
7s;<V
 	

 	

 	
su   CN AM$N %EM,N .B2M &N 	N "NN NN 	OO
4O5O 
OO O$z2app.workers.scheduled_sender.cleanup_old_scheduledc                 ~   t               }	 ddlm} t        j                          |d      z
  }|j	                  t
              j                  t
        j                  dk(  t
        j                  |k        j                         }|j                          d|d|j                          S # |j                          w xY w)u   
    Очистка старых отправленных уведомлений
    Запускается раз в день
    r   )	timedelta   )daysr   r   )r   deleted_count)r   r   rI   r   r!   r   r"   r   r1   deleter5   r7   )r9   r:   rI   cutoff_datedeleteds        rF   cleanup_old_scheduledrP   ~   s     .B& oo')*<<((0188!((F2!))K7
 &( 	
 			  $
 	

s   BB* *B<N)__doc__celeryr   sqlalchemy.ormr   r   app.db.sessionr   app.db.modelsr   r   r	   r
   r   r   app.workers.notificationsr   app.core.configr   rG   rP        rF   <module>rZ      sg     "  '  ; ( $XYi ZiX $QR SrY   