<p>Got it. Added the following code.</p>

<pre><code>if (!craft()-&gt;tasks-&gt;isTaskRunning()){
    $task = craft()-&gt;tasks-&gt;getNextPendingTask();

    if ($task){
        craft()-&gt;tasks-&gt;runPendingTasks();
    }
}
</code></pre>

CC BY-SA 4.0
2018-10-15T11:45:36.070Z
2018-10-15T11:45:36.070Z
Tim Paine
<p>I have a plugin which runs a task on order complete. The tasks runs successfully without any errors. But the problem is the task is only completed when admin is logged in. So if someone has completed the order, they have to wait until an admin logs in to dashboard. What could I be missing? The code I'm using is :</p>

<p><code>CustomPlugin.php</code></p>

<pre><code>craft()-&gt;on('commerce_orders.onOrderComplete', [$this, 'initializeTask']);

public function initializeTask(Event $event){
    $order = $event-&gt;params['order'];
    foreach ($order-&gt;lineItems as $lineItem) {
        craft()-&gt;tasks-&gt;createTask('Custom', null, [
            'id' =&gt; $order-&gt;id,
        ]);
    }
}
</code></pre>

<p><code>CustomTask.php</code></p>

<pre><code>class CustomTask extends BaseTask
{
    protected function defineSettings()
    {
        return [
            'id' =&gt; AttributeType::Number,
        ];
    }

    public function getDescription()
    {
        return 'Custom Task';
    }

    public function getTotalSteps()
    {
        return 1;
    }

    public function runStep($step)
    {
        $orderId = $this-&gt;getSettings()-&gt;orderId;
        .
        .
        .
        return true;
    }
}
</code></pre>

<p>Is there any type of priority or setting that needs to be set?</p>

<plugin-development><tasks><craft2>
Tasks runs only after admin is logged in
# Tasks runs only after admin is logged in

(Asked by: username_0 on 2018-10-15)

I have a plugin which runs a task on order complete. The tasks runs successfully without any errors. But the problem is the task is only completed when admin is logged in. So if someone has completed the order, they have to wait until an admin logs in to dashboard. What could I be missing? The code I'm using is :
CustomPlugin.php
craft()->on('commerce_orders.onOrderComplete', [$this, 'initializeTask']);

public function initializeTask(Event $event){
    $order = $event->params['order'];
    foreach ($order->lineItems as $lineItem) {
        craft()->tasks->createTask('Custom', null, [
            'id' => $order->id,
        ]);
    }
}

CustomTask.php
class CustomTask extends BaseTask
{
    protected function defineSettings()
    {
        return [
            'id' => AttributeType::Number,
        ];
    }

    public function getDescription()
    {
        return 'Custom Task';
    }

    public function getTotalSteps()
    {
        return 1;
    }

    public function runStep($step)
    {
        $orderId = $this->getSettings()->orderId;
        .
        .
        .
        return true;
    }
}

Is there any type of priority or setting that needs to be set?

**Tags:** plugin-development, tasks, craft2

---

## 1 answers

### Answer by username_0 on 2018-10-15. Score: 1

Got it. Added the following code.
if (!craft()->tasks->isTaskRunning()){
    $task = craft()->tasks->getNextPendingTask();

    if ($task){
        craft()->tasks->runPendingTasks();
    }
}


---

