Background jobs (Cron)

Often there is a need to run background jobs. For example there are Background jobs in Nextcloud that send out the activity emails. Or expire the trashbin.

Types of background jobs

Nextcloud by default offers you two types of background jobs. The \OCP\BackgroundJob\QueuedJob and \OCP\BackgroundJob\TimedJob.

The QueuedJob is for one time jobs. This can for example be triggered by inserting a job because an event happened. The TimedJob has a method setInterval where you can set the time minimum time in seconds between the jobs (from the constructor). This is useful in case you want to have a job that is run at most once a day for example.

Of course you can customize this all to your liking by just extending \OCP\BackgroundJob\Job

Writing a background job

Writing a background job is rather straight forward. You write a class and extend your job class of choice.

  1. <?php
  2. namespace OCA\MyApp\Cron;
  3. use \OCA\MyApp\Service\SomeService;
  4. use \OCP\BackgroundJob\TimedJob;
  5. use \OCP\AppFramework\Utility\ITimeFactory;
  6. class SomeTask extends TimedJob {
  7. private $myService;
  8. public function __construct(ITimeFactory $time, SomeService $service) {
  9. parent::__construct($time);
  10. $this->myService = $service;
  11. // Run once an hour
  12. parent::setInterval(3600);
  13. }
  14. protected function run($arguments) {
  15. $this->myService->doCron($arguments['uid']);
  16. }
  17. }

As you can see our dependency injection also works just fine for background jobs. The ITimeFactory always needs to be passed to the parent constructor. Since it is required to be set.

In this case it is a background job that runs every hour. And we take the uid argument to pass on to the service to run the background job.

The run function is the main thing you need to implement and where all the logic happens.

Registering a background job

Now that you have written your background job there is of course the small matter of how to make sure the system actually runs your job. In order to do this your job needs to be registered.

info.xml

You can register your jobs in your info.xml by addning;

  1. <background-jobs>
  2. <job>OCA\MyApp\Cron\SomeTask</job>
  3. </background-jobs>

This will on install/update of the application add the job OCA\MyApp\Cron\SomeTask. Of course in this case the arguments passed to your run function is just an empty array.

Registering manually

In case you want more fine grained control about when a background job is inserted and you want to pass arguments to it you need to manually register your background jobs.

You do this by using \OCP\BackgroundJob\IJobList. There you can add a job or remove a job.

For example you could add or remove a certain job based on some controller:

  1. <?php
  2. namespace OCA\MyApp\Controller;
  3. use \OCA\MyApp\Cron\SomeTask;
  4. use \OCP\AppFramework\Controller;
  5. use \OCP\BackgroundJob\IJobList;
  6. use \OCP\IRequest;
  7. class SomeController extends Controller {
  8. private $jobList
  9. public function __construct(string $appName, IRequest $request, IJobList $jobList) {
  10. parent::__construct($appName, $request);
  11. $this->jobList = $jobList;
  12. }
  13. public function addJob(string $uid) {
  14. $this->jobList->add(SomeTask::class, ['uid' => $uid]);
  15. }
  16. public function removeJob(string $uid) {
  17. $this->jobList->remove(SomeTask::class, ['uid' => $uid]);
  18. }
  19. }

This provides more fine grained control and you can pass arguments to your background jobs easily.