
    tĀi                        d Z ddlZddlZddlZddlZddlmZ ddlmZ ej                  j                  d e
 ee      j                               ddlmZ  ee      j                  dz  Zej!                  d        ee      j                  d	z  Z ee      j                  j                  dz  d
z  Zd ZdefdZd Zde
fdZdefdZd Zedk(  r ej8                   e              yy)zV
Cron wrapper for content pipeline.
Handles locking, logging, and error notification.
    N)datetime)Path)run_pipelinelogsTexist_okz.pipeline.lockz	error.logc                     t         dt        j                         j                  d       dz  } t	        j
                  |       }|j                  t        j                         |j                  t	        j                  d             t	        j                         }|j                  t        j                         |j                  t	        j                  d             t	        j                         }|j                  t        j                         |j                  |       |j                  |       t	        j                  t              S )z%Configure logging for cron execution.	pipeline_z%Y-%m-%dz.logz4%(asctime)s - %(name)s - %(levelname)s - %(message)sz%(levelname)s: %(message)s)LOG_DIRr   nowstrftimeloggingFileHandlersetLevelDEBUGsetFormatter	FormatterStreamHandlerINFO	getLogger
addHandler__name__)log_filefile_handlerconsole_handlerroot_loggers       -/var/www/html/content-pipeline/cron_runner.pysetup_loggingr      s    9X\\^%<%<Z%H$INNH &&x0L'--(PQ
 ++-OW\\*  67
 ##%K'<(?+X&&    returnc                  N   	 t        t        d      } t        j                  | t        j                  t        j
                  z         | j                  t        t        j                         j                                      | j                          y# t        t        f$ r Y yw xY w)z0Acquire exclusive lock to prevent parallel runs.wTF)open	LOCK_FILEfcntlflockLOCK_EXLOCK_NBwritestrr   r   	isoformatflushIOErrorOSError)lock_fds    r   acquire_lockr0   6   sq    y#&GU]]U]]:;c(,,.22456W s   BB B$#B$c                  L    	 t         j                          y# t        $ r Y yw xY w)zRelease lock file.N)r$   unlinkFileNotFoundError r   r   release_lockr5   B   s%     s    	##messagec                 n   	 t         j                  j                  d       t        t         d      5 }t	        j
                         j                  d      }|j                  d| d|  d       ddd       y# 1 sw Y   yxY w# t        $ r(}t        d	| t        j                  
       Y d}~yd}~ww xY w)z2Log critical errors to parent project's error log.Tr   az%Y-%m-%d %H:%M:%S[z] [content-pipeline] 
Nz%Failed to write to parent error log: file)PARENT_ERROR_LOGparentmkdirr#   r   r   r   r)   	Exceptionprintsysstderr)r6   f	timestampes       r   log_to_parent_error_logrG   J   s    L%%t%4"C( 	EA //0CDIGGa	{"7yCD	E 	E 	E  L5aS9

KKLs4   0B <A7.B 7B <B  B 	B4B//B4resultc                     t         dz  }	 t        |d      5 }t        j                  | |dt               ddd       y# 1 sw Y   yxY w# t
        $ r(}t        d| t        j                         Y d}~yd}~ww xY w)z(Save pipeline run result for monitoring.zlast_run.jsonr"      )indentdefaultNzFailed to save run result: r;   )	r   r#   jsondumpr*   r@   rA   rB   rC   )rH   result_filerD   rF   s       r   save_run_resultrP   U   so    O+KB+s# 	8qIIfa37	8 	8 	8 B+A3/cjjAABs1   A
 >A
 AA
 A
 
	A;A66A;c            
      *   t               } | j                  d       | j                  d       | j                  d       t               s| j                  d       y	 t	               }t        j                         j                         |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  d	}t!        |       |j                  r;| j                  d|j                          	 t#                | j                  d       y|j                  xs |j                  xs d}| j                  d	|        |j                  rt%        d	|        	 t#                | j                  d       y
# t&        $ rM}| j)                  d       t%        dt+        |              Y d}~t#                | j                  d       y
d}~ww xY w# t#                | j                  d       w xY w)zMain cron entry point.z<============================================================z$Content Pipeline Cron Runner Startedz!Pipeline already running, exitingr   )	rE   successtopic_id
article_id
wp_post_idpost_urlerrorskipped_reasonstatsz!Pipeline completed successfully: z%Content Pipeline Cron Runner FinishedzUnknown errorzPipeline failed:    zUnhandled exception in pipelinezUnhandled exception: N)r   infor0   warningr   r   r   r+   rR   rS   rT   rU   rV   rW   rX   rY   rP   r5   rG   r@   	exceptionr*   )loggerrH   
run_result	error_msgrF   s        r   mainra   _   s   _F
KK
KK67
KK >:;&= "113~~ ++ ++\\$33\\


 	
#>>KK;FOO;LMN" 	;<! P(=(=PILL,YK89 ||'*;I;(GH 	;<  :;"7Ax @A 	;< 	;<s2   B;F 3AF 	G2%(G-G5 -G22G5 5H__main__)__doc__r%   rM   r   rB   r   pathlibr   pathinsertr*   __file__r>   pipeliner   r   r?   r$   r=   r   boolr0   r5   rG   dictrP   ra   r   exitr4   r   r   <module>rl      s   
    
   3tH~,,- . ! x.

&
( t N!!$44	>((//&8;F '6	d 	LS LBD B2=j zCHHTV r   