|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * TemplateResyncJob |
| 5 | + * |
| 6 | + * Applies an admin template re-sync asynchronously for large target |
| 7 | + * groups (REQ-RESYNC-005 "Large groups apply asynchronously"). Enqueued |
| 8 | + * by {@see \OCA\LaunchPad\Service\TemplateResyncService::resync()} — a |
| 9 | + * one-off {@see QueuedJob}, not registered at app boot; NC's background |
| 10 | + * job runner removes it from the queue once {@see self::run()} returns. |
| 11 | + * |
| 12 | + * @category BackgroundJob |
| 13 | + * @package OCA\LaunchPad\BackgroundJob |
| 14 | + * @author Conduction b.v. <info@conduction.nl> |
| 15 | + * @copyright 2026 Conduction b.v. |
| 16 | + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 17 | + * @version GIT:auto |
| 18 | + * @link https://conduction.nl |
| 19 | + * |
| 20 | + * SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 21 | + * SPDX-License-Identifier: EUPL-1.2 |
| 22 | + */ |
| 23 | + |
| 24 | +declare(strict_types=1); |
| 25 | + |
| 26 | +namespace OCA\LaunchPad\BackgroundJob; |
| 27 | + |
| 28 | +use OCA\LaunchPad\Service\TemplateResyncService; |
| 29 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 30 | +use OCP\BackgroundJob\QueuedJob; |
| 31 | +use Psr\Log\LoggerInterface; |
| 32 | +use Throwable; |
| 33 | + |
| 34 | +/** |
| 35 | + * One-off async apply for a large-target-group template re-sync. |
| 36 | + * |
| 37 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) — $argument's shape is |
| 38 | + * validated inline; the parent QueuedJob interface requires the |
| 39 | + * parameter regardless. |
| 40 | + * @spec openspec/specs/admin-templates/spec.md#requirement-req-resync-005-re-sync-is-idempotent-audited-async-capable-and-notifies-users |
| 41 | + */ |
| 42 | +class TemplateResyncJob extends QueuedJob |
| 43 | +{ |
| 44 | + /** |
| 45 | + * Constructor. |
| 46 | + * |
| 47 | + * @param ITimeFactory $time Time factory (parent |
| 48 | + * requirement). |
| 49 | + * @param TemplateResyncService $resyncService The re-sync orchestrator — |
| 50 | + * {@see TemplateResyncService::applyResync()} |
| 51 | + * recomputes the plan |
| 52 | + * fresh at run time |
| 53 | + * (rather than |
| 54 | + * deserialising a |
| 55 | + * stale one), so the |
| 56 | + * apply reflects the |
| 57 | + * template's state at |
| 58 | + * the moment the job |
| 59 | + * actually runs. |
| 60 | + * @param LoggerInterface $logger PSR-3 logger. |
| 61 | + */ |
| 62 | + public function __construct( |
| 63 | + ITimeFactory $time, |
| 64 | + private readonly TemplateResyncService $resyncService, |
| 65 | + private readonly LoggerInterface $logger, |
| 66 | + ) { |
| 67 | + parent::__construct(time: $time); |
| 68 | + }//end __construct() |
| 69 | + |
| 70 | + /** |
| 71 | + * Apply the re-sync plan for `$argument['templateId']` / |
| 72 | + * `$argument['strategy']`, writing the audit record and notifying |
| 73 | + * every affected user on completion. |
| 74 | + * |
| 75 | + * Malformed arguments are logged and skipped rather than throwing — |
| 76 | + * a throw here would make NC's job runner retry indefinitely with the |
| 77 | + * same bad payload. |
| 78 | + * |
| 79 | + * @param mixed $argument `{templateId: int, strategy: string, |
| 80 | + * actingAdminId: string}`. |
| 81 | + * |
| 82 | + * @return void |
| 83 | + * |
| 84 | + * @spec openspec/specs/admin-templates/spec.md |
| 85 | + */ |
| 86 | + protected function run($argument): void |
| 87 | + { |
| 88 | + $templateId = (int) ($argument['templateId'] ?? 0); |
| 89 | + $strategy = (string) ($argument['strategy'] ?? ''); |
| 90 | + $actingAdminId = (string) ($argument['actingAdminId'] ?? ''); |
| 91 | + |
| 92 | + if ($templateId <= 0 || $strategy === '' || $actingAdminId === '') { |
| 93 | + $this->logger->warning( |
| 94 | + message: 'launchpad.template_resync.job_skipped reason=invalid_arguments', |
| 95 | + context: ['argument' => $argument] |
| 96 | + ); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + $result = $this->resyncService->applyResync( |
| 102 | + templateId: $templateId, |
| 103 | + strategy: $strategy, |
| 104 | + actingAdminId: $actingAdminId |
| 105 | + ); |
| 106 | + |
| 107 | + $this->logger->info( |
| 108 | + message: sprintf( |
| 109 | + 'launchpad.template_resync.job_completed template=%d strategy=%s affected=%d total=%d', |
| 110 | + $templateId, |
| 111 | + $strategy, |
| 112 | + $result['affectedCount'], |
| 113 | + $result['totalCopies'] |
| 114 | + ) |
| 115 | + ); |
| 116 | + } catch (Throwable $t) { |
| 117 | + $this->logger->error( |
| 118 | + message: 'launchpad.template_resync.job_failed', |
| 119 | + context: [ |
| 120 | + 'templateId' => $templateId, |
| 121 | + 'strategy' => $strategy, |
| 122 | + 'exception' => $t, |
| 123 | + ] |
| 124 | + ); |
| 125 | + }//end try |
| 126 | + }//end run() |
| 127 | +}//end class |
0 commit comments