<p>Might I suggest that a Craft task might not be the best way to pull off what you're trying to achieve.</p>

<p>By default, they will be triggered by the next control panel request, no matter when that occurs.</p>

<p>Sure you could build in some timing logic in your plugin, or set the <a href="https://craftcms.com/docs/config-settings#runTasksAutomatically" rel="nofollow noreferrer">runTasksAutomatically</a> config setting to false and have a cron job hit the task at your set schedule, but that all seems overly complicated.</p>

<p>Just setup an independent cron job to fire when you need your logic to run and have it hit a custom <a href="http://pluginfactory.io" rel="nofollow noreferrer">plugin's</a> <a href="https://craftcms.com/docs/plugins/controllers" rel="nofollow noreferrer">controller's action</a> and run your logic directly outside of Craft's task implementation.</p>

CC BY-SA 3.0
2017-01-31T04:54:03.653Z
2017-01-31T04:54:03.653Z
Brad Bell
<p>I already learned how to create a task from <a href="https://craftcms.stackexchange.com/questions/14530/tasks-and-steps">this page</a> but I would like to know how to run this task?
I'm creating a portal where users are allowed to give their votes and discuss several projects. Backend users can create different phases and declare a start and an end date and a few days before these phases end all members/participants of this phase should receive an email.</p>
<p>I know how to send the mail, I know how to create the task but I don't know how to start the task 1, 3 and 7 days before a specific date.</p>
<p>Do those steps run every single second?</p>
<pre><code>public function runStep($step)
{
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
    $criteria-&gt;group = 'phase';
    if($criteria-&gt;count()){
        $phases =  $criteria-&gt;find();
        foreach ($phases as $phase){
            $now = time();
            $threeDaysBefore = $now - (60 * 60 * 24 * 3); //Seconds * Minutes * Hours * Days
            $OneDaysBefore   = $now - (60 * 60 * 24 * 1); //Seconds * Minutes * Hours * Days
            $endTime = $phase-&gt;phaseEndDate-&gt;getTimestamp();
            if($endTime == $OneDaysBefore || $endTime == $threeDaysBefore){
                 craft()-&gt;myplugin-&gt;sendMailToUser();
            }
        }
    }
}
</code></pre>
<p>Where do I activate the task? Do I run <code>craft()-&gt;tasks-&gt;createTask(/*parameter*/)</code> in my plugin init() function?</p>
<p>And my last question: how can I debug my function? In my chrome network tap I receive a taskInfo every second but I can't <code>var_dump()</code> something from my <code>runstep</code> function.</p>
<p>Thank you very much</p>

<plugin-development><tasks>
Craft task send mails at specific date
# Craft task send mails at specific date

(Asked by: username_0 on 2017-01-30)

I already learned how to create a task from this page but I would like to know how to run this task?
I'm creating a portal where users are allowed to give their votes and discuss several projects. Backend users can create different phases and declare a start and an end date and a few days before these phases end all members/participants of this phase should receive an email.
I know how to send the mail, I know how to create the task but I don't know how to start the task 1, 3 and 7 days before a specific date.
Do those steps run every single second?
public function runStep($step)
{
    $criteria = craft()->elements->getCriteria(ElementType::Category);
    $criteria->group = 'phase';
    if($criteria->count()){
        $phases =  $criteria->find();
        foreach ($phases as $phase){
            $now = time();
            $threeDaysBefore = $now - (60 * 60 * 24 * 3); //Seconds * Minutes * Hours * Days
            $OneDaysBefore   = $now - (60 * 60 * 24 * 1); //Seconds * Minutes * Hours * Days
            $endTime = $phase->phaseEndDate->getTimestamp();
            if($endTime == $OneDaysBefore || $endTime == $threeDaysBefore){
                 craft()->myplugin->sendMailToUser();
            }
        }
    }
}

Where do I activate the task? Do I run craft()->tasks->createTask(/*parameter*/) in my plugin init() function?
And my last question: how can I debug my function? In my chrome network tap I receive a taskInfo every second but I can't var_dump() something from my runstep function.
Thank you very much

**Tags:** plugin-development, tasks

---

## 1 answers

### Answer by username_1 on 2017-01-31. Score: 3 | Accepted answer

Might I suggest that a Craft task might not be the best way to pull off what you're trying to achieve.

By default, they will be triggered by the next control panel request, no matter when that occurs.

Sure you could build in some timing logic in your plugin, or set the [runTasksAutomatically](https://craftcms.com/docs/config-settings#runTasksAutomatically) config setting to false and have a cron job hit the task at your set schedule, but that all seems overly complicated.

Just setup an independent cron job to fire when you need your logic to run and have it hit a custom [plugin's](http://pluginfactory.io) [controller's action](https://craftcms.com/docs/plugins/controllers) and run your logic directly outside of Craft's task implementation.


---

