Using getopt for CLI arguments (#5446)
* Adding Argument class to Friendica * Adding Argument class to Friendica * Adding Argument class to Friendica * fixing arguments for `spawnWorker` * Adding `use Friendica\BaseObject` to `ApiTest.php` * Refactoring the argument-usages of Friendica * Refactoring the argument-usages of Friendica * removing superfluous []
This commit is contained in:
parent
ea24ac9d95
commit
cd52d0b3e9
|
@ -12,6 +12,12 @@ use Friendica\Core\Config;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
|
// Get options
|
||||||
|
$shortopts = '';
|
||||||
|
$shortopts .= 'f';
|
||||||
|
$longopts = [ 'foreground' ];
|
||||||
|
$options = getopt($shortopts, $longopts);
|
||||||
|
|
||||||
// Ensure that daemon.php is executed from the base path of the installation
|
// Ensure that daemon.php is executed from the base path of the installation
|
||||||
if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
||||||
$directory = dirname($_SERVER["argv"][0]);
|
$directory = dirname($_SERVER["argv"][0]);
|
||||||
|
@ -55,7 +61,7 @@ if (in_array("status", $_SERVER["argv"])) {
|
||||||
$mode = "status";
|
$mode = "status";
|
||||||
}
|
}
|
||||||
|
|
||||||
$foreground = in_array("--foreground", $_SERVER["argv"]);
|
$foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
|
||||||
|
|
||||||
if (!isset($mode)) {
|
if (!isset($mode)) {
|
||||||
die("Please use either 'start', 'stop' or 'status'.\n");
|
die("Please use either 'start', 'stop' or 'status'.\n");
|
||||||
|
|
|
@ -6,11 +6,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\BaseObject;
|
|
||||||
use Friendica\Core\Addon;
|
use Friendica\Core\Addon;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
|
|
||||||
|
// Get options
|
||||||
|
$shortopts = '';
|
||||||
|
$shortopts .= 'sc';
|
||||||
|
$longopts = [ 'spawn', 'cron' ];
|
||||||
|
$options = getopt($shortopts, $longopts);
|
||||||
|
|
||||||
// Ensure that worker.php is executed from the base path of the installation
|
// Ensure that worker.php is executed from the base path of the installation
|
||||||
if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
||||||
$directory = dirname($_SERVER["argv"][0]);
|
$directory = dirname($_SERVER["argv"][0]);
|
||||||
|
@ -41,14 +46,14 @@ $a->set_baseurl(Config::get('system', 'url'));
|
||||||
|
|
||||||
Addon::loadHooks();
|
Addon::loadHooks();
|
||||||
|
|
||||||
$spawn = (($_SERVER["argc"] == 2) && ($_SERVER["argv"][1] == "spawn"));
|
$spawn = array_key_exists('s', $options) || array_key_exists('spawn', $options);
|
||||||
|
|
||||||
if ($spawn) {
|
if ($spawn) {
|
||||||
Worker::spawnWorker();
|
Worker::spawnWorker();
|
||||||
killme();
|
killme();
|
||||||
}
|
}
|
||||||
|
|
||||||
$run_cron = (($_SERVER["argc"] <= 1) || ($_SERVER["argv"][1] != "no_cron"));
|
$run_cron = array_key_exists('c', $options) || array_key_exists('cron', $options);
|
||||||
|
|
||||||
Worker::processQueue($run_cron);
|
Worker::processQueue($run_cron);
|
||||||
|
|
||||||
|
|
23
src/App.php
23
src/App.php
|
@ -1116,19 +1116,32 @@ class App
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function proc_run($args)
|
/**
|
||||||
|
* Executes a child process with 'proc_open'
|
||||||
|
*
|
||||||
|
* @param string $command The command to execute
|
||||||
|
* @param array $args Arguments to pass to the command ( [ 'key' => value, 'key2' => value2, ... ]
|
||||||
|
*/
|
||||||
|
public function proc_run($command, $args)
|
||||||
{
|
{
|
||||||
if (!function_exists('proc_open')) {
|
if (!function_exists('proc_open')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($args, $this->getConfigValue('config', 'php_path', 'php'));
|
$cmdline = $this->getConfigValue('config', 'php_path', 'php') . $command;
|
||||||
|
|
||||||
for ($x = 0; $x < count($args); $x ++) {
|
foreach ($args as $key => $value) {
|
||||||
$args[$x] = escapeshellarg($args[$x]);
|
if (!is_null($value) && is_bool($value) && !$value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cmdline .= ' --' . $key;
|
||||||
|
if (!is_null($value) && !is_bool($value)) {
|
||||||
|
$cmdline .= ' ' . $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmdline = implode(' ', $args);
|
$cmdline = escapeshellarg($cmdline);
|
||||||
|
|
||||||
if ($this->min_memory_reached()) {
|
if ($this->min_memory_reached()) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1010,13 +1010,11 @@ class Worker
|
||||||
*/
|
*/
|
||||||
public static function spawnWorker($do_cron = false)
|
public static function spawnWorker($do_cron = false)
|
||||||
{
|
{
|
||||||
$args = ["bin/worker.php"];
|
$command = 'bin/worker.php';
|
||||||
|
|
||||||
if (!$do_cron) {
|
$args = [ 'cron' => $do_cron ];
|
||||||
$args[] = "no_cron";
|
|
||||||
}
|
|
||||||
|
|
||||||
get_app()->proc_run($args);
|
get_app()->proc_run($command, $args);
|
||||||
|
|
||||||
// after spawning we have to remove the flag.
|
// after spawning we have to remove the flag.
|
||||||
if (Config::get('system', 'worker_daemon_mode', false)) {
|
if (Config::get('system', 'worker_daemon_mode', false)) {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
namespace Friendica\Test;
|
namespace Friendica\Test;
|
||||||
|
|
||||||
|
use Friendica\BaseObject;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
use Friendica\Core\PConfig;
|
use Friendica\Core\PConfig;
|
||||||
use Friendica\Core\Protocol;
|
use Friendica\Core\Protocol;
|
||||||
|
@ -28,7 +29,7 @@ class ApiTest extends DatabaseTest
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// Reusable App object
|
// Reusable App object
|
||||||
$this->app = \Friendica\BaseObject::getApp();
|
$this->app = BaseObject::getApp();
|
||||||
|
|
||||||
// User data that the test database is populated with
|
// User data that the test database is populated with
|
||||||
$this->selfUser = [
|
$this->selfUser = [
|
||||||
|
|
Loading…
Reference in a new issue