nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
27.22k stars 4.05k forks source link

[Bug]: Manually installing a Background job will not be performed #32909

Closed burningCatalyst closed 2 years ago

burningCatalyst commented 2 years ago

⚠️ This issue respects the following points: ⚠️

Bug description

If I try to add a background job manually (via the jobList->add method) it will be listed in the in the oc_jobs table (in the DB):

Bildschirmfoto 2022-06-17 um 09 33 25

but when it executes it don't give the argument with it and this error will show up:

ArgumentCountError: Too few arguments to function OCA\AutoDelete\Cron\AutoDeleteAPI::__construct(), 0 passed in /var/www/web/lib/private/BackgroundJob/JobList.php on line 307 and exactly 2 expected

Hope you can help :)

Steps to reproduce

  1. Make a basic App like here
  2. Activate cron in your backend (so Nextcloud is using the cron.php)
  3. Add something like this in your crontab: * * * * * sudo -u docker bash -l -c 'php -f /var/www/web/cron.php'
  4. Add a Button in frontend to send data to the Background job
  5. Add a second controller like here , a background Job like here and a second service, mapper and second entity class (like Note.php in the tutorial) like in the tutorial
  6. Push the button to trigger the background job "installation"

Expected behavior

Execute the selfmade background task with the given arguments.

Installation method

Official Docker image

Operating system

No response

PHP engine version

PHP 8.0

Web server

Apache (supported)

Database engine version

MariaDB

Is this bug present after an update or on a fresh install?

Fresh Nextcloud Server install

Are you using the Nextcloud Server Encryption module?

No response

What user-backends are you using?

Configuration report

No response

List of activated Apps

Accessibility   1.10.0  
Activity    2.16.0  
Auto Delete 0.5.0   (my own app)    
Brute-force settings    2.4.0   
Circles 24.0.0  
Collaborative tags  1.14.0  
Comments    1.14.0  
Contacts Interaction    1.5.0   
Dashboard   7.4.0   
Deleted files   1.14.0  
Federation  1.14.0  
File sharing    1.16.2  
First run wizard    2.13.0  
Log Reader  2.9.0   
Monitoring  1.14.0  
Nextcloud announcements 1.13.0  
Notifications   2.12.0  
Password policy 1.14.0  
PDF viewer  2.5.0   
Photos  1.6.0   
Privacy 1.8.0   
Recommendations 1.3.0   
Right click 1.3.0   
Share by mail   1.14.0  
Support 1.7.0   
Text    3.5.1   
Theming 1.15.0  
Update notification 1.14.0  
Usage survey    1.12.0  
User status 1.4.0   
Versions    1.17.0  
Video player    1.13.0  
Weather status  1.4.0   
Auditing / Logging  1.14.0  
Default encryption module   2.12.0  
External storage support    1.16.1  
LDAP user and group backend 1.14.1

Nextcloud Signing status

No response

Nextcloud Logs

{"reqId":"eZVFA3bEtU7QwqdMGMZz","level":3,"time":"2022-06-17T06:50:03+00:00","remoteAddr":"","user":"--","app":"cron","method":"","url":"--","message":"Too few arguments to function OCA\\AutoDelete\\Cron\\AutoDeleteAPI::__construct(), 0 passed in /var/www/web/lib/private/BackgroundJob/JobList.php on line 307 and exactly 2 expected","userAgent":"--","version":"24.0.1.1","exception":{"Exception":"ArgumentCountError","Message":"Too few arguments to function OCA\\AutoDelete\\Cron\\AutoDeleteAPI::__construct(), 0 passed in /var/www/web/lib/private/BackgroundJob/JobList.php on line 307 and exactly 2 expected","Code":0,"Trace":[{"file":"/var/www/web/lib/private/BackgroundJob/JobList.php","line":307,"function":"__construct","class":"OCA\\AutoDelete\\Cron\\AutoDeleteAPI","type":"->","args":[]},{"file":"/var/www/web/lib/private/BackgroundJob/JobList.php","line":241,"function":"buildJob","class":"OC\\BackgroundJob\\JobList","type":"->","args":[{"id":"128","class":"OCA\\AutoDelete\\Cron\\AutoDeleteAPI","argument":"{\"path\":\"dataToDelete.json\"}","last_run":"0","last_checked":"1655178242","0":"And 4 more entries, set log level to debug to see all entries"}]},{"file":"/var/www/web/cron.php","line":144,"function":"getNext","class":"OC\\BackgroundJob\\JobList","type":"->","args":[false]}],"File":"/var/www/web/apps/autodelete/lib/Cron/AutoDeleteAPI.php","Line":23,"CustomMessage":"--"}}

Additional info

My second Controller looks like this:

<?php
declare(strict_types=1);
namespace OCA\AutoDelete\Controller;

use OCP\IRequest;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use \OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\AppFramework\Http\DataResponse;

use OCA\AutoDelete\Cron\AutoDeleteAPI;

class AutoDeleteCronController extends Controller 
{
    /** @var IJobList */
    private IJobList $jobList;
    /** @var IConfig */
    private $config;

    /**
     * @param string $appName
     * @param IRequest $request
     * @param IJobList $jobList
     * @param IConfig $config
     */
    public function __construct($appName, IRequest $request, IJobList $jobList, IConfig $config) 
    {
        parent::__construct($appName, $request);
        $this->jobList = $jobList;
        $this->config = $config;
    }

    // Add job in 'oc_jobs'
    /**
     * @param string $dataToDelete
     * @return DataResponse
     */
    public function addJob(string $dataToDelete)
    {
        // Save the path to the data in a file
        $tempData = 'dataToDelete.json';
        $h = fopen($tempData, 'w');
        fwrite($h, $dataToDelete . PHP_EOL);
        fclose($h);

        $this->jobList->add(AutoDeleteAPI::class, ['path' => $tempData]);

        //return new DataResponse($dataToDelete);
    }
}

And the Background Job looks like this:

<?php
declare(strict_types=1);
namespace OCA\AutoDelete\Cron;

use OCA\Autodelete\Service\AutoDeleteCronService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;

class AutoDeleteAPI extends TimedJob
{
    // Cronjob: * * * * * sudo -u docker bash -l -c 'php -f /var/www/web/cron.php'
    /** @var AutoDeleteCronService */
    private $myservice;
    /** @var ITimeFactory */
    protected $time;

    /**
     * @param AutoDeleteCronService $service
     * @param ITimeFactory $time
     */
    public function __construct(AutoDeleteCronService $service, ITimeFactory $time) 
    {
        // Do the Job every Day
        $this->setInterval(30);
        // Delay until low-load time
        //$this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);

        parent::__construct($time);
        $this->time = $time;
        $this->myservice = $service;

    }

    // Trigger the Service
    /**
     * @param $argument
     */
    protected function run($argument)
    {
        $this->myservice->doCron($argument);
    }
}
kesselb commented 2 years ago

Too few arguments to function OCA\AutoDelete\Cron\AutoDeleteAPI::__construct(), 0 passed

Mean: DI container is not able to autowire your class.

https://github.com/nextcloud/server/blob/b17c4a60727ac66e05102399b4b9cdd5ce7cf725/lib/private/BackgroundJob/JobList.php#L298-L326

When you end up in line 307 we run into a query exception before and try to instantiate the class with new. Please set a breakpoint in line 303 and debug why the container is not able to resolve your dependency. That can be anything from a missing composer dump-autoload to configuration error.

https://help.nextcloud.com/c/dev/11 is our channel for developer questions.

PVince81 commented 2 years ago

please reach out to the developer chat or the forum for programming questions, this is not a bug otherwise most apps' background jobs would not work at all

avinash-0007 commented 2 years ago

myself also faced the same issue and same error when i create a new background job.

avinash-0007 commented 2 years ago

ArgumentCountError: Too few arguments to function OCA\Calendar\BackgroundJob\SendInviteResponseMailJob::__construct(), 0 passed in /var/www/html/lib/private/BackgroundJob/JobList.php on line 281 and exactly 9 expected in /var/www/html/custom_apps/calendar/lib/BackgroundJob/SendInviteResponseMailJob.php:45

avinash-0007 commented 2 years ago

Can anyone tell whats wrong ?

avinash-0007 commented 2 years ago

@PVince81 can you please tell why this is happening when we create a new background job manual.

burningCatalyst commented 2 years ago

Since my nextcloud is only for testing, I reinstalled it and it worked.

avinash-0007 commented 2 years ago

@burningCatalyst reinstalled it is not a right solution i guess. can you tell what is causing this issue.