1

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

3

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 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 controller's action and run your logic directly outside of Craft's task implementation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.