<blockquote>
<p>Class 'modules\importGeoJson\BaseJob' not found</p>
</blockquote>
<p>You're getting this error because you're trying to extend the <a href="https://docs.craftcms.com/api/v3/craft-queue-basejob.html" rel="nofollow noreferrer">craft\queue\BaseJob</a>, but you're not importing (aliasing) that class. In general, a simple class identifier will be assumed to belong to the current namespace. So since your class is namespaced to <code>modules\jobs</code>, the <code>BaseJob</code> class identifier resolves to <code>modules\jobs\BaseJob</code>, which doesn't exist, so you get an error.</p>
<p>To fix this, either import the class with a <code>use</code> statement or use a fully-qualified class name:</p>
<pre class="lang-php prettyprint-override"><code>// option 1: import
use craft\queue\BaseJob

class ImportGeoJson extends BaseJob {}
</code></pre>
<pre class="lang-php prettyprint-override"><code>// option 2: fully-qualified class name
class ImportGeoJson extends \craft\queue\BaseJob
</code></pre>
<p>If you use a fully qualified class name, including the leading backslash (<code>\</code>)  is important – without it, the class name becomes relative to the current namespace again!</p>
<hr />
<p>Not sure if you're just simplifying for your example code, but you should definitely avoid putting more than one class in one file, and especially avoid multiple namespaces in a single file. Your module will soon grow unwieldy and hard to maintain. Take a look at <a href="https://www.php-fig.org/psr/psr-4/" rel="nofollow noreferrer">PSR-4</a>, which provides a good structure to map class names to folders and files. For example, I would use the namespace <code>modules\jobs\queue</code> for your <code>ImportGeoJson</code> job, which means you can place it in the file <code>modules\jobs\queue\ImportGeoJson.php</code>.</p>

CC BY-SA 4.0
2021-09-21T09:20:32.850Z
Thanks - PHP oop is into my strong point but avoiding two classes and splitting into services and job seems the way forward. Im using in a plugin as opposed to module as I want the job fired from admin action. Have the task firing now so just gotta figure out the actual work of the job!
joomkit
CC BY-SA 4.0
2021-09-24T14:30:22.837Z
@joomkit If your issue is solved now, would you mind marking my answer as accepted? Thanks!
MoritzLost
CC BY-SA 4.0
2021-09-21T08:08:08.813Z
2021-09-21T08:08:08.813Z
MoritzLost
<p>For anyone strugglng with this. I split the process into a service and a job.</p>
<p>It seems you also have to register your job/services classes in main plugin file first:</p>
<pre><code> $this-&gt;setComponents([
            'Importgeojson' =&gt; Importgeojson::class, // job
            'MapitapiService' =&gt; MapitapiService::class // service
        ]);
</code></pre>
<p>then call the service from your plugin controller like:</p>
<pre><code>    Mapitapi::$plugin-&gt;mapitapiService-&gt;doBackGroundJob();
</code></pre>
<p>then in your service method (doBackGroundJob) start the queue job</p>
<pre><code>$queue = Craft::$app-&gt;getQueue();
$jobId = $queue-&gt;push(new Importgeojson());
</code></pre>
<p>and in your job file (eg as above - 'Importgeojson'), you have you job class with same name were you do the work of the job:</p>
<pre><code>class Importgeojson extends BaseJob
{
    // Public Properties
    // =========================================================================

    /**
     * @var string
     */
    public $someAttribute = 'Some Default';

    // Public Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    public function execute($queue)
    {
        // Do work here
    }

    // Protected Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    protected function defaultDescription(): string
    {
        return Craft::t('mapitapi', 'Importgeojson');
    }
}
</code></pre>

CC BY-SA 4.0
2021-09-21T09:21:49.283Z
You don't need to set register the job as a component, since you're instantiating it manually anyway. Just make sure to namespace the class correctly and put it in the right place, it should work fine then!
MoritzLost
CC BY-SA 4.0
2021-09-21T09:16:36.950Z
2021-09-21T09:16:36.950Z
joomkit
<p>I am trying to run a background job in a controller but wondering if its actually possible. I'm not sure about <a href="https://craftcms.com/docs/3.x/extend/queue-jobs.html#adding-your-job-to-the-queue" rel="nofollow noreferrer">namespacing</a> and could do with some guidance on whether the job should be running in the plugins service or not. I get errors like: Class 'modules\importGeoJson\BaseJob' not found. I cant really tell from docs or find a clear example on this.</p>
<pre><code>namespace 
namespace\pluginname\controllers;
use Craft;
use craft\web\Controller;
use craft\web\View;
use craft\elements\Entry;
use craft\db\Query;
use craft\base\Element;
use craft\db\QueryAbortedException;

use craft\helpers\App;

class DefaultController extends Controller{
   //etc
  public function StartJob{
        \craft\helpers\Queue::push(new importGeoJson(), 10);
  }
}

namespace modules\jobs;

class importGeoJson extends BaseJob
{
  public function execute($queue)
    {
    }
}
</code></pre>

<plugin-development><queue>
Running a background job in a controller
# Running a background job in a controller

(Asked by: username_0 on 2021-09-20)

I am trying to run a background job in a controller but wondering if its actually possible. I'm not sure about namespacing and could do with some guidance on whether the job should be running in the plugins service or not. I get errors like: Class 'modules\importGeoJson\BaseJob' not found. I cant really tell from docs or find a clear example on this.
namespace 
namespace\pluginname\controllers;
use Craft;
use craft\web\Controller;
use craft\web\View;
use craft\elements\Entry;
use craft\db\Query;
use craft\base\Element;
use craft\db\QueryAbortedException;

use craft\helpers\App;

class DefaultController extends Controller{
   //etc
  public function StartJob{
        \craft\helpers\Queue::push(new importGeoJson(), 10);
  }
}

namespace modules\jobs;

class importGeoJson extends BaseJob
{
  public function execute($queue)
    {
    }
}

**Tags:** plugin-development, queue

---

## 2 answers

### Answer by username_1 on 2021-09-21. Score: 2 | Accepted answer

Class 'modules\importGeoJson\BaseJob' not found

You're getting this error because you're trying to extend the craft\queue\BaseJob, but you're not importing (aliasing) that class. In general, a simple class identifier will be assumed to belong to the current namespace. So since your class is namespaced to modules\jobs, the BaseJob class identifier resolves to modules\jobs\BaseJob, which doesn't exist, so you get an error.
To fix this, either import the class with a use statement or use a fully-qualified class name:
// option 1: import
use craft\queue\BaseJob

class ImportGeoJson extends BaseJob {}

// option 2: fully-qualified class name
class ImportGeoJson extends \craft\queue\BaseJob

If you use a fully qualified class name, including the leading backslash (\)  is important – without it, the class name becomes relative to the current namespace again!

Not sure if you're just simplifying for your example code, but you should definitely avoid putting more than one class in one file, and especially avoid multiple namespaces in a single file. Your module will soon grow unwieldy and hard to maintain. Take a look at PSR-4, which provides a good structure to map class names to folders and files. For example, I would use the namespace modules\jobs\queue for your ImportGeoJson job, which means you can place it in the file modules\jobs\queue\ImportGeoJson.php.

> **Comments:**
> **username_0**: Thanks - PHP oop is into my strong point but avoiding two classes and splitting into services and job seems the way forward. Im using in a plugin as opposed to module as I want the job fired from admin action. Have the task firing now so just gotta figure out the actual work of the job!  
> **username_1**: @joomkit If your issue is solved now, would you mind marking my answer as accepted? Thanks!  

---

### Answer by username_0 on 2021-09-21. Score: 1

For anyone strugglng with this. I split the process into a service and a job.
It seems you also have to register your job/services classes in main plugin file first:
 $this->setComponents([
            'Importgeojson' => Importgeojson::class, // job
            'MapitapiService' => MapitapiService::class // service
        ]);

then call the service from your plugin controller like:
    Mapitapi::$plugin->mapitapiService->doBackGroundJob();

then in your service method (doBackGroundJob) start the queue job
$queue = Craft::$app->getQueue();
$jobId = $queue->push(new Importgeojson());

and in your job file (eg as above - 'Importgeojson'), you have you job class with same name were you do the work of the job:
class Importgeojson extends BaseJob
{
    // Public Properties
    // =========================================================================

    /**
     * @var string
     */
    public $someAttribute = 'Some Default';

    // Public Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    public function execute($queue)
    {
        // Do work here
    }

    // Protected Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    protected function defaultDescription(): string
    {
        return Craft::t('mapitapi', 'Importgeojson');
    }
}

> **Comments:**
> **username_1**: You don't need to set register the job as a component, since you're instantiating it manually anyway. Just make sure to namespace the class correctly and put it in the right place, it should work fine then!  

---

