正在发送电子邮件的暂停循环


Pause loop that is sending emails

我正在尝试通过一次从数据库中获取 10 封电子邮件来向我的订阅者列表发送电子邮件。我希望能够暂停并恢复使用控制器操作发送这些电子邮件。

在Symfony中有什么方法(或者这可能是一个一般的PHP问题(可以用另一个动作控制一个控制器动作吗?像这样:

public function sendEmailAction() 
{
    // loop through recipients and send emails
}
public function pauseEmailAction()
{
    // pause the loop in sendEmail
}
public function resumeEmailAction() 
{
    // resume sendEmailAction from the point where 
    // pauseEmailAction has stopped it
}

我可能错过了一些东西,但这应该足以满足您的需求。

public function firstAction()
{
    for (i=0; i < 100; i++) {
        $users = $this->giveMeTenUsers($i);
        $this->secondAction($users);
    }
}
public function secondAction(array $users)
{
    // do stuff like send the emails
    return;
}

firstAction调用secondAction时,firstAction的执行被"停止",等待secondAction的结果。当secondAction命中return;时,它结束了自己的执行,从而将程序发回firstAction,在调用它的循环内。

这就是你需要的吗?还是有我没有得到的元素?

编辑:你为什么不尝试设置一个布尔值来中断或不中断?

喜欢这个:

public function mainAction() // firstAction
{
    for (i=0; i < 100; i++) {
        while ($this->isInterrupted()) {}
        $users = $this->giveMeTenUsers($i);
        // Do stuff
    }
}
public function interruptAction() // secondAction
{
    $this->setInterrupted(true);
}
public function releaseAction() // thirdAction
{
    $this->setInterrupted(false);
}

编辑2:顺便说一下,您不必使用操作,它适用于任何类型的方法。

编辑3:添加到您的班级中

private $interrupted = false;
public function isInterrupted()
{
    return $this->interrupted;
}
public setInterrupted(bool $interrupted)
{
    $this->interrupted = $interrupted;
}