You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudMaintenanceNoticeDeliv...

265 lines
10 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudMaintenanceNotice;
use App\Models\CloudMaintenanceNoticeInstance;
use App\Models\CloudMaintenanceNoticeRecipient;
use App\Models\User;
use App\Services\Mail;
use Illuminate\Database\Capsule\Manager as DB;
/** Leased, one-at-a-time maintenance mail delivery with no unknown replay. */
final class CloudMaintenanceNoticeDeliveryService
{
private const LEASE_SECONDS = 300;
private const EXPIRED_LEASE_LIMIT = 10;
private readonly \Closure $clock;
private readonly \Closure $sender;
private readonly \Closure $blacklist;
public function __construct(?callable $sender = null,
?callable $blacklist = null, ?callable $clock = null,
private readonly CloudMaintenanceNoticeService $notices =
new CloudMaintenanceNoticeService())
{
$this->clock = \Closure::fromCallable($clock
?? static fn (): int => time());
$this->sender = \Closure::fromCallable($sender
?? static fn (string $to, string $subject, array $variables): bool =>
Mail::send(
$to,
$subject,
'cloud/maintenance-notice.tpl',
$variables,
[],
'maintenance_notice'
));
$this->blacklist = \Closure::fromCallable($blacklist
?? static fn (string $email): bool =>
Mail::isRecipientBlacklisted($email));
}
public function tick(): bool
{
$worked = $this->convergeExpiredLeases();
$claim = $this->claim();
if ($claim === null) {
return $worked;
}
if (isset($claim['settled_notice_id'])) {
$this->notices->recalculate((int) $claim['settled_notice_id']);
return true;
}
$recipient = $claim['recipient'];
$token = $claim['lease_token'];
$recipientId = (int) $recipient->id;
$noticeId = (int) $recipient->notice_id;
try {
$user = User::where('id', (int) $recipient->user_id)
->first(['email']);
if ($user === null) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'user_deleted', '');
return true;
}
$snapshot = (string) $recipient->email_snapshot;
$currentEmail = strtolower(trim((string) $user->email));
if (!hash_equals($snapshot, $currentEmail)) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'recipient_email_changed', '');
return true;
}
if (($this->blacklist)($snapshot)) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'recipient_blacklisted', '');
return true;
}
$mail = $this->mail($recipient);
} catch (\Throwable $error) {
$this->finalize($recipientId, $noticeId, $token,
'failed', 'delivery_preflight_failed', get_class($error));
return true;
}
try {
$accepted = ($this->sender)(
(string) $recipient->email_snapshot,
(string) $mail['subject'],
$mail['variables']
) === true;
} catch (\Throwable $error) {
$this->finalize($recipientId, $noticeId, $token,
'unknown', 'delivery_effect_unknown', get_class($error));
return true;
}
$this->finalize(
$recipientId,
$noticeId,
$token,
$accepted ? 'sent' : 'unknown',
$accepted ? '' : 'delivery_effect_unknown',
''
);
return true;
}
private function claim(): ?array
{
return DB::connection('default')->transaction(function (): ?array {
$now = $this->now();
$recipient = CloudMaintenanceNoticeRecipient::where(
'status', 'pending'
)->where('available_at', '<=', $now)->orderBy('available_at')
->orderBy('id')->lockForUpdate()->first();
if ($recipient === null) {
return null;
}
$notice = CloudMaintenanceNotice::where(
'id', $recipient->notice_id
)->lockForUpdate()->first();
if ($notice === null || !in_array((string) $notice->status,
['scheduled', 'sending'], true)) {
$recipient->status = 'cancelled';
$recipient->last_error_code = 'notice_not_deliverable';
$recipient->last_error = '';
$recipient->updated_at = $now;
$recipient->save();
return ['settled_notice_id' => (int) $recipient->notice_id];
}
$token = bin2hex(random_bytes(16));
$recipient->status = 'sending';
$recipient->attempts = (int) $recipient->attempts + 1;
$recipient->lease_token = $token;
$recipient->lease_until = $now + self::LEASE_SECONDS;
$recipient->last_error_code = '';
$recipient->last_error = '';
$recipient->updated_at = $now;
if (!$recipient->save()) {
throw new \RuntimeException('Maintenance recipient claim failed.');
}
if ((string) $notice->status === 'scheduled') {
$notice->status = 'sending';
$notice->updated_at = $now;
$notice->save();
}
return ['recipient' => $recipient, 'lease_token' => $token];
}, 3);
}
private function convergeExpiredLeases(): bool
{
$noticeIds = DB::connection('default')->transaction(function (): array {
$now = $this->now();
$rows = CloudMaintenanceNoticeRecipient::where('status', 'sending')
->where('lease_until', '>', 0)
->where('lease_until', '<=', $now)
->orderBy('lease_until')->orderBy('id')
->limit(self::EXPIRED_LEASE_LIMIT)->lockForUpdate()->get();
$noticeIds = [];
foreach ($rows as $row) {
$updated = CloudMaintenanceNoticeRecipient::where('id', $row->id)
->where('status', 'sending')
->where('lease_token', $row->lease_token)->update([
'status' => 'unknown',
'lease_token' => null,
'lease_until' => 0,
'last_error_code' => 'delivery_lease_expired',
'last_error' => '',
'updated_at' => $now,
]);
if ($updated === 1) {
$noticeIds[(int) $row->notice_id] = true;
}
}
return array_keys($noticeIds);
}, 3);
foreach ($noticeIds as $noticeId) {
$this->notices->recalculate((int) $noticeId);
}
return $noticeIds !== [];
}
private function mail(CloudMaintenanceNoticeRecipient $recipient): array
{
$notice = CloudMaintenanceNotice::where(
'id', $recipient->notice_id
)->first();
if ($notice === null) {
throw new \RuntimeException('Maintenance notice is missing.');
}
$scope = json_decode((string) $notice->scope_snapshot_json,
true, 32, JSON_THROW_ON_ERROR);
if (!is_array($scope) || !is_string($scope['label'] ?? null)) {
throw new \RuntimeException('Maintenance scope is invalid.');
}
$instances = CloudMaintenanceNoticeInstance::where(
'notice_id', $notice->id
)->where('user_id', $recipient->user_id)->orderBy('id')->get()
->map(static fn ($instance): array => [
'name' => (string) $instance->instance_name,
'ipv4' => (string) $instance->ipv4,
'ipv6' => (string) $instance->ipv6,
])->all();
if ($instances === []) {
throw new \RuntimeException('Maintenance instance list is empty.');
}
$impacts = [
'network_interruption' => 'Network connectivity may be briefly interrupted',
'instance_restart' => 'Instances will be restarted',
'service_interruption' => 'Service may be briefly interrupted',
'no_interruption' => 'No service interruption is expected',
];
$impact = $impacts[(string) $notice->impact] ?? null;
if ($impact === null) {
throw new \RuntimeException('Maintenance impact is invalid.');
}
return [
'subject' => (string) $notice->subject,
'variables' => [
'notice_subject' => (string) $notice->subject,
'message_text' => (string) $notice->message_text,
'scope_label' => (string) $scope['label'],
'impact_label' => $impact,
'maintenance_starts_utc' => gmdate(
'Y-m-d H:i:s \\U\\T\\C',
(int) $notice->maintenance_starts_at
),
'maintenance_ends_utc' => gmdate(
'Y-m-d H:i:s \\U\\T\\C',
(int) $notice->maintenance_ends_at
),
'instances' => $instances,
],
];
}
private function finalize(int $recipientId, int $noticeId,
string $token, string $status, string $errorCode,
string $error): void
{
$now = $this->now();
$updated = CloudMaintenanceNoticeRecipient::where('id', $recipientId)
->where('status', 'sending')->where('lease_token', $token)
->update([
'status' => $status,
'lease_token' => null,
'lease_until' => 0,
'sent_at' => $status === 'sent' ? $now : null,
'last_error_code' => $errorCode,
'last_error' => mb_substr($error, 0, 500),
'updated_at' => $now,
]);
if ($updated === 1) {
$this->notices->recalculate($noticeId);
}
}
private function now(): int
{
return (int) ($this->clock)();
}
}