使用 Silverstripe 4.11 和 silverstripe-queuedjobs,我尝试在每个给定时间段以 4 封电子邮件为一组发送电子邮件。
(每个块在每个给定时间段向(100 个收件人)中的 4 个收件人发送一封电子邮件)
块 1 发送电子邮件给收件人 A、B、C、D
等待指定时间
块 2 向收件人 E、F、G、H 发送电子邮件
等等......
我尝试使用 sleep() 来暂停块发送的执行(在本例中为 2 分钟),但这使得整个网站没有响应。 (动画银条徽标在队列中可见)。
有没有办法(silverstripe-queuedjobs 模块中的内置函数)以 4 块为一组(私有静态 $chunk_size)发送电子邮件,并且之间有延迟?
或者这是结合使用 cron-job 和 silverstripe-queuedjobs 的目的吗?
您能提供一个示例来实现此目的吗?
感谢您的帮助。
这就是我到目前为止所拥有的:
class SendEmailsJob extends AbstractQueuedJob implements QueuedJob
{
private static $chunk_size = 4;
public function getTitle()
{
return 'Send Emails in Chunks';
}
public function process()
{
$recipients = Recipient::get(); // Assuming Recipients is your DataObject
// Calculate the total number of chunks
$totalChunks = ceil($recipients->count() / self::$chunk_size);
for ($chunk = 0; $chunk < $totalChunks; $chunk++) {
$start = $chunk * self::$chunk_size;
$end = ($chunk + 1) * self::$chunk_size;
$chunkRecipients = $recipients->limit(self::$chunk_size, $start);
foreach ($chunkRecipients as $recipient) {
// Send email to $recipient
$email = Email::create()
->setTo($recipient->Email)
->setSubject('Your Email Subject')
->setBody('Your Email Body')
->send();
}
// Sleep for a while to respect the host's email sending limits
sleep(120); // 2 minutes // 3600 Sleep for 1 hour (adjust as needed)
// Update the job progress
$this->currentStep = $chunk + 1;
$this->totalSteps = $totalChunks;
$this->addMessage("Processed chunk {$this->currentStep}/{$this->totalSteps}");
}
$this->isComplete = true;
}
}
有没有办法(silverstripe-queuedjobs 模块中的内置函数)以 4 块为一组(私有静态 $chunk_size)发送电子邮件,并且之间有延迟?
执行此操作的方法是为每个块创建一个新作业,其中每个作业都有自己的开始时间。