diff --git a/core/Command/Background/RunningJobs.php b/core/Command/Background/RunningJobs.php
new file mode 100644
index 00000000000..8b63ace09c5
--- /dev/null
+++ b/core/Command/Background/RunningJobs.php
@@ -0,0 +1,92 @@
+Run ID: job identifier a found in database (Snowflake ID)
+ - Class: class of the job
+ - Started at: start time of the job
+ - Server ID: server ID as defined in config.php> (see `serverid`). Highlighted if it’s running on current server.
+ - PID: PID of process executing the job
+ - Running since: human readable elapsed time since job started
+
+ EOF;
+
+ $this
+ ->setName('background-job:running')
+ ->setDescription('Show currently running jobs')
+ ->setHelp($help)
+ ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Maximum number of results returned by the command', 200);
+ }
+
+ #[Override]
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $limit = (int)$input->getOption('limit');
+ $jobs = $this->jobRuns->runningJobs($limit);
+ $this->writeStreamingTableInOutputFormat($input, $output, $this->formatLine($jobs), 20);
+
+ return Base::SUCCESS;
+ }
+
+ private function formatLine(iterable $jobs): \Generator {
+ $now = time();
+ $currentServerId = $this->config->getSystemValueInt('serverid', -1);
+ foreach ($jobs as $job) {
+ yield [
+ 'Run ID' => $job->runId,
+ 'Class' => $job->className,
+ 'Started at' => $job->startedAt->format('Y-m-d H:i:s'),
+ 'Server ID' => $job->serverId === $currentServerId ? '' . $job->serverId . '' : $job->serverId,
+ 'PID' => $job->pid,
+ 'Running since' => $this->formatDuration($now - $job->startedAt->format('U')),
+ ];
+ }
+ }
+
+ /**
+ * TODO Move this function to utils class with better formatting (plural, i18n…)
+ */
+ private function formatDuration(int $seconds): string {
+ if ($seconds < 60) {
+ return sprintf('%d seconds', $seconds);
+ }
+ if ($seconds < 3600) {
+ return sprintf('%d minutes', $seconds / 60);
+ }
+ if ($seconds < (3600 * 24)) {
+ return sprintf('> %d hours', $seconds / 3600);
+ }
+
+ return sprintf('> %d days', $seconds / (3600 * 24));
+ }
+}
diff --git a/core/Migrations/Version34000Date20260521110333.php b/core/Migrations/Version34000Date20260521110333.php
new file mode 100644
index 00000000000..08f59813911
--- /dev/null
+++ b/core/Migrations/Version34000Date20260521110333.php
@@ -0,0 +1,54 @@
+hasTable('job_runs')) {
+ $table = $schema->createTable('job_runs');
+ $table->addColumn('run_id', Types::BIGINT, [
+ 'notnull' => true,
+ 'unsigned' => true,
+ ]);
+ $table->addColumn('class_id', Types::BIGINT, ['notnull' => true]);
+ $table->addColumn('pid', Types::INTEGER, ['notnull' => true]); // Should be MEDIUMINT
+ $table->addColumn('status', Types::SMALLINT, ['notnull' => true]); // Should be TINYINT
+ $table->addColumn('duration', Types::INTEGER, ['notnull' => true, 'default' => 0]);
+ $table->addColumn('ram_peak_usage', Types::INTEGER, ['notnull' => true, 'default' => 0]); // Should be MEDIUMINT
+ $table->setPrimaryKey(['run_id']);
+ $table->addIndex(['status'], 'status');
+
+ return $schema;
+ }
+
+ return null;
+ }
+}
diff --git a/core/register_command.php b/core/register_command.php
index 856894b5c4c..38cca5781f2 100644
--- a/core/register_command.php
+++ b/core/register_command.php
@@ -20,6 +20,7 @@ use OC\Core\Command\Background\Job;
use OC\Core\Command\Background\JobWorker;
use OC\Core\Command\Background\ListCommand;
use OC\Core\Command\Background\Mode;
+use OC\Core\Command\Background\RunningJobs;
use OC\Core\Command\Broadcast\Test;
use OC\Core\Command\Check;
use OC\Core\Command\Config\App\DeleteConfig;
@@ -148,6 +149,7 @@ if ($config->getSystemValueBool('installed', false)) {
$application->add(Server::get(ListCommand::class));
$application->add(Server::get(Delete::class));
$application->add(Server::get(JobWorker::class));
+ $application->add(Server::get(RunningJobs::class));
$application->add(Server::get(Test::class));
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 5519e61f2a0..23c77bf3ec6 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -200,8 +200,10 @@ return array(
'OCP\\AutoloadNotAllowedException' => $baseDir . '/lib/public/AutoloadNotAllowedException.php',
'OCP\\BackgroundJob\\IJob' => $baseDir . '/lib/public/BackgroundJob/IJob.php',
'OCP\\BackgroundJob\\IJobList' => $baseDir . '/lib/public/BackgroundJob/IJobList.php',
+ 'OCP\\BackgroundJob\\IJobRuns' => $baseDir . '/lib/public/BackgroundJob/IJobRuns.php',
'OCP\\BackgroundJob\\IParallelAwareJob' => $baseDir . '/lib/public/BackgroundJob/IParallelAwareJob.php',
'OCP\\BackgroundJob\\Job' => $baseDir . '/lib/public/BackgroundJob/Job.php',
+ 'OCP\\BackgroundJob\\JobRun' => $baseDir . '/lib/public/BackgroundJob/JobRun.php',
'OCP\\BackgroundJob\\JobStatus' => $baseDir . '/lib/public/BackgroundJob/JobStatus.php',
'OCP\\BackgroundJob\\QueuedJob' => $baseDir . '/lib/public/BackgroundJob/QueuedJob.php',
'OCP\\BackgroundJob\\TimedJob' => $baseDir . '/lib/public/BackgroundJob/TimedJob.php',
@@ -1253,6 +1255,7 @@ return array(
'OC\\Avatar\\UserAvatar' => $baseDir . '/lib/private/Avatar/UserAvatar.php',
'OC\\BackgroundJob\\JobClassesRegistry' => $baseDir . '/lib/private/BackgroundJob/JobClassesRegistry.php',
'OC\\BackgroundJob\\JobList' => $baseDir . '/lib/private/BackgroundJob/JobList.php',
+ 'OC\\BackgroundJob\\JobRuns' => $baseDir . '/lib/private/BackgroundJob/JobRuns.php',
'OC\\BinaryFinder' => $baseDir . '/lib/private/BinaryFinder.php',
'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => $baseDir . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php',
'OC\\Broadcast\\Events\\BroadcastEvent' => $baseDir . '/lib/private/Broadcast/Events/BroadcastEvent.php',
@@ -1335,6 +1338,7 @@ return array(
'OC\\Core\\Command\\Background\\JobWorker' => $baseDir . '/core/Command/Background/JobWorker.php',
'OC\\Core\\Command\\Background\\ListCommand' => $baseDir . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\Mode' => $baseDir . '/core/Command/Background/Mode.php',
+ 'OC\\Core\\Command\\Background\\RunningJobs' => $baseDir . '/core/Command/Background/RunningJobs.php',
'OC\\Core\\Command\\Base' => $baseDir . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => $baseDir . '/core/Command/Broadcast/Test.php',
'OC\\Core\\Command\\Check' => $baseDir . '/core/Command/Check.php',
@@ -1614,6 +1618,7 @@ return array(
'OC\\Core\\Migrations\\Version34000Date20260318095645' => $baseDir . '/core/Migrations/Version34000Date20260318095645.php',
'OC\\Core\\Migrations\\Version34000Date20260415161745' => $baseDir . '/core/Migrations/Version34000Date20260415161745.php',
'OC\\Core\\Migrations\\Version34000Date20260518163022' => $baseDir . '/core/Migrations/Version34000Date20260518163022.php',
+ 'OC\\Core\\Migrations\\Version34000Date20260521110333' => $baseDir . '/core/Migrations/Version34000Date20260521110333.php',
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => $baseDir . '/core/ResponseDefinitions.php',
'OC\\Core\\Service\\CronService' => $baseDir . '/core/Service/CronService.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 16666c438b7..8d0e74d1cdf 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -241,8 +241,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\AutoloadNotAllowedException' => __DIR__ . '/../../..' . '/lib/public/AutoloadNotAllowedException.php',
'OCP\\BackgroundJob\\IJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJob.php',
'OCP\\BackgroundJob\\IJobList' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJobList.php',
+ 'OCP\\BackgroundJob\\IJobRuns' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IJobRuns.php',
'OCP\\BackgroundJob\\IParallelAwareJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/IParallelAwareJob.php',
'OCP\\BackgroundJob\\Job' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/Job.php',
+ 'OCP\\BackgroundJob\\JobRun' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/JobRun.php',
'OCP\\BackgroundJob\\JobStatus' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/JobStatus.php',
'OCP\\BackgroundJob\\QueuedJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/QueuedJob.php',
'OCP\\BackgroundJob\\TimedJob' => __DIR__ . '/../../..' . '/lib/public/BackgroundJob/TimedJob.php',
@@ -1294,6 +1296,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Avatar\\UserAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/UserAvatar.php',
'OC\\BackgroundJob\\JobClassesRegistry' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/JobClassesRegistry.php',
'OC\\BackgroundJob\\JobList' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/JobList.php',
+ 'OC\\BackgroundJob\\JobRuns' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/JobRuns.php',
'OC\\BinaryFinder' => __DIR__ . '/../../..' . '/lib/private/BinaryFinder.php',
'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => __DIR__ . '/../../..' . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php',
'OC\\Broadcast\\Events\\BroadcastEvent' => __DIR__ . '/../../..' . '/lib/private/Broadcast/Events/BroadcastEvent.php',
@@ -1376,6 +1379,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Background\\JobWorker' => __DIR__ . '/../../..' . '/core/Command/Background/JobWorker.php',
'OC\\Core\\Command\\Background\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\Mode' => __DIR__ . '/../../..' . '/core/Command/Background/Mode.php',
+ 'OC\\Core\\Command\\Background\\RunningJobs' => __DIR__ . '/../../..' . '/core/Command/Background/RunningJobs.php',
'OC\\Core\\Command\\Base' => __DIR__ . '/../../..' . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => __DIR__ . '/../../..' . '/core/Command/Broadcast/Test.php',
'OC\\Core\\Command\\Check' => __DIR__ . '/../../..' . '/core/Command/Check.php',
@@ -1655,6 +1659,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Migrations\\Version34000Date20260318095645' => __DIR__ . '/../../..' . '/core/Migrations/Version34000Date20260318095645.php',
'OC\\Core\\Migrations\\Version34000Date20260415161745' => __DIR__ . '/../../..' . '/core/Migrations/Version34000Date20260415161745.php',
'OC\\Core\\Migrations\\Version34000Date20260518163022' => __DIR__ . '/../../..' . '/core/Migrations/Version34000Date20260518163022.php',
+ 'OC\\Core\\Migrations\\Version34000Date20260521110333' => __DIR__ . '/../../..' . '/core/Migrations/Version34000Date20260521110333.php',
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
'OC\\Core\\ResponseDefinitions' => __DIR__ . '/../../..' . '/core/ResponseDefinitions.php',
'OC\\Core\\Service\\CronService' => __DIR__ . '/../../..' . '/core/Service/CronService.php',
diff --git a/lib/private/BackgroundJob/JobRuns.php b/lib/private/BackgroundJob/JobRuns.php
new file mode 100644
index 00000000000..7d1f0b2c3e7
--- /dev/null
+++ b/lib/private/BackgroundJob/JobRuns.php
@@ -0,0 +1,81 @@
+snowflakeGenerator->nextId();
+ $qb = $this->connection->getQueryBuilder();
+ $qb
+ ->insert(self::TABLE)
+ ->setValue('run_id', $id)
+ ->setValue('class_id', $qb->createNamedParameter($classId))
+ ->setValue('pid', $qb->createNamedParameter(posix_getpid()))
+ ->setValue('status', $qb->createNamedParameter(JobStatus::RUNNING->value))
+ ->executeStatement();
+
+ return $id;
+ }
+
+ #[Override]
+ public function finished(int|string $runId, int $duration, int $memoryPeakUsage, JobStatus $status = JobStatus::SUCCEEDED): bool {
+ $qb = $this->connection->getQueryBuilder();
+ $result = $qb
+ ->update(self::TABLE)
+ ->set('status', $qb->createNamedParameter($status->value))
+ ->set('duration', $qb->createNamedParameter($duration))
+ ->set('ram_peak_usage', $qb->createNamedParameter($memoryPeakUsage))
+ ->where($qb->expr()->eq('run_id', $qb->createNamedParameter($runId)))
+ ->executeStatement();
+
+ return $result === 1;
+ }
+
+ #[Override]
+ public function runningJobs(int $limit = 200): \Generator {
+ $qb = $this->connection->getQueryBuilder();
+ $result = $qb
+ ->select('run_id', 'class_id', 'pid', 'status')
+ ->from(self::TABLE)
+ ->where($qb->expr()->eq('status', $qb->createNamedParameter(JobStatus::RUNNING->value)))
+ ->setMaxResults($limit)
+ ->executeQuery();
+
+ foreach ($result->iterateAssociative() as $row) {
+ $snowflakeInfo = $this->snowflakeDecoder->decode((string)$row['run_id']);
+ yield new JobRun(
+ $row['run_id'],
+ $this->classesRegistry->getName($row['class_id']),
+ $snowflakeInfo->getServerId(),
+ (int)$row['pid'],
+ $snowflakeInfo->getCreatedAt(),
+ JobStatus::from((int)$row['status']),
+ );
+ }
+ }
+}
diff --git a/lib/public/BackgroundJob/IJobRuns.php b/lib/public/BackgroundJob/IJobRuns.php
new file mode 100644
index 00000000000..a67fa821706
--- /dev/null
+++ b/lib/public/BackgroundJob/IJobRuns.php
@@ -0,0 +1,25 @@
+connection = Server::get(IDBConnection::class);
+ $this->registry = Server::get(JobClassesRegistry::class);
+ $this->runs = new JobRuns(
+ $this->connection,
+ Server::get(ISnowflakeGenerator::class),
+ Server::get(ISnowflakeDecoder::class),
+ $this->registry,
+ );
+ }
+
+ public function testJobStarted(): void {
+ $myPid = 1337;
+ $myClass = DummyJob::class;
+
+ $runId = $this->runs->started($this->registry->getId(DummyJob::class), $myPid);
+
+ $this->assertGreaterThan(0, $runId);
+ }
+
+ public function testJobSucceeded(): void {
+ $myPid = 1337;
+ $myClass = DummyJob::class;
+
+ $runId = $this->runs->started($this->registry->getId(DummyJob::class), $myPid);
+
+ $result = $this->runs->finished($runId, 12, 9876543);
+
+ $this->assertTrue($result);
+ }
+
+ public function testJobFailed(): void {
+ $myPid = 1337;
+ $myClass = DummyJob::class;
+
+ $runId = $this->runs->started($this->registry->getId(DummyJob::class), $myPid);
+
+ $result = $this->runs->finished($runId, 13, 87654321, JobStatus::FAILED);
+
+ $this->assertTrue($result);
+ }
+
+ public function testRunningJobs(): void {
+ $myPid = 1337;
+ $myClass = DummyJob::class;
+
+ $runId = $this->runs->started($this->registry->getId(DummyJob::class), $myPid);
+
+ $runningJobs = 0;
+ foreach ($this->runs->runningJobs() as $job) {
+ $this->assertInstanceOf(JobRun::class, $job);
+ ++$runningJobs;
+ }
+ $this->assertGreaterThan(0, $runningJobs);
+ }
+}