Skip to content

Commit bf19691

Browse files
author
epriestley
committed
More phd stuff.
1 parent 2b6f16d commit bf19691

File tree

6 files changed

+328
-69
lines changed

6 files changed

+328
-69
lines changed

scripts/daemon/phabricator_daemon_launcher.php

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,24 @@
2121
require_once $root.'/scripts/__init_script__.php';
2222
require_once $root.'/scripts/__init_env__.php';
2323

24+
phutil_require_module('phabricator', 'infrastructure/daemon/control');
25+
$control = new PhabricatorDaemonControl();
26+
2427
$phd_dir = PhabricatorEnv::getEnvConfig('phd.pid-directory');
2528
$pid_dir = $phd_dir.'/pid';
2629

2730
switch (isset($argv[1]) ? $argv[1] : 'help') {
2831
case 'list':
29-
phutil_require_module('phutil', 'console');
30-
31-
$loader = new PhutilSymbolLoader();
32-
$symbols = $loader
33-
->setAncestorClass('PhutilDaemon')
34-
->selectSymbolsWithoutLoading();
35-
36-
$symbols = igroup($symbols, 'library');
37-
foreach ($symbols as $library => $symbol_list) {
38-
echo phutil_console_format("Daemons in library __%s__:\n", $library);
39-
foreach ($symbol_list as $symbol) {
40-
echo " ".$symbol['name']."\n";
41-
}
42-
echo "\n";
43-
}
44-
45-
break;
32+
$err = $control->executeListCommand();
33+
exit($err);
4634

4735
case 'status':
48-
$pid_descs = Filesystem::listDirectory($pid_dir);
49-
if (!$pid_descs) {
50-
echo "There are no running Phabricator daemons.\n";
51-
} else {
52-
printf(
53-
"%-5s\t%-24s\t%s\n",
54-
"PID",
55-
"Started",
56-
"Daemon");
57-
foreach ($pid_descs as $pid_file) {
58-
$data = Filesystem::readFile($pid_dir.'/'.$pid_file);
59-
$data = json_decode($data, true);
60-
61-
$pid = idx($data, 'pid', '?');
62-
$name = idx($data, 'name', '?');
63-
$since = idx($data, 'start')
64-
? date('M j Y, g:i:s A', $data['start'])
65-
: '?';
66-
67-
printf(
68-
"%5s\t%-24s\t%s\n",
69-
$pid,
70-
$since,
71-
$name);
72-
}
73-
}
36+
$err = $control->executeStatusCommand();
37+
exit($err);
7438

75-
break;
39+
case 'stop':
40+
$err = $control->executeStopCommand();
41+
exit($err);
7642

7743
case 'launch':
7844
phutil_require_module('phutil', 'moduleutils');
@@ -82,6 +48,8 @@
8248
throw new Exception("Daemon name required!");
8349
}
8450

51+
$pass = array_slice($argv, 3);
52+
8553
$n = 1;
8654
if (is_numeric($daemon)) {
8755
$n = $daemon;
@@ -92,6 +60,7 @@
9260
if (!$daemon) {
9361
throw new Exception("Daemon name required!");
9462
}
63+
$pass = array_slice($argv, 4);
9564
}
9665

9766
$loader = new PhutilSymbolLoader();
@@ -131,12 +100,23 @@
131100
Filesystem::assertIsDirectory($pid_dir);
132101
Filesystem::assertWritable($pid_dir);
133102

103+
foreach ($pass as $key => $arg) {
104+
$pass[$key] = escapeshellarg($arg);
105+
}
106+
134107
echo "Starting {$n} x {$daemon}";
108+
109+
chdir($launch_daemon);
135110
for ($ii = 0; $ii < $n; $ii++) {
136111
list($stdout, $stderr) = execx(
137-
"(cd %s && ./launch_daemon.php %s --daemonize --phd=%s)",
138-
$launch_daemon,
112+
"./launch_daemon.php ".
113+
"%s ".
114+
"--load-phutil-library=%s ".
115+
"--daemonize ".
116+
"--phd=%s ".
117+
implode(' ', $pass),
139118
$daemon,
119+
phutil_get_library_root('phabricator'),
140120
$pid_dir);
141121
echo ".";
142122
}
@@ -191,27 +171,6 @@
191171
case '--help':
192172
case 'help':
193173
default:
194-
echo <<<EOHELP
195-
phd - phabricator daemon launcher
196-
197-
launch <daemon>
198-
Start a daemon.
199-
200-
list
201-
List available daemons.
202-
203-
stop
204-
Stop all daemons.
205-
206-
status
207-
List running daemons.
208-
209-
stop
210-
Stop all running daemons.
211-
212-
parse-commit <rXnnnn>
213-
Parse a single commit.
214-
215-
EOHELP;
216-
exit(1);
174+
$err = $control->executeHelpCommand();
175+
exit($err);
217176
}

src/__phutil_library_map__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@
205205
'PhabricatorController' => 'applications/base/controller/base',
206206
'PhabricatorDaemon' => 'infrastructure/daemon/base',
207207
'PhabricatorDaemonConsoleController' => 'applications/daemon/controller/console',
208+
'PhabricatorDaemonControl' => 'infrastructure/daemon/control',
208209
'PhabricatorDaemonController' => 'applications/daemon/controller/base',
210+
'PhabricatorDaemonReference' => 'infrastructure/daemon/control/reference',
209211
'PhabricatorDaemonTimelineConsoleController' => 'applications/daemon/controller/timeline',
210212
'PhabricatorDaemonTimelineEventController' => 'applications/daemon/controller/timelineevent',
211213
'PhabricatorDirectoryCategory' => 'applications/directory/storage/category',
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
final class PhabricatorDaemonControl {
20+
21+
22+
public function executeListCommand() {
23+
$symbols = $this->loadAvailableDaemonClasses();
24+
25+
$symbols = igroup($symbols, 'library');
26+
27+
echo "\n";
28+
foreach ($symbols as $library => $symbol_list) {
29+
echo phutil_console_format("Daemons in library __%s__:\n", $library);
30+
foreach ($symbol_list as $symbol) {
31+
echo " ".$symbol['name']."\n";
32+
}
33+
echo "\n";
34+
}
35+
36+
return 0;
37+
}
38+
39+
public function executeStatusCommand() {
40+
$daemons = $this->loadRunningDaemons();
41+
42+
if (!$daemons) {
43+
echo "There are no running Phabricator daemons.\n";
44+
return 0;
45+
}
46+
47+
printf(
48+
"%-5s\t%-24s\t%s\n",
49+
"PID",
50+
"Started",
51+
"Daemon");
52+
foreach ($daemons as $daemon) {
53+
printf(
54+
"%5s\t%-24s\t%s\n",
55+
$daemon->getPID(),
56+
$daemon->getEpochStarted()
57+
? date('M j Y, g:i:s A', $daemon->getEpochStarted())
58+
: null,
59+
$daemon->getName());
60+
}
61+
62+
return 0;
63+
}
64+
65+
public function executeStopCommand() {
66+
$daemons = $this->loadRunningDaemons();
67+
if (!$daemons) {
68+
echo "There are no running Phabricator daemons.\n";
69+
return 0;
70+
}
71+
72+
$running = $daemons;
73+
74+
foreach ($running as $key => $daemon) {
75+
$pid = $daemon->getPID();
76+
$name = $daemon->getName();
77+
78+
echo "Stopping daemon '{$name}' ({$pid})...\n";
79+
if (!$daemon->isRunning()) {
80+
echo "Daemon is not running.\n";
81+
unset($running[$key]);
82+
} else {
83+
posix_kill($pid, SIGINT);
84+
}
85+
}
86+
87+
$start = time();
88+
do {
89+
foreach ($running as $key => $daemon) {
90+
$pid = $daemon->getPID();
91+
if (!$daemon->isRunning()) {
92+
echo "Daemon {$pid} exited normally.\n";
93+
unset($running[$key]);
94+
}
95+
}
96+
if (empty($running)) {
97+
break;
98+
}
99+
usleep(100000);
100+
} while (time() < $start + 15);
101+
102+
foreach ($running as $key => $daemon) {
103+
$pid = $daemon->getPID();
104+
echo "KILLing daemon {$pid}.\n";
105+
posix_kill($pid, SIGKILL);
106+
}
107+
108+
foreach ($daemons as $daemon) {
109+
if ($daemon->getPIDFile()) {
110+
Filesystem::remove($daemon->getPIDFile());
111+
}
112+
}
113+
114+
}
115+
116+
public function executeHelpCommand() {
117+
echo phutil_console_format(<<<EOHELP
118+
**NAME**
119+
**phd** - phabricator daemon launcher
120+
121+
**COMMAND REFERENCE**
122+
123+
**launch** [__n__] __daemon__
124+
Start a daemon (or n copies of a daemon).
125+
126+
**list**
127+
List available daemons.
128+
129+
**stop**
130+
Stop all daemons.
131+
132+
**status**
133+
List running daemons.
134+
135+
**stop**
136+
Stop all running daemons.
137+
138+
**help**
139+
Show this help.
140+
141+
**parse-commit** __rXnnnn__
142+
Parse a single commit.
143+
144+
145+
EOHELP
146+
);
147+
return 1;
148+
}
149+
150+
151+
protected function getControlDirectory($dir) {
152+
return PhabricatorEnv::getEnvConfig('phd.pid-directory').'/'.$dir;
153+
}
154+
155+
protected function loadAvailableDaemonClasses() {
156+
$loader = new PhutilSymbolLoader();
157+
return $loader
158+
->setAncestorClass('PhutilDaemon')
159+
->selectSymbolsWithoutLoading();
160+
}
161+
162+
protected function loadRunningDaemons() {
163+
$results = array();
164+
165+
$pid_dir = $this->getControlDirectory('pid');
166+
$pid_files = Filesystem::listDirectory($pid_dir);
167+
if (!$pid_files) {
168+
return $results;
169+
}
170+
171+
foreach ($pid_files as $pid_file) {
172+
$pid_data = Filesystem::readFile($pid_dir.'/'.$pid_file);
173+
$dict = json_decode($pid_data, true);
174+
if (!is_array($dict)) {
175+
// Just return a hanging reference, since control code needs to be
176+
// robust against unusual system states.
177+
$dict = array();
178+
}
179+
$ref = PhabricatorDaemonReference::newFromDictionary($dict);
180+
$ref->setPIDFile($pid_dir.'/'.$pid_file);
181+
$results[] = $ref;
182+
}
183+
184+
return $results;
185+
}
186+
187+
protected function killDaemon(PhabricatorDaemonReference $ref) {
188+
}
189+
190+
191+
192+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'infrastructure/daemon/control/reference');
10+
phutil_require_module('phabricator', 'infrastructure/env');
11+
12+
phutil_require_module('phutil', 'console');
13+
phutil_require_module('phutil', 'filesystem');
14+
phutil_require_module('phutil', 'symbols');
15+
phutil_require_module('phutil', 'utils');
16+
17+
18+
phutil_require_source('PhabricatorDaemonControl.php');

0 commit comments

Comments
 (0)