Files
Patrick Niebeling b4057809ed
Build package / php-lint (8.2) (push) Successful in 49s
Build package / php-lint (8.3) (push) Successful in 42s
Build package / php-lint (8.4) (push) Successful in 37s
Build package / xml-lint (push) Successful in 13s
Build package / unit-tests (push) Successful in 44s
Build package / package (push) Successful in 59s
Run Deck as the workflow owner, and drop the job interval
Workflows were being switched off with "its board no longer exists or is
no longer available to you" for boards that were perfectly intact.

Deck does not read the session for permissions. PermissionService -- the
class behind every check, including the ones inside CardService::reorder()
-- takes the current user as a plain `private ?string $userId`, filled
from the app container's `userId` service (ISession::get('user_id'),
registered shared). Pimple resolves that once per process and caches it,
and ServerContainer caches Deck's app container just as long. In cron the
value is null whenever a Deck-owned job ran earlier in the same pass, and
otherwise the first workflow owner touched -- never the user being
impersonated. null fails every check, so Deck answered NoPermissionException
for an untouched board and findStackIds() read that as "the board is gone".

IUserSession::setUser() never had any effect on this path.

- DeckIntegrationService::beginUserContext()/endUserContext() pin
  PermissionService (mandatory), CardService, BoardService and
  ActivityManager (best effort) to the workflow owner, and restore them
  afterwards. If PermissionService cannot be pinned, the runner skips that
  user instead of acting under someone else's permissions.
- findStackIds() now takes the uid as an argument and is assembled from
  pieces that cannot answer for the wrong user: BoardMapper and StackMapper
  carry no user state, and getPermissions() is handed the uid explicitly.
  It no longer goes through StackService::findAll().
- NoPermissionException is no longer treated as "board missing". Unknown
  failures throw, which leaves the workflow enabled.

This also fixes the second half of the same defect: card moves silently
failed for every user except the first one processed in a cron pass.

Separately, RunWorkflowsJob is now a plain Job instead of a TimedJob and
runs on every cron pass. The workflow_deck_automation.interval config key
is gone. Time sensitivity is no longer merely declared but structurally
unreachable: JobList::add() leaves the column at its TIME_SENSITIVE
default, and the ratchet in setLastRun() only fires for TimedJob.
2026-08-13 22:40:57 +02:00

54 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\WorkflowDeckAutomation\BackgroundJob;
use OCA\WorkflowDeckAutomation\Service\WorkflowRunner;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\Job;
/**
* Evaluates all enabled workflows, once per cron pass.
*
* Deliberately a plain `Job`, not a `TimedJob`: there is no interval to
* configure and none to compare against, so the cadence is exactly
* Nextcloud's cron cadence — every pass, no matter how often cron is set
* up to run. `TimedJob` with `setInterval(0)` would behave almost the
* same, but it keeps an interval concept around that nothing uses.
*
* **Time sensitivity still matters, and it comes for free here.**
* `OC\Core\Service\CronService::runCli()` reads `maintenance_window_start`
* (default `100`, i.e. unset) and, whenever the current UTC hour is outside
* `[start, start+4]`, asks `JobList::getNext($onlyTimeSensitive = true)`,
* which filters on the `time_sensitive` *column* of `oc_jobs`. A job stamped
* insensitive is then skipped for the other 20 hours of the day — this app
* shipped that way once and the job looked completely dead, showing
* `last_run = 1970-01-01` in `occ background-job:list` indefinitely. Note
* the gate only exists in `runCli()`, so ajax/webcron are unaffected and
* `occ background-job:worker '<class>'` bypasses it, which is why manual
* testing can look fine while cron does nothing.
*
* A plain `Job` can never acquire that stamp: `JobList::add()` leaves the
* column at its `TIME_SENSITIVE` default, and the one place that ever
* downgrades it, `JobList::setLastRun()`, is guarded by
* `$job instanceof TimedJob && !$job->isTimeSensitive()` — with no `else`.
* Rows written by an older version of this app keep whatever they were
* stamped with, though, so an instance that ran the `TIME_INSENSITIVE`
* version still needs the one-off `UPDATE oc_jobs SET time_sensitive = 0`
* documented in the README.
*/
class RunWorkflowsJob extends Job {
public function __construct(
ITimeFactory $time,
private WorkflowRunner $runner,
) {
parent::__construct($time);
$this->setAllowParallelRuns(false);
}
protected function run($argument): void {
$this->runner->run();
}
}