
    &i                         d Z ddl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
 ddlmZ dd	lmZ  ej$                  e      Z e       Zd Zd Zd Zd ZddededefdZy# e$ rZ ed
e        Y dZ[FdZ[ww xY w)zV
Product Fetch Cron Jobs

Simple cron jobs for daily product fetch using APScheduler.
    N)BackgroundScheduler)CronTrigger)datetime)Session)get_db_session)ProductFetchCronLog)product_fetchz,Warning: Could not import required modules: c                  R   t               } d}	 t        j                  d       t        ddddt	        j
                               }| j                  |       | j                          | j                  |       t        j                  d|j                          t        j                  dd	dd
      }t        j                  d|j                          |j                  |_        | j                          t        j                  d       | j'                          y# t        $ rm}t        j                  dt        |              |r@d|_        t        |      |_        t	        j
                         |_        | j                          Y d}~d}~ww xY w# | j'                          w xY w)z2Daily cron job to fetch products from external APINz%Starting daily product fetch cron jobstarted      
   )status
company_idtotal_pages	page_size
started_atz Created cron log entry with ID:    )r   page_numberr   r   z$Product fetch task started with ID: zCDaily product fetch cron job completed - task running in backgroundz'Error in daily product fetch cron job: failed)r   loggerinfor   r   utcnowaddcommitrefreshidr	   delaytask_id	Exceptionerrorstrr   error_messagecompleted_atclose)dbcron_logtaskes       E/var/www/html/hubwallet-dev/src/smart_inventory/jobs/products_cron.pydaily_product_fetch_jobr+      sL    "BH-;< '(
 	x
		


86x{{mDE ""	
 	:477)DE  77
		 	YZ 	
  >s1vhGH&HO%(VH"$,OO$5H!IIK 	
s+   C<D 	F$A#FF FF F&c                  d   	 t         j                  rt        j                  d       yt         j	                  t
        t        dd      ddd	       t         j                          t        j                  d
       y# t        $ r+} t        j                  dt        |               Y d} ~ yd} ~ ww xY w)z!Start the product fetch cron jobsz*Product fetch scheduler is already runningNr   r   )hourminutedaily_product_fetchzDaily Product FetchT)functriggerr   namereplace_existingzGProduct fetch cron job started successfully - will run daily at 2:00 AMz'Error starting product fetch cron job: )	schedulerrunningr   r   add_jobr+   r   startr    r!   r"   r)   s    r*   start_product_cronr9   O   s    IKKDE 	(Qq1$&! 	 	
 	]^ I>s1vhGHHIs   %A; AA; ;	B/!B**B/c                     	 t         j                  r*t         j                          t        j	                  d       yt        j	                  d       y# t
        $ r+} t        j                  dt        |               Y d} ~ yd} ~ ww xY w)z Stop the product fetch cron jobszProduct fetch cron jobs stoppedz*Product fetch scheduler is already stoppedz(Error stopping product fetch cron jobs: N)r4   r5   shutdownr   r   r    r!   r"   r8   s    r*   stop_product_cronr<   g   sd    J KK9:KKDE J?AxHIIJs   9A A 	B!BBc            	         	 g } t         j                         D ]e  }| j                  |j                  |j                  |j
                  r|j
                  j                         ndt        |j                        d       g t         j                  | dS # t        $ r8}t        j                  dt        |              dt        |      icY d}~S d}~ww xY w)z)Get the status of product fetch cron jobsN)r   r2   next_run_timer1   )scheduler_runningjobszError getting cron status: r!   )r4   get_jobsappendr   r2   r>   	isoformatr"   r1   r5   r    r   r!   )r@   jobr)   s      r*   get_cron_statusrE   s   s    !%%' 	CKKffBEBSBS!2!2!<!<!>Y]s{{+	 	 "+!2!2
 	
  !23q6(;<Q  !s   BB 	C-C	CCr   r   r   c                     	 t         j                  d|  d|        t                dddS # t        $ r9}t         j	                  dt        |              dt        |      dcY d}~S d}~ww xY w)	z,Manually trigger product fetch (for testing)z.Manually triggering product fetch: company_id=z, total_pages=TzManual product fetch triggered)successmessagezError in manual product fetch: FN)r   r   r+   r    r!   r"   )r   r   r   r)   s       r*   trigger_manual_fetchrI      so    
5DZLP^_j^klm 	 !,LMM 56s1vh?@ SV445s   ), 	A..A)#A.)A.)r   r   r   )__doc__logging!apscheduler.schedulers.backgroundr   apscheduler.triggers.cronr   r   sqlalchemy.ormr   src.utils.dbr   -src.smart_inventory.apps.products.cron_modelsr   src.smart_inventory.tasks.tasksr	   ImportErrorr)   print	getLogger__name__r   r4   r+   r9   r<   rE   intrI        r*   <module>rY      s     A 1  ">+Q= 
		8	$  !	2jI0	J!*5S 53 5s 5o  >	8
<==>s   A# #A=(A88A=