Background jobs (Cron)

    Nextcloud by default offers you two types of background jobs. The 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 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 is rather straight forward. You write a class and extend your job class of choice.

    In this case it is a background job that runs every hour. And we take the 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.

    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.

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

    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:

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