-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nswdpc/fix-queuedjob
Queued job fixes and enhancements
- Loading branch information
Showing
4 changed files
with
232 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace NSWDPC\Messaging\Mailgun; | ||
|
||
/** | ||
* This exception is thrown when an error occurs in a job | ||
*/ | ||
class JobProcessingException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
namespace NSWDPC\Messaging\Mailgun; | ||
|
||
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; | ||
use Symbiote\QueuedJobs\Services\QueuedJob; | ||
use SilverStripe\Core\Config\Config; | ||
use Symbiote\QueuedJobs\Services\QueuedJobService; | ||
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; | ||
|
||
/** | ||
* @author James Ellis <james.ellis@dpc.nsw.gov.au> | ||
* Job used to requeue SendJob descriptors marked broken | ||
* This is helpful if you have high mail traffic and have many broken {@link SendJob} records sitting in the queue | ||
*/ | ||
class RequeueJob extends AbstractQueuedJob | ||
{ | ||
|
||
public function getTitle() | ||
{ | ||
return _t( | ||
__CLASS__ . ".JOB_TITLE", | ||
"Re-queue failed attempts to send messages via the Mailgun API" | ||
); | ||
} | ||
|
||
/** | ||
* Attempt to send the message via the Mailgun API | ||
*/ | ||
public function process() | ||
{ | ||
|
||
$descriptors = QueuedJobDescriptor::get() | ||
->filter([ | ||
'JobStatus' => QueuedJob::STATUS_BROKEN, | ||
'Implementation' => SendJob::class | ||
]); | ||
$count = $descriptors->count(); | ||
$kick = $skip = 0; | ||
if($count > 0) { | ||
$this->totalSteps = $count; | ||
foreach($descriptors as $descriptor) { | ||
|
||
$data = @unserialize($descriptor->SavedJobData); | ||
if(empty($data->parameters)) { | ||
// parameters cleared so pointless re-queuing | ||
$skip++; | ||
continue; | ||
} | ||
|
||
// recreate this job as new | ||
$next = new \Datetime(); | ||
$next->modify('+1 minute'); | ||
|
||
$descriptor->StartAfter = $next->format('Y-m-d H:i:s'); | ||
$descriptor->JobStatus = QueuedJob::STATUS_NEW; | ||
$descriptor->StepsProcessed = 0; | ||
$descriptor->LastProcessedCount = -1; | ||
$descriptor->Worker = null;// clear otherwise job is considered locked | ||
$descriptor->write(); | ||
|
||
$kick++; | ||
$this->currentStep++; | ||
} | ||
|
||
$this->addMessage( | ||
_t( | ||
__CLASS__ . '.JOB_STATUS', | ||
"Marked {kick}, ignored {skip} broken SendJob descriptors as new", | ||
[ | ||
'kick' => $kick, | ||
'skip' => $skip | ||
] | ||
), | ||
"info" | ||
); | ||
|
||
} else { | ||
$this->addMessage( | ||
_t( | ||
__CLASS__ . '.JOB_STATUS_NO_JOBS', | ||
"No jobs can be re-queued" | ||
), | ||
"info" | ||
); | ||
} | ||
|
||
$this->isComplete = true; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters