Developing new Background Job

Hello,
I’m trying to create background job. I’m following the manual.
https://docs.nextcloud.com/server/15/developer_manual/app/backgroundjobs.html
My class:

class PollTask extends TimedJob
{

public function __construct() {
    parent::setInterval(60);
}

public function run($arguments) {
    $this->myService->doCron($arguments['uid']);
    notify();
}

public function notify() {
    //Tried different things
}

}

I also defined this in info.xml

  1. Why in document it is defined like that. Is it possible to use $this and static in the same method?
    public static function run($arguments) { $this->myService->doCron($arguments['uid']); }
  2. When should constructor work? I tried it with XDebug, but I didn’t catch.
  3. I found new entry for my app in database. As I understand, it never run before, but somehow appeared there.

MariaDB [nextcloud]> select id, class, argument, last_run, last_checked from oc_jobs where class like ‘%PollTask%’;

±------±-----------------------------------------------±---------±---------±-------------+
| id | class | argument | last_run | last_checked |
±------±-----------------------------------------------±---------±---------±-------------+
| 12647 | OCA\StatusNotifications\Notifications\PollTask | null | 0 | 1563372421 |
±------±-----------------------------------------------±---------±---------±-------------+

Please, advice, if you know something about that.

Regards

Any errors in your log?

  1. Why in document it is defined like that. Is it possible to use $this and static in the same method?
    public static function run($arguments) { $this->myService->doCron($arguments['uid']); }

If you need to be using the current instance of your class, you cannot use a static method, however just removing the static keyword should work then :slight_smile:

  1. When should constructor work? I tried it with XDebug, but I didn’t catch.

Depending on how you run the cron task, it might be that xdebug is not catching then because of your php configuration.

  1. I found new entry for my app in database. As I understand, it never run before, but somehow appeared there.

This looks like the static access to an instance you are trying in your run method could cause the actual execution to fail. https://github.com/nextcloud/server/blob/7370fb37bb3dfe684a8abeb3867ade082a29ff1a/lib/public/BackgroundJob/Job.php#L76-L80 Switching to debug logging in your config.php might give you more details on the run of that background job.

Hello,
Thank you for answer!

  1. I tried. It might be created when I set it non-static. I deleted it from database, then re-enabling created my PollTask in oc_jobs again.
  2. Here are my settings in php.ini:
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=on
xdebug.remote_host=x.x.x.x
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_autostart=on
xdebug.remote_mode=req
xdebug.idekey="PHPSTORM"
  1. Actually my log is on debug. It never executed.

By the way, may be it’s problem of arguments?

Mind to share the oc_jobs table entry? Would be good to have a look at the stored timestamps for that job to see why it might never be executed.

This will probably still not trigger for the cron job. You could try running the cron.php manually with

set XDEBUG_CONFIG=remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0
php cron.php

Hello, here it is now:

MariaDB [nextcloud]> select * from oc_jobs where class like '%PollTask%';
+-------+------------------------------------------------+----------+----------+--------------+-------------+--------------------+
| id    | class                                          | argument | last_run | last_checked | reserved_at | execution_duration |
+-------+------------------------------------------------+----------+----------+--------------+-------------+--------------------+
| 44470 | OCA\StatusNotifications\Notifications\PollTask | null     |        0 |   1566430208 |  1566430208 |                  0 |
+-------+------------------------------------------------+----------+----------+--------------+-------------+--------------------+
1 row in set (0.00 sec)

I will try on some other cron job.