-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Queue mechanism to stop processing jobs #24
base: development
Are you sure you want to change the base?
Changes from 4 commits
837afd9
958f7ef
bbe1f7b
a96d586
3e7e4b0
f6e3b27
07c63b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ SENDMAIL_DSN=sendmail://default | |
MAILER_DSN=native://default | ||
|
||
MAIL_CHARSET=utf-8 | ||
|
||
MAX_ATTEMPTS_DEFAULT=10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ani2amigos as we have a config file to deal with the envs and load them all at once, you can move this |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,16 +112,18 @@ public function ack(MailJobInterface $mailJob) | |
} | ||
|
||
$pheanstalk = $this->getConnection()->getInstance()->useTube($this->queueName); | ||
if ($mailJob->isCompleted()) { | ||
$pheanstalk->delete($mailJob->getPheanstalkJob()); | ||
return null; | ||
} | ||
|
||
$timestamp = $mailJob->getTimeToSend(); | ||
$delay = max(0, $timestamp - time()); | ||
$pheanstalk->delete($mailJob->getPheanstalkJob()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pheanstalk has a method to re-queue the job, the release as it was set.
|
||
$mailJob->incrementAttempt(); | ||
if (!$mailJob->isCompleted() && $mailJob->getAttempt() <= $_ENV['MAX_ATTEMPTS_DEFAULT']) { | ||
$timestamp = $mailJob->getTimeToSend(); | ||
$delay = max(0, $timestamp - time()); | ||
// add back to the queue as it wasn't completed maybe due to some transitory error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove any comment from code not related to docs |
||
// could also be failed. | ||
$pheanstalk->release($mailJob->getPheanstalkJob(), Pheanstalk::DEFAULT_PRIORITY, $delay); | ||
|
||
$pheanstalk->put($this->createPayload($mailJob), Pheanstalk::DEFAULT_PRIORITY, $delay); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
@@ -150,4 +152,5 @@ protected function createPayload(MailJobInterface $mailJob) | |
'message' => $mailJob->getMessage(), | ||
]); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,17 +112,26 @@ public function ack(MailJobInterface $mailJob) | |
throw new InvalidCallException('PdoMailJob cannot be a new object to be acknowledged'); | ||
} | ||
|
||
$sqlText = 'UPDATE `%s` | ||
$mailJob->incrementAttempt(); | ||
if($mailJob->getAttempt() > $_ENV['MAX_ATTEMPTS_DEFAULT']){ | ||
$sqlText = 'DELETE FROM mail_queue WHERE id = :max_attempt;'; | ||
$sql = sprintf($sqlText, $this->tableName); | ||
$query = $this->getConnection()->getInstance()->prepare($sql); | ||
$query->bindValue(':id', $mailJob->getId()); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid using
|
||
$sqlText = 'UPDATE `%s` | ||
SET `attempt`=:attempt, `state`=:state, `timeToSend`=:timeToSend, `sentTime`=:sentTime | ||
WHERE `id`=:id'; | ||
$sql = sprintf($sqlText, $this->tableName); | ||
$sentTime = $mailJob->isCompleted() ? date('Y-m-d H:i:s', time()) : null; | ||
$query = $this->getConnection()->getInstance()->prepare($sql); | ||
$query->bindValue(':id', $mailJob->getId(), PDO::PARAM_INT); | ||
$query->bindValue(':attempt', $mailJob->getAttempt(), PDO::PARAM_INT); | ||
$query->bindValue(':state', $mailJob->getState()); | ||
$query->bindValue(':timeToSend', $mailJob->getTimeToSend()); | ||
$query->bindValue(':sentTime', $sentTime); | ||
$sql = sprintf($sqlText, $this->tableName); | ||
$sentTime = $mailJob->isCompleted() ? date('Y-m-d H:i:s', time()) : null; | ||
$query = $this->getConnection()->getInstance()->prepare($sql); | ||
$query->bindValue(':id', $mailJob->getId(), PDO::PARAM_INT); | ||
$query->bindValue(':attempt', $mailJob->getAttempt(), PDO::PARAM_INT); | ||
$query->bindValue(':state', $mailJob->getState()); | ||
$query->bindValue(':timeToSend', $mailJob->getTimeToSend()); | ||
$query->bindValue(':sentTime', $sentTime); | ||
} | ||
|
||
return $query->execute(); | ||
} | ||
|
||
|
@@ -138,4 +147,5 @@ public function isEmpty() | |
$query->execute(); | ||
return intval($query->fetchColumn(0)) === 0; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,5 @@ public function ack(MailJobInterface $mailJob); | |
* @return bool | ||
*/ | ||
public function isEmpty(); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10 its a lot of to be set as default, you can change this to be 3