/**
* Sign emails to claim emails send by CURRENT PROCESS, then signed emails will not be retrieved by other processes.
*
* @param bool $signUnassigned Whether to sign email those are not assigned to any server_id (IS NULL).
* Take effects only when column specified by EmailQueueCommand::$sendByCol exists.
* @param bool $renewSignature Whether to renew the signature when sign emails every time.
*
* @return int The numbers of mails signed this time.
*
*/
protected function signEmails($signUnassigned = true, $renewSignature = false)
{
$modelClass = $this->_templateModel;
$where = [$this->signatureAttr => null];
if ($modelClass->hasAttribute($this->assignedToSvrAttr)) {
$where = ['and', $where];
if ($signUnassigned) {
$where[] = ['or', [$this->assignedToSvrAttr => $this->serverID], [$this->assignedToSvrAttr => null]];
} else {
$where[] = [$this->assignedToSvrAttr => $this->serverID];
}
} elseif (!$signUnassigned) {
$msg = "Cannot find any entries to sign, while 'signUnassigned' is false ";
$msg .= "and attribute '{$this->assignedToSvrAttr}' is missing in model class.";
$this->consoleLog($msg, true, Console::FG_YELLOW);
return 0;
}
$cols = [$this->signatureAttr => $this->getSignature($renewSignature)];
$queryBuilder = $this->_db->getQueryBuilder();
$sql = $queryBuilder->update($modelClass::tableName(), $cols, $where, $params);
if ($this->signSize) {
$sql = $queryBuilder->buildOrderByAndLimit($sql, null, $this->signSize, null);
}
return $this->_db->createCommand($sql, $params)->execute();
}