Basic multiprocess operation via pcntl_fork()
[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 // Spawn the children
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;
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 } while ( $deadPid == -1 && !$termReceived );
60 // Kill the remaining children
61 // If they're already dead, say due to SIGTERM, then they'll be zombies until
62 // pcntl_waitpid() below, so the PID won't be reused.
63 foreach ( $children as $childPid ) {
64 if ( $childPid != $deadPid ) {
65 posix_kill( $childPid, SIGTERM );
66 }
67 }
68 foreach ( $children as $childPid ) {
69 pcntl_waitpid( $childPid, $status );
70 }
71 // All done
72 exit( 0 );
73 }
74
75 // Set up this child
76 $wgMemc = wfGetCache( $wgMainCacheType );
77 }
78
79 if ( isset( $options['maxjobs'] ) ) {
80 $maxJobs = $options['maxjobs'];
81 } else {
82 $maxJobs = 10000;
83 }
84
85 $type = false;
86 if ( isset( $options['type'] ) )
87 $type = $options['type'];
88
89 $wgTitle = Title::newFromText( 'RunJobs.php' );
90
91 $dbw = wfGetDB( DB_MASTER );
92 $n = 0;
93 $conds = '';
94 if ($type !== false)
95 $conds = "job_cmd = " . $dbw->addQuotes($type);
96
97 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
98 $offset=0;
99 for (;;) {
100 $job = ($type == false) ?
101 Job::pop($offset)
102 : Job::pop_type($type);
103
104 if ($job == false)
105 break;
106
107 wfWaitForSlaves( 5 );
108 $t = microtime( true );
109 $offset=$job->id;
110 $status = $job->run();
111 $t = microtime( true ) - $t;
112 $timeMs = intval( $t * 1000 );
113 if ( !$job->run() ) {
114 runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
115 } else {
116 runJobsLog( $job->toString() . " t=$timeMs good" );
117 }
118 if ( $maxJobs && ++$n > $maxJobs ) {
119 break 2;
120 }
121 }
122 }
123
124
125 function runJobsLog( $msg ) {
126 print wfTimestamp( TS_DB ) . " $msg\n";
127 wfDebugLog( 'runJobs', $msg );
128 }
129
130 function handleTermSignal( $signal ) {
131 $GLOBALS['termReceived'] = true;
132 }
133