Skip to content

Commit 5c64efa

Browse files
committed
Support atomic deployment using symlink #81
1 parent 990d0e6 commit 5c64efa

5 files changed

Lines changed: 23 additions & 23 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
strategy:
1616
matrix:
1717
php-version:
18-
- '8.0'
1918
- '8.1'
2019
- '8.2'
2120
- '8.3'

bin/cavalcade-runner

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use Throwable;
88

99
include dirname(__DIR__) . '/autoload.php';
1010

11-
$options = getopt('l:p:b:w:c:i:d:e:k:z:s:q:');
11+
$options = getopt('l:p:b:w:c:f:i:d:e:k:z:s:q:');
1212
$log_path = $options['l'] ?? '/var/log/wp/cron.log';
1313
$pid_file = $options['p'] ?? '/run/cavalcade-runner/cavalcade-runner.pid';
1414
$wp_base_path = $options['b'] ?? '/var/web/wp';
1515
$max_workers_count = intval($options['w'] ?? 2);
1616
$wpcli_path = $options['c'] ?? '/usr/local/bin/wp';
17+
$wpconfig_path = $options['f'];
1718
$cleanup_interval = intval($options['i'] ?? 60);
1819
$cleanup_delay = intval($options['d'] ?? 24 * 60 * 60);
1920
$healthcheck_option = $options['e'];
@@ -64,6 +65,7 @@ try {
6465
$pdoclass,
6566
$max_workers_count,
6667
$wpcli_path,
68+
$wpconfig_path,
6769
$cleanup_interval,
6870
$cleanup_delay,
6971
$wp_base_path,

bin/create-table

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ include dirname(__DIR__) . '/autoload.php';
99

1010
date_default_timezone_set('UTC');
1111
$log = Logger::create('work/log/create-table.log');
12-
$wpcli_path = '/usr/local/bin/wp';
13-
$wp_path = '/var/web/wp';
1412

1513
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($log) {
1614
// error was suppressed with the @-operator

build/cavalcade-runner/restarter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -eu
55
ctrl_fifo=work/runner_ctrl.fifo
66
ctrl_done_fifo=work/runner_ctrl_done.fifo
77

8-
runner_command='bin/cavalcade-runner-test -l work/log/cavalcade-runner.log -p work/cavalcade-runner.pid -b /www -w 10 -i 2 -d 6 -e 192.0.2.255,5,/www/.maintenance -k work/runner.lock -s work/log/runner-state.json -z 2000 -q inc/healthcheck-test.php'
8+
runner_command='bin/cavalcade-runner-test -l work/log/cavalcade-runner.log -p work/cavalcade-runner.pid -b /www -f /www/wp-config.php -w 10 -i 2 -d 6 -e 192.0.2.255,5,/www/.maintenance -k work/runner.lock -s work/log/runner-state.json -z 2000 -q inc/healthcheck-test.php'
99

1010
mkdir -p work
1111
mkfifo "$ctrl_fifo" "$ctrl_done_fifo"

inc/class-runner.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Runner
2727
protected $pdoclass;
2828
protected $db;
2929
protected $workers = [];
30-
protected $wp_path;
30+
protected $wp_base_path;
31+
protected $wpconfig_path;
3132
protected $table_prefix;
3233
protected $table;
3334
protected $state;
@@ -40,6 +41,7 @@ public function __construct(
4041
$pdoclass,
4142
$max_workers,
4243
$wpcli_path,
44+
$wpconfig_path,
4345
$cleanup_interval,
4446
$cleanup_delay,
4547
$wp_base_path,
@@ -49,9 +51,10 @@ public function __construct(
4951
$this->pdoclass = $pdoclass;
5052
$this->max_workers = $max_workers;
5153
$this->wpcli_path = $wpcli_path;
54+
$this->wpconfig_path = $wpconfig_path;
5255
$this->cleanup_interval = $cleanup_interval;
5356
$this->cleanup_delay = $cleanup_delay;
54-
$this->wp_path = realpath($wp_base_path);
57+
$this->wp_base_path = $wp_base_path;
5558
$this->max_log_size = $max_log_size;
5659
$this->state_path = $state_path;
5760
$this->hooks = new Hooks();
@@ -68,6 +71,7 @@ public static function instance(
6871
$pdoclass,
6972
$max_workers,
7073
$wpcli_path,
74+
$wpconfig_path,
7175
$cleanup_interval,
7276
$cleanup_delay,
7377
$wp_base_path,
@@ -80,6 +84,7 @@ public static function instance(
8084
$pdoclass,
8185
$max_workers,
8286
$wpcli_path,
87+
$wpconfig_path,
8388
$cleanup_interval,
8489
$cleanup_delay,
8590
$wp_base_path,
@@ -126,15 +131,8 @@ public function bootstrap()
126131
{
127132
$this->load_state();
128133

129-
$config_path = $this->wp_path . '/wp-config.php';
130-
if (!file_exists($config_path)) {
131-
$config_path = realpath($this->wp_path . '/../wp-config.php');
132-
if (!file_exists($config_path)) {
133-
throw new Exception(sprintf(
134-
'Could not find config file at %s',
135-
$this->wp_path . '/wp-config.php or next level up.'
136-
));
137-
}
134+
if (!file_exists($this->wpconfig_path)) {
135+
throw new Exception('Could not find config file at ' . $this->wpconfig_path);
138136
}
139137

140138
// Load configuration ONLY
@@ -143,7 +141,7 @@ public function bootstrap()
143141
$_SERVER['HTTP_HOST'] = 'cavalcade.example';
144142
}
145143

146-
include $config_path;
144+
include $this->wpconfig_path;
147145
$this->table_prefix = isset($table_prefix) ? $table_prefix : 'wp_';
148146
$this->table = $this->table_prefix . 'cavalcade_jobs';
149147
$charset = defined('DB_CHARSET') ? DB_CHARSET : 'utf8mb4';
@@ -528,11 +526,6 @@ protected function terminate($type)
528526
unset($this->db);
529527
}
530528

531-
public function get_wp_path()
532-
{
533-
return $this->wp_path;
534-
}
535-
536529
protected function get_next_job()
537530
{
538531
// $this->log->debug('trying to get next job');
@@ -592,7 +585,15 @@ protected function run_job($job)
592585
1 => ['pipe', 'w'], // stdout
593586
2 => ['pipe', 'w'], // stderr
594587
];
595-
$process = proc_open($command, $spec, $pipes, $this->wp_path);
588+
589+
// Resolve path every time to support atomic deployment.
590+
$wp_path = realpath($this->wp_base_path);
591+
if ($wp_path === false) {
592+
$this->log->error('failed to resolve path: ' . $this->wp_base_path);
593+
throw new Exception('failed to resolve path');
594+
}
595+
596+
$process = proc_open($command, $spec, $pipes, $wp_path);
596597

597598
if ($process === false) {
598599
throw new Exception('unable to proc_open()');

0 commit comments

Comments
 (0)