friendica/bin/worker.php

92 lines
2.8 KiB
PHP
Raw Normal View History

2017-12-14 11:38:51 -05:00
#!/usr/bin/env php
2010-07-18 23:49:54 -04:00
<?php
2017-12-14 11:38:51 -05:00
/**
2023-01-01 09:36:24 -05:00
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2020-01-19 01:05:23 -05:00
* Starts the background processing
2017-12-14 11:38:51 -05:00
*/
if (php_sapi_name() !== 'cli') {
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
exit();
}
2019-08-05 03:02:55 -04:00
use Dice\Dice;
use Friendica\App;
2021-01-01 14:35:29 -05:00
use Friendica\App\Mode;
2023-07-22 21:21:41 -04:00
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Update;
use Friendica\Core\Worker;
use Friendica\DI;
2019-09-17 10:47:00 -04:00
use Psr\Log\LoggerInterface;
2017-04-30 00:01:26 -04:00
// Get options
$shortopts = 'sn';
$longopts = ['spawn', 'no_cron'];
$options = getopt($shortopts, $longopts);
2017-11-19 17:00:43 -05:00
// Ensure that worker.php is executed from the base path of the installation
if (!file_exists("index.php") && (sizeof($_SERVER["argv"]) != 0)) {
$directory = dirname($_SERVER["argv"][0]);
if (substr($directory, 0, 1) != '/') {
$directory = $_SERVER["PWD"] . '/' . $directory;
2017-06-04 16:03:37 -04:00
}
$directory = realpath($directory . '/..');
chdir($directory);
}
2011-04-16 02:40:43 -04:00
require dirname(__DIR__) . '/vendor/autoload.php';
2019-08-05 03:02:55 -04:00
$dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
2023-07-22 21:21:41 -04:00
/** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */
$addonLoader = $dice->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class);
$dice = $dice->addRules($addonLoader->getActiveAddonConfig('dependencies'));
$dice = $dice->addRule(LoggerInterface::class, ['constructParams' => [LogChannel::WORKER]]);
DI::init($dice);
2021-10-23 14:46:17 -04:00
\Friendica\Core\Logger\Handler\ErrorHandler::register($dice->create(\Psr\Log\LoggerInterface::class));
2021-01-01 14:35:29 -05:00
DI::mode()->setExecutor(Mode::WORKER);
// Check the database structure and possibly fixes it
2023-01-06 06:50:14 -05:00
Update::check(DI::basePath(), true);
2017-09-30 13:42:03 -04:00
// Quit when in maintenance
if (!DI::mode()->has(App\Mode::MAINTENANCEDISABLED)) {
return;
}
$spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options);
2017-12-14 11:38:51 -05:00
if ($spawn) {
Worker::spawnWorker();
2018-12-26 00:40:12 -05:00
exit();
2017-12-14 11:38:51 -05:00
}
$run_cron = !array_key_exists('n', $options) && !array_key_exists('no_cron', $options);
2017-12-14 11:38:51 -05:00
2021-11-01 08:54:18 -04:00
$process = DI::process()->create(getmypid(), basename(__FILE__));
2021-10-24 14:43:59 -04:00
Worker::processQueue($run_cron, $process);
2021-10-24 14:43:59 -04:00
Worker::unclaimProcess($process);
DI::process()->delete($process);