GlisWeb framework
_cron.php
Vai alla documentazione di questo file.
1 <?php
2 
13  // inclusione del framework
14  require '../../_src/_config.php';
15 
16  // costanti che descrivono lo stato di funzionamento del framework
17  define( 'CRON_RUNNING' , 'CRONRUN' );
18 
19  // apro il report
20  $cHnd = fopen( DIRECTORY_BASE . DIRECTORY_LOG . 'latest/cron.latest.log', WRITE_FILE_OVERWRITE );
21 
22  // log
23  logWrite( 'chiamata cron API', 'cron', LOG_INFO );
24 
25  // lock delle tabelle
26  mysqlQuery( $cf['mysql']['connection'], 'LOCK TABLES cron WRITE, cron_log WRITE, job WRITE' );
27 
28  // tempo
29  $time = time();
30 
31  // output
32  $cf['cron']['results'] = array();
33 
34  // seleziono i task con profili di schedulazione compatibili con l'orario corrente
36  $cf['mysql']['connection'],
37  'SELECT * FROM cron WHERE '.
38  '( minuto = ? OR minuto IS NULL ) AND '.
39  '( ora = ? OR ora IS NULL ) AND '.
40  '( giorno_del_mese = ? OR giorno_del_mese IS NULL ) AND '.
41  '( mese = ? OR mese IS NULL ) AND '.
42  '( giorno_della_settimana = ? OR giorno_della_settimana IS NULL ) AND '.
43  '( settimana = ? OR settimana IS NULL ) AND '.
44  '( from_unixtime( timestamp_esecuzione, "%Y%m%d%H%i") < ? OR timestamp_esecuzione IS NULL ) ',
45  array(
46  array( 's' => intval( date( 'i', $time ) ) ), //
47  array( 's' => date( 'G', $time ) ), //
48  array( 's' => date( 'j', $time ) ), //
49  array( 's' => date( 'n', $time ) ), //
50  array( 's' => date( 'w', $time ) ), // 0 - 6, 0 -> domenica
51  array( 's' => date( 'W', $time ) ), // 1 - 52/53
52  array( 's' => date( 'YmdHi', $time ) )
53  )
54  );
55 
56  // log
57  logWrite( 'criteri di ricerca -> ' . date( 'i', $time ) . ' ' . date( 'G', $time ) . ' ' . date( 'j', $time ) . ' ' . date( 'n', $time ) . ' ' . date( 'w', $time ) . ' ' . date( 'W', $time ), 'cron', LOG_INFO );
58 
59  // timestamp di esecuzione iniziale
60  foreach( $tasks as $task ) {
61  mysqlQuery( $cf['mysql']['connection'], 'UPDATE cron SET timestamp_esecuzione = ? WHERE id = ?', array( array( 's' => $time ), array( 's' => $task['id'] ) ) );
62  }
63 
64  // seleziono i job attivi
66  $cf['mysql']['connection'],
67  'SELECT * FROM job WHERE '.
68  'timestamp_apertura <= ? AND timestamp_apertura IS NOT NULL AND timestamp_completamento IS NULL '.
69  'AND ( from_unixtime( timestamp_esecuzione, "%Y%m%d%H%i") < ? OR timestamp_esecuzione IS NULL )',
70  array(
71  array( 's' => $time ),
72  array( 's' => date( 'YmdHi', $time ) )
73  )
74  );
75 
76  // log
77  logWrite( 'trovati ' . count( $jobs ) . ' job', 'cron', LOG_DEBUG );
78 
79  // timestamp di esecuzione iniziale
80  foreach( $jobs as $job ) {
81  if( file_exists( DIRECTORY_BASE . $job['job'] ) ) {
82  mysqlQuery( $cf['mysql']['connection'], 'UPDATE job SET timestamp_esecuzione = ? WHERE id = ?', array( array( 's' => $time ), array( 's' => $job['id'] ) ) );
83  }
84  }
85 
86  // unlock delle tabelle
87  mysqlQuery( $cf['mysql']['connection'], 'UNLOCK TABLES' );
88 
89  // ciclo sui task
90  foreach( $tasks as $task ) {
91  if( file_exists( DIRECTORY_BASE . $task['task'] ) ) {
92 
93 # // verifico che il task non sia già stato eseguito nel minuto corrente
94 # if( date( 'YmdHi', $task['timestamp_esecuzione'] ) != date( 'YmdHi', $time ) ) {
95 
96  // resetto lo status
97  $status = array();
98 
99  // latest
100  fwrite( $cHnd, 'eseguo ' . $task['task'] . ' x' . $task['iterazioni'] . PHP_EOL );
101 
102  // log
103  logWrite( 'eseguo il task ' . $task['id'] . ' -> ' . $task['task'], 'cron', LOG_DEBUG );
104 
105 # // array dei risultati
106 # $cf['cron']['results'] = array();
107 
108  // eseguo il task
109  for( $iter = 0; $iter < $task['iterazioni']; $iter++ ) {
110  logWrite( 'iterazione #' . $iter . ' per il task ' . $task['id'] . ' -> ' . $task['task'], 'cron', LOG_DEBUG );
111  fwrite( $cHnd, 'iterazione #' . $iter . PHP_EOL );
112  require DIRECTORY_BASE . $task['task'];
113  $cf['cron']['results']['task'][ $task['task'] ][ $task['id'] ] = array_replace_recursive( $status, array( 'esecuzione' => time() ) );
114  if( ! isset( $task['delay'] ) || empty( $task['delay'] ) ) { $task['delay'] = 3; }
115  sleep( $task['delay'] );
116  }
117 
118  // aggiorno il log
119  mysqlQuery( $cf['mysql']['connection'], 'INSERT INTO cron_log ( id_cron, testo, timestamp_esecuzione ) VALUES ( ?, ?, ? )', array( array( 's' => $task['id'] ), array( 's' => json_encode( $cf['cron']['results'] ) ), array( 's' => $time ) ) );
120 
121  // aggiorno la tabella di pianificazione
122  mysqlQuery( $cf['mysql']['connection'], 'UPDATE cron SET timestamp_esecuzione = ? WHERE id = ?', array( array( 's' => $time ), array( 's' => $task['id'] ) ) );
123 
124  // latest
125  fwrite( $cHnd, print_r( $status, true ) . PHP_EOL );
126 
127 # } else {
128 
129 # // log
130 # logWrite( 'task ' . $task['id'] . ' già eseguito alle ' . date( 'Y-m-d H:i', $time ), 'cron', LOG_NOTICE );
131 
132 # }
133 
134  }
135  }
136 
137  // ciclo sui job
138  foreach( $jobs as $job ) {
139  if( file_exists( DIRECTORY_BASE . $job['job'] ) ) {
140 
141 # // verifico che il job non sia già stato eseguito nel minuto corrente
142 # if( date( 'YmdHi', $job['timestamp_esecuzione'] ) != date( 'YmdHi', $time ) ) {
143 
144  // log
145  logWrite( 'eseguo il job ' . $job['id'] . ' -> ' . $job['job'], 'cron', LOG_DEBUG );
146 
147 # // array dei risultati
148 # $cf['cron']['results'] = array();
149 
150  // eseguo il job
151  require DIRECTORY_BASE . $job['job'];
152 
153  // integro la timestamp di esecuzione
154  $cf['cron']['results']['job'][ $job['job'] ][ $job['id'] ] = array_replace_recursive( $status, array( 'esecuzione' => time() ) );
155 
156  // aggiorno la tabella di avanzamento lavori
157  mysqlQuery( $cf['mysql']['connection'], 'UPDATE job SET timestamp_esecuzione = ?, workspace = ? WHERE id = ?', array( array( 's' => $time ), array( 's' => serialize( $wksp ) ), array( 's' => $job['id'] ) ) );
158 
159 # } else {
160 
161 # // log
162 # logWrite( 'job ' . $job['id'] . ' già eseguito alle ' . date( 'Y-m-d H:i', $time ), 'cron', LOG_NOTICE );
163 
164 # }
165 
166  }
167  }
168 
169  // log
170  appendToFile( '-- ' . date( 'Y-m-d H:i:s' ) . PHP_EOL . print_r( $cf['cron']['results'], true ), 'var/log/cron/' . date( 'Ymd' ) . '.log' );
171 
172  // output
173  buildJson( $cf['cron']['results'] );
174 
175  // chiusura latest
176  fclose( $cHnd );
177 
178 ?>
foreach( $tasks as $task) $jobs
Definition: _cron.php:65
appendToFile( $t, $f, &$e=NULL)
aggiunge una stringa a un file
$cf['cron']['results']
Definition: _cron.php:32
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
mysqlQuery( $c, $q, $p=false, &$e=array())
$tasks
Definition: _cron.php:35
$time
Definition: _cron.php:29
$wksp['log']
const DIRECTORY_LOG
Definition: _config.php:267
const WRITE_FILE_OVERWRITE
const DIRECTORY_BASE
Definition: _osm.php:3
buildJson( $content, $encoding=ENCODING_UTF8, $headers=array())
$cHnd
Definition: _cron.php:20