Skip to content

Commit

Permalink
Merge pull request #11157 from nextcloud/feat/devmanual/schedule-back…
Browse files Browse the repository at this point in the history
…ground-job

feat(devmanual): Schedule background jobs
  • Loading branch information
marcelklehr authored Sep 29, 2023
2 parents 0c87b09 + 99e2e56 commit d1af5a4
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions developer_manual/basics/backgroundjobs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,40 @@ For example you could add or remove a certain job based on some controller:
This provides more fine grained control and you can pass arguments to your background
jobs easily.

Scheduling
^^^^^^^^^^

A background job can be scheduled to run after a specific time and date. This avoids maintaining a time check inside a background job.

Beware that the reliability of the execution time is limited. Systems that do not use system cron may have no active users and therefore no reliable cron trigger at the target time. System cron can also not guarantee that the job is picked up right away if the background job queue is full. The only guarantee you get is that the job is not picked up earlier than the specified time.

.. code-block:: php
:caption: lib/Service/ShareService.php
:emphasize-lines: 19-23
<?php
namespace OCA\MyApp\Service;
use OCA\MyApp\BackgroundJob\RevokeShare;
use OCP\BackgroundJob\IJobList;
class ShareService {
private IJobList $jobList
public function __construct(IJobList $jobList) {
$this->jobList = $jobList;
}
public function shareWithUser(string $uid, int $expiration) {
// create an expiring share
$this->jobList->scheduleAfter(
RevokeShare::class,
['id' => $shareId],
$expiration,
);
}
}

0 comments on commit d1af5a4

Please sign in to comment.