* Don't kill child processes during normal termination, that will leave the last...
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * This script starts pending jobs.
4 *
5 * Usage:
6 * --maxjobs <num> (default 10000)
7 * --type <job_cmd>
8 *
9 * @file
10 * @ingroup Maintenance
11 */
12
13 $optionsWithArgs = array( 'maxjobs', 'type', 'procs' );
14 $wgUseNormalUser = true;
15 require_once( 'commandLine.inc' );
16
17 if ( isset( $options['procs'] ) ) {
18 $procs = intval( $options['procs'] );
19 if ( $procs < 1 || $procs > 1000 ) {
20 echo "Invalid argument to --procs\n";
21 exit( 1 );
22 }
23
24 // Don't share DB or memcached connections
25 $lb = wfGetLB();
26 $lb->closeAll();
27 $wgCaches = array();
28 unset( $wgMemc );
29
30 // Create the child processes
31 $children = array();
32 for ( $childId = 0; $childId < $procs; $childId++ ) {
33 $pid = pcntl_fork();
34 if ( $pid === -1 || $pid === false ) {
35 echo "Error creating child processes\n";
36 exit( 1 );
37 }
38 if ( !$pid ) {
39 break;
40 }
41
42 $children[$pid] = true;
43 }
44 if ( $pid ) {
45 // Parent process
46 // Trap SIGTERM
47 pcntl_signal( SIGTERM, 'handleTermSignal', false );
48 // Wait for a child to exit
49 $status = false;
50 $termReceived = false;
51 do {
52 $deadPid = pcntl_wait( $status );
53 // Run signal handlers
54 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
55 pcntl_signal_dispatch();
56 } else {
57 declare (ticks=1) { $status = $status; }
58 }
59 if ( $deadPid > 0 ) {
60 unset( $children[$deadPid] );
61 }
62 // Respond to TERM signal
63 if ( $termReceived ) {
64 foreach ( $children as $childPid => $unused ) {
65 posix_kill( $childPid, SIGTERM );
66 }
67 $termReceived = false;
68 }
69 } while ( count( $children ) );
70 // All done
71 exit( 0 );
72 }
73
74 // Set up this child
75 $wgMemc = wfGetCache( $wgMainCacheType );
76 }
77
78 if ( isset( $options['maxjobs'] ) ) {
79 $maxJobs = $options['maxjobs'];
80 } else {
81 $maxJobs = 10000;
82 }
83
84 $type = false;
85 if ( isset( $options['type'] ) )
86 $type = $options['type'];
87
88 $wgTitle = Title::newFromText( 'RunJobs.php' );
89
90 $dbw = wfGetDB( DB_MASTER );
91 $n = 0;
92 $conds = '';
93 if ($type !== false)
94 $conds = "job_cmd = " . $dbw->addQuotes($type);
95
96 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
97 $offset=0;
98 for (;;) {
99 $job = ($type == false) ?
100 Job::pop($offset)
101 : Job::pop_type($type);
102
103 if ($job == false)
104 break;
105
106 wfWaitForSlaves( 5 );
107 $t = microtime( true );
108 $offset=$job->id;
109 $status = $job->run();
110 $t = microtime( true ) - $t;
111 $timeMs = intval( $t * 1000 );
112 if ( !$job->run() ) {
113 runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
114 } else {
115 runJobsLog( $job->toString() . " t=$timeMs good" );
116 }
117 if ( $maxJobs && ++$n > $maxJobs ) {
118 break 2;
119 }
120 }
121 }
122
123
124 function runJobsLog( $msg ) {
125 print wfTimestamp( TS_DB ) . " $msg\n";
126 wfDebugLog( 'runJobs', $msg );
127 }
128
129 function handleTermSignal( $signal ) {
130 $GLOBALS['termReceived'] = true;
131 }
132