XF 2.1 How to job works xenforo ?

fahad ashraf

Well-known member
I want to processed some data on background after saving it on database so i create a job


Code:
class jobclass extends AbstractJob {

    public function run($maxRunTime) {
        var_dump('test');exit;
    }

    public function getStatusMessage() {
        $actionPhrase = \XF::phrase('sending');
        $typePhrase = \XF::phrase('xfrm_resources');
        return sprintf('%s... %s (%s/%s)', $actionPhrase, $typePhrase, \XF::language()->numberFormat($this->data['count']), \XF::language()->numberFormat($this->data['total'])
        );
    }

    public function canCancel() {
        return true;
    }

    public function canTriggerByChoice() {
        return false;
    }

}

but when i am en-queue this job by using below code


Code:
     $this->app()->jobManager()->enqueueUnique('xbsend' . $log->log_sms_id, 'XenBulletins\Test:jobclass', [
                'log_id' => $log->log_id
      ]);

so i am getting error

Code:
Could not get runner for job XenBulletins\Test:jobclass (unique: xbsend16). Skipping.


any one please help me how it will work >??
 
Your error means that XF wasn't able to resolve your provided class string XenBulletins\Test:jobclass to your class. The colon in your class string gets resolved to \Job\ at runtime, so XF is looking for XenBulletins\Test\Job\jobclass. Make sure your class has the right namespace and directory structure, and I do think capitalization matters as well.
 
Your error means that XF wasn't able to resolve your provided class string XenBulletins\Test:jobclass to your class. The colon in your class string gets resolved to \Job\ at runtime, so XF is looking for XenBulletins\Test\Job\jobclass. Make sure your class has the right namespace and directory structure, and I do think capitalization matters as well.
Thanks for response yes you are right there was namespace problem . But now my question is how can i run job in background

Thanks
 
Your job does run in the background, hence you'll never see any output from it. It'll be processed at a random time after a request has been passed off to the client. Not even necessarily the request that enqueued it fwiw.
 
Manual jobs run in the foreground in some circumstances. When a manual job is enqueued, redirect responses from admin pages are intercepted to run the job before returning you to the original redirect location. Confusingly, enqueue marks jobs as automatic by default but enqueueUnique does not. You can pass false as a fourth argument to enqueueUnique to mark the job as automatic instead, which should cause it to always run in the background.
 
  • Like
Reactions: Zig
Back
Top Bottom