]> git.mxchange.org Git - friendica.git/blob - bin/worker.php
Refactoring Logging to use Configuration
[friendica.git] / bin / worker.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * @file bin/worker.php
5  * @brief Starts the background processing
6  */
7
8 use Friendica\App;
9 use Friendica\Core\Config;
10 use Friendica\Core\Config\Cache;
11 use Friendica\Core\Update;
12 use Friendica\Core\Worker;
13 use Friendica\Factory;
14 use Friendica\Util\BasePath;
15
16 // Get options
17 $shortopts = 'sn';
18 $longopts = ['spawn', 'no_cron'];
19 $options = getopt($shortopts, $longopts);
20
21 // Ensure that worker.php is executed from the base path of the installation
22 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
23         $directory = dirname($_SERVER["argv"][0]);
24
25         if (substr($directory, 0, 1) != '/') {
26                 $directory = $_SERVER["PWD"] . '/' . $directory;
27         }
28         $directory = realpath($directory . '/..');
29
30         chdir($directory);
31 }
32
33 require dirname(__DIR__) . '/vendor/autoload.php';
34
35 $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
36 $configLoader = new Cache\ConfigCacheLoader($basedir);
37 $configCache = Factory\ConfigFactory::createCache($configLoader);
38 Factory\DBFactory::init($configCache, $_SERVER);
39 $config = Factory\ConfigFactory::createConfig($configCache);
40 // needed to call PConfig::init()
41 Factory\ConfigFactory::createPConfig($configCache);
42 $logger = Factory\LoggerFactory::create('worker', $config);
43 $profiler = Factory\ProfilerFactory::create($logger, $config);
44
45 $a = new App($config, $logger, $profiler);
46
47 // Check the database structure and possibly fixes it
48 Update::check($a->getBasePath(), true);
49
50 // Quit when in maintenance
51 if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) {
52         return;
53 }
54
55 $a->setBaseURL(Config::get('system', 'url'));
56
57 $spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options);
58
59 if ($spawn) {
60         Worker::spawnWorker();
61         exit();
62 }
63
64 $run_cron = !array_key_exists('n', $options) && !array_key_exists('no_cron', $options);
65
66 Worker::processQueue($run_cron);
67
68 Worker::unclaimProcess();
69
70 Worker::endProcess();