
    !ɀiz                        d Z ddlZddlZddlZddlZddlmZmZ ddlmZ dZ	dZ
 ee      j                  Zedz  Zej                  d	        ej                   ej"                  d
 ej$                  edz         ej&                         g        ej(                  e      ZdefdZdedefdZdefdZd Zedk(  r ej:                   e              yy)a  
Daily scheduler for content pipeline.
Runs at a random time between 08:00 and 19:00.

Usage:
    - Schedule this script to run early morning via cron (e.g., 00:05)
    - It will sleep until a random time in the configured window
    - Then execute the pipeline

Cron entry:
    5 0 * * * cd /var/www/html/content-pipeline && ./venv/bin/python daily_scheduler.py >> logs/scheduler.log 2>&1
    N)datetime	timedelta)Path      logsT)exist_okz)%(asctime)s - %(levelname)s - %(message)szscheduler.log)levelformathandlersreturnc                      t        j                         } | j                  dddd      }t        dz  }t        dz  }t        j                  ||dz
        }|t        |      z   }|S )z@Generate a random datetime between start and end hour for today.r   )hourminutesecondmicrosecond<      )minutes)r   nowreplacePUBLISH_WINDOW_START_HOURPUBLISH_WINDOW_END_HOURrandomrandintr   )r   todaystart_minutesend_minutesrandom_minutepublish_times         1/var/www/html/content-pipeline/daily_scheduler.pyget_random_publish_timer"   +   sf    
,,.CKKQqKBE .2M)B.K NN=+/BM9];;L     target_timec           
      "   t        j                         }|| k\  r)t        j                  d| j	                  d       d       y| |z
  j                         }t        t        |      d      \  }}t        |d      \  }}t        j                  d| d| d	| j	                  d              d
}|dkD  r_t        ||      }t        j                  |       ||z  }|dkD  r.t        t        |            }	t        j                  d|	 d       |dkD  r_y)ze
    Sleep until the target time.
    Returns True if we slept, False if target time has passed.
    zTarget time z%H:%Mz  has passed, running immediatelyFi  r   zSleeping for zh zm until i,  r   )secondszStill waiting... z
 remainingT)r   r   loggerinfostrftimetotal_secondsdivmodintmintimesleepr   debug)
r$   r   sleep_secondshours	remainderr   r&   
chunk_sizesleep_duration	remainings
             r!   sleep_untilr7   >   s   
 ,,.C
kl;#7#7#@"AAabc 3&557Mc-0$7E9i,GW
KK-wb	+:N:Nw:W9XYZ J
!
Z7

>"'1!#m*<=ILL,YKzBC !
 r#   c                      t         j                  d       	 t        j                  j	                  dt        t                     ddlm}   |        S # t        $ r"}t         j                  d|        Y d}~yd}~ww xY w)zExecute the content pipeline.zStarting content pipeliner   )mainzPipeline execution failed: Nr   )r'   r(   syspathinsertstrPROJECT_DIRcron_runnerr9   	Exception	exception)	cron_maines     r!   run_pipelinerD   ]   s_    
KK+,	3{+,1{ 6qc:;s   9A 	A<A77A<c                  $   t         j                  d       t         j                  d       t         j                  dt        ddt        dd       t         j                  d       t	               } t         j                  d| j                  d              t        d	z  }t        |d
      5 }|j                  | j                          d       ddd       t        |        t               }t         j                  d|        |S # 1 sw Y   8xY w)zMain scheduler entry point.z<============================================================zDaily Content Scheduler StartedzPublish window: 02dz:00 - z:00zSelected publish time: z%Y-%m-%d %H:%Mzscheduled_time.txtw
Nz#Scheduler finished with exit code: )r'   r(   r   r   r"   r)   LOG_DIRopenwrite	isoformatr7   rD   )r    schedule_filefresults       r!   r9   r9   m   s    
KK
KK12
KK"#<S"AH_`cGddghi
KK +,L
KK),*?*?@P*Q)RST 22M	mS	! 1Q	<))+,B/01  ^F
KK5fX>?M1 1s   ,#DD__main__)__doc__loggingr   r:   r.   r   r   pathlibr   r   r   __file__parentr>   rI   mkdirbasicConfigINFOFileHandlerStreamHandler	getLogger__name__r'   r"   boolr7   r,   rD   r9   exit r#   r!   <module>r`      s      
  (     8n##

 t    
,,6Go56 
		8	$ &X $ >c  4 zCHHTV r#   