Esempio n. 1
0
// unset($pid);
// parent will die here
if (!$forked) {
    exit(1);
}
// Avoid forking connection crash, renew the connection.
Database::disconnect();
// Get maximum allowed connections before table lock.
$capLimit = (int) @conf::get('system::crontab.process_limit');
if (!$capLimit) {
    $capLimit = Process::MAXIMUM_CAPACITY;
}
// Start transaction before lock tables.
Database::beginTransaction();
// Pick next awaiting process
Database::locKTables([FRAMEWORK_COLLECTION_PROCESS . ' READ']);
$res = (int) Database::fetchField('SELECT IFNULL(SUM(`capacity`), 0) as occupation
    FROM `' . FRAMEWORK_COLLECTION_PROCESS . '`
    WHERE `pid` IS NOT NULL AND `pid` > 0');
Database::unlockTables(true);
if ($res >= $capLimit) {
    Log::debug('Active processes has occupied maximum server capacity, daemon exits.');
    Database::rollback();
    die;
}
unset($res, $capLimit);
Database::lockTables(array(FRAMEWORK_COLLECTION_PROCESS . ' LOW_PRIORITY WRITE', FRAMEWORK_COLLECTION_PROCESS . ' as `active` LOW_PRIORITY WRITE', FRAMEWORK_COLLECTION_PROCESS . ' as `inactive` LOW_PRIORITY WRITE'));
$process = Database::fetchRow('SELECT `inactive`.* FROM `' . FRAMEWORK_COLLECTION_PROCESS . '` as `inactive`
    LEFT JOIN ( SELECT `type`, SUM(`capacity`) as `occupation` FROM `' . FRAMEWORK_COLLECTION_PROCESS . '`
        WHERE `pid` IS NOT NULL GROUP BY `type` ) as `active`
      ON `active`.`type` = `inactive`.`type`