enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
+static inline void fpm_sockets_get_env_name(char *envname, unsigned idx) /* {{{ */
+{
+ if (!idx) {
+ strcpy(envname, "FPM_SOCKETS");
+ } else {
+ sprintf(envname, "FPM_SOCKETS_%d", idx);
+ }
+}
+/* }}} */
+
static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */
{
unsigned i;
if (env_value) {
for (i = 0; i < socket_set_count; i++) {
- if (!i) {
- strcpy(envname, "FPM_SOCKETS");
- } else {
- sprintf(envname, "FPM_SOCKETS_%d", i);
- }
+ fpm_sockets_get_env_name(envname, i);
setenv(envname, env_value + socket_set[i], 1);
}
+ fpm_sockets_get_env_name(envname, socket_set_count);
+ unsetenv(envname);
free(env_value);
}
{
unsigned i, lq_len;
struct fpm_worker_pool_s *wp;
- char sockname[32];
+ char envname[32];
char sockpath[256];
char *inherited;
struct listening_socket_s *ls;
/* import inherited sockets */
for (i = 0; i < FPM_ENV_SOCKET_SET_MAX; i++) {
- if (!i) {
- strcpy(sockname, "FPM_SOCKETS");
- } else {
- sprintf(sockname, "FPM_SOCKETS_%d", i);
- }
- inherited = getenv(sockname);
+ fpm_sockets_get_env_name(envname, i);
+ inherited = getenv(envname);
if (!inherited) {
break;
}
const P_PREFIX_STDOUT = '\[pool unconfined\] child \d+ said into stdout: ';
const FINAL_SUFFIX = ', pipe is closed';
+ const DEBUG = 'DEBUG';
+ const NOTICE = 'NOTICE';
+ const WARNING = 'WARNING';
+ const ERROR = 'ERROR';
+ const ALERT = 'ALERT';
+
/**
* @var string
*/
return true;
}
+ /**
+ * @param array $lines
+ * @return bool
+ */
+ public function expectReloadingLines(array $lines)
+ {
+ if (
+ !$this->expectNotice($lines[0], 'Reloading in progress ...') ||
+ !$this->expectNotice($lines[1], 'reloading: .*')
+ ) {
+ return false;
+ }
+
+ for ($i = 2; $i < count($lines) - 2; $i++) {
+ if (!$this->expectNotice($lines[$i], 'using inherited socket fd=\d+, "[^"]+"')) {
+ return false;
+ }
+ }
+
+ return $this->expectStartingLines(array_splice($lines, $i));
+ }
+
/**
* @param array $lines
* @return bool
*/
public function expectDebug(string $line, string $expectedMessage, $pool = null)
{
- return $this->expectEntry('DEBUG', $line, $expectedMessage, $pool);
+ return $this->expectEntry(self::DEBUG, $line, $expectedMessage, $pool);
}
/**
*/
public function expectNotice(string $line, string $expectedMessage, $pool = null)
{
- return $this->expectEntry('NOTICE', $line, $expectedMessage, $pool);
+ return $this->expectEntry(self::NOTICE, $line, $expectedMessage, $pool);
}
/**
*/
public function expectWarning(string $line, string $expectedMessage, $pool = null)
{
- return $this->expectEntry('WARNING', $line, $expectedMessage, $pool);
+ return $this->expectEntry(self::WARNING, $line, $expectedMessage, $pool);
}
/**
*/
public function expectError(string $line, string $expectedMessage, $pool = null)
{
- return $this->expectEntry('ERROR', $line, $expectedMessage, $pool);
+ return $this->expectEntry(self::ERROR, $line, $expectedMessage, $pool);
}
/**
*/
public function expectAlert(string $line, string $expectedMessage, $pool = null)
{
- return $this->expectEntry('ALERT', $line, $expectedMessage, $pool);
+ return $this->expectEntry(self::ALERT, $line, $expectedMessage, $pool);
}
-
/**
* @param string $msg
* @return bool
/**
* Configuration template
*
- * @var string
+ * @var string|array
*/
private $configTemplate;
unlink($filePath);
}
}
- // clean config files
+
+ self::cleanConfigFiles();
+ }
+
+ /**
+ * Clean config files
+ */
+ static public function cleanConfigFiles() {
if (is_dir(self::CONF_DIR)) {
foreach(glob(self::CONF_DIR . '/*.conf') as $name) {
unlink($name);
}
}
+ if ($this->debug) {
+ foreach ($lines as $line) {
+ echo "LOG LINE: " . $line;
+ }
+ }
+
return $lines;
}
return getmygid();
}
+ /**
+ * Reload FPM by sending USR2 signal and optionally change config before that.
+ *
+ * @param string|array $configTemplate
+ * @return string
+ * @throws \Exception
+ */
+ public function reload($configTemplate = null)
+ {
+ if (!is_null($configTemplate)) {
+ self::cleanConfigFiles();
+ $this->configTemplate = $configTemplate;
+ $this->createConfig();
+ }
+
+ return $this->signal('USR2');
+ }
+
/**
* Send signal to the supplied PID or the server PID.
*
throw new \Exception('The config template array has to have main config');
}
$mainTemplate = $configTemplates['main'];
- unset($configTemplates['main']);
if (!is_dir(self::CONF_DIR)) {
mkdir(self::CONF_DIR);
}
- foreach ($configTemplates as $name => $configTemplate) {
+ foreach ($this->createPoolConfigs($configTemplates) as $name => $poolConfig) {
$this->makeFile(
'conf',
- $this->processTemplate($configTemplate),
+ $this->processTemplate($poolConfig),
self::CONF_DIR,
$name
);
return $this->makeFile($extension, $this->processTemplate($mainTemplate));
}
+ /**
+ * Create pool config templates.
+ *
+ * @param array $configTemplates
+ * @return array
+ * @throws \Exception
+ */
+ private function createPoolConfigs(array $configTemplates)
+ {
+ if (!isset($configTemplates['poolTemplate'])) {
+ unset($configTemplates['main']);
+ return $configTemplates;
+ }
+ $poolTemplate = $configTemplates['poolTemplate'];
+ $configs = [];
+ if (isset($configTemplates['count'])) {
+ $start = $configTemplates['start'] ?? 1;
+ for ($i = $start; $i < $start + $configTemplates['count']; $i++) {
+ $configs[$i] = str_replace('%index%', $i, $poolTemplate);
+ }
+ } elseif (isset($configTemplates['names'])) {
+ foreach($configTemplates['names'] as $name) {
+ $configs[$name] = str_replace('%name%', $name, $poolTemplate);
+ }
+ } else {
+ throw new \Exception('The config template requires count or names if poolTemplate set');
+ }
+ return $configs;
+ }
+
/**
* Process template string.
*
$this->logTool->checkTruncatedMessage($this->response->getErrorData());
}
+ /**
+ * Expect reloading lines to be logged.
+ *
+ * @param int $socketCount
+ */
+ public function expectLogReloadingNotices($socketCount = 1)
+ {
+ $this->logTool->expectReloadingLines($this->getLogLines($socketCount + 4));
+ }
+
/**
* Expect starting lines to be logged.
*/
return $this->logTool->checkWrappedMessage($logLines, false, true, $is_stderr);
}
+ /**
+ * Expect log entry.
+ *
+ * @param string $type The log type
+ * @param string $message The expected message
+ * @param string|null $pool The pool for pool prefixed log entry
+ * @param int $count The number of items
+ * @return bool
+ */
+ private function expectLogEntry(string $type, string $message, $pool = null, $count = 1)
+ {
+ for ($i = 0; $i < $count; $i++) {
+ if (!$this->logTool->expectEntry($type, $this->getLastLogLine(), $message, $pool)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Expect a log debug message.
*
* @param string $message
* @param string|null $pool
+ * @param int $count
* @return bool
*/
- public function expectLogDebug(string $message, $pool = null)
+ public function expectLogDebug(string $message, $pool = null, $count = 1)
{
- return $this->logTool->expectDebug($this->getLastLogLine(), $message, $pool);
+ return $this->expectLogEntry(LogTool::DEBUG, $message, $pool, $count);
}
/**
*
* @param string $message
* @param string|null $pool
+ * @param int $count
* @return bool
*/
- public function expectLogNotice(string $message, $pool = null)
+ public function expectLogNotice(string $message, $pool = null, $count = 1)
{
- return $this->logTool->expectNotice($this->getLastLogLine(), $message, $pool);
+ return $this->expectLogEntry(LogTool::NOTICE, $message, $pool, $count);
}
/**
*
* @param string $message
* @param string|null $pool
+ * @param int $count
* @return bool
*/
- public function expectLogWarning(string $message, $pool = null)
+ public function expectLogWarning(string $message, $pool = null, $count = 1)
{
- return $this->logTool->expectWarning($this->getLastLogLine(), $message, $pool);
+ return $this->expectLogEntry(LogTool::WARNING, $message, $pool, $count);
}
/**
*
* @param string $message
* @param string|null $pool
+ * @param int $count
* @return bool
*/
- public function expectLogError(string $message, $pool = null)
+ public function expectLogError(string $message, $pool = null, $count = 1)
{
- return $this->logTool->expectError($this->getLastLogLine(), $message, $pool);
+ return $this->expectLogEntry(LogTool::ERROR, $message, $pool, $count);
}
/**
*
* @param string $message
* @param string|null $pool
+ * @param int $count
* @return bool
*/
- public function expectLogAlert(string $message, $pool = null)
+ public function expectLogAlert(string $message, $pool = null, $count = 1)
{
- return $this->logTool->expectAlert($this->getLastLogLine(), $message, $pool);
+ return $this->expectLogEntry(LogTool::ALERT, $message, $pool, $count);
}
/**