1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace OC\BackgroundJob;
- use OCP\BackgroundJob\IJob;
- use OCP\BackgroundJob\IJobList;
- use OCP\ILogger;
- abstract class Job implements IJob {
-
- protected $id;
-
- protected $lastRun;
-
- protected $argument;
- public function execute(IJobList $jobList, ILogger $logger = null) {
- $jobList->setLastRun($this);
- if ($logger === null) {
- $logger = \OC::$server->getLogger();
- }
- try {
- $jobStartTime = time();
- $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
- $this->run($this->argument);
- $timeTaken = time() - $jobStartTime;
- $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
- $jobList->setExecutionTime($this, $timeTaken);
- } catch (\Throwable $e) {
- if ($logger) {
- $logger->logException($e, [
- 'app' => 'core',
- 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
- ]);
- }
- }
- }
- public function start(IJobList $jobList): void {
- $this->execute($jobList);
- }
- abstract protected function run($argument);
- public function setId(int $id) {
- $this->id = $id;
- }
- public function setLastRun(int $lastRun) {
- $this->lastRun = $lastRun;
- }
- public function setArgument($argument) {
- $this->argument = $argument;
- }
- public function getId() {
- return $this->id;
- }
- public function getLastRun() {
- return $this->lastRun;
- }
- public function getArgument() {
- return $this->argument;
- }
- }
|