示例#1
0
/*
Copyright 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
require_once "bootstrap.inc";
require_once "../utils.inc";
require_once "batch_lib.inc";
$aDevices = allDevices();
foreach ($aDevices as $device) {
    foreach ($gaTasks as $task) {
        // lock file for this specific task.
        $lockfile = lockFilename($device, $task);
        $fp = fopen($lockfile, "w+");
        if (!flock($fp, LOCK_EX | LOCK_NB)) {
            // this task is still running
            echo "Task \"{$task}\" locked for device {$device}: {$lockfile}\n";
        }
    }
}
        require_once "copy.php";
        updateCrawl($labelFromRun, $gArchive, $locations[0], array("finishedDateTime" => time()));
        cprint(date("G:i") . ": DONE with crawl!");
        exit(0);
    }
}
// TODO - Combine "obtain" and "parse"?
// The "crawl" process has multiple distinct tasks. This is because the URLs are
// sent to and queued at WebPagetest which is asynchronous.
// We create a child process for each task.
// Each task has a unique file lock, so that a long task does NOT block a
// shorter process from being restarted during the next cronjob.
$aChildPids = array();
foreach ($gaTasks as $task) {
    // lock file for this specific task.
    $lockfile = lockFilename($locations[0], $task);
    $fp = fopen($lockfile, "w+");
    if (!flock($fp, LOCK_EX | LOCK_NB)) {
        // this task is still running
        lprint("Task \"{$task}\" already running. Bail.");
    } else {
        // start this task
        lprint("Starting task \"{$task}\"...");
        // fork the child process
        // return:
        //   -1 - error
        //    0 - we're the forked child process
        //   >0 - we're the parent process and this is the process ID of the child process
        $pid = pcntl_fork();
        if (-1 === $pid) {
            die("ERROR: failed to fork child process.\n");