Skip to content

Commit c65c506

Browse files
committed
Added checks for existing job
1 parent 9d37c88 commit c65c506

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

src/Controller/Controller.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,16 @@ public function jobMessage(): array {
9292
$jobId = $this->requestStack->getCurrentRequest()->get('job_id');
9393
$job = $this->helper->getJobFromId($jobId);
9494

95+
if (empty($job)) {
96+
$message = $this->t('Job not found');
97+
}
98+
else {
99+
$message = $job->getMessage();
100+
}
101+
95102
$renderArray['content'] = [
96103
'#type' => 'markup',
97-
'#markup' => '<p>' . $job->getMessage() . '</p>',
104+
'#markup' => '<p>' . $message . '</p>',
98105
];
99106

100107
return $renderArray;

src/Form/RetryJob.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function getFormId(): string {
5757
*/
5858
public function getQuestion(): TranslatableMarkup {
5959
$job = $this->helper->getJobFromId((string) $this->jobId);
60+
61+
if (empty($job)) {
62+
return $this->t('Job not found');
63+
}
64+
6065
$webformId = $this->helper->getWebformIdFromQueue($job->getId());
6166

6267
return $this->t('Are you sure you want to retry queue job related to Webform: @webformId, Submission id: @serialId', [
@@ -94,22 +99,22 @@ public function buildForm(array $form, FormStateInterface $form_state, int $job_
9499
*/
95100
public function submitForm(array &$form, FormStateInterface $form_state): void {
96101
$job = $this->helper->getJobFromId((string) $this->jobId);
97-
$queue_id = $job->getQueueId();
102+
if (!empty($job)) {
103+
$queue_id = $job->getQueueId();
98104

99-
$queue_storage = $this->entityTypeManager->getStorage('advancedqueue_queue');
100-
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
101-
$queue = $queue_storage->load($queue_id);
105+
$queue_storage = $this->entityTypeManager->getStorage('advancedqueue_queue');
106+
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
107+
$queue = $queue_storage->load($queue_id);
102108

103-
$queue_backend = $queue->getBackend();
104-
if ($queue_backend instanceof Database) {
105-
$job = $this->helper->getJobFromId((string) $this->jobId);
109+
$queue_backend = $queue->getBackend();
110+
if ($queue_backend instanceof Database) {
111+
if ($job->getState() != Job::STATE_FAILURE) {
112+
throw new \InvalidArgumentException('Only failed jobs can be retried.');
113+
}
106114

107-
if ($job->getState() != Job::STATE_FAILURE) {
108-
throw new \InvalidArgumentException('Only failed jobs can be retried.');
115+
$queue_backend->retryJob($job);
116+
$form_state->setRedirectUrl($this->getCancelUrl());
109117
}
110-
111-
$queue_backend->retryJob($job);
112-
$form_state->setRedirectUrl($this->getCancelUrl());
113118
}
114119
}
115120

src/Helper/Helper.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ public function __construct(
3838
* @param string $jobId
3939
* The job id.
4040
*
41-
* @return \Drupal\advancedqueue\Job
41+
* @return \Drupal\advancedqueue\Job|null
4242
* A list of attributes related to a job.
4343
*/
44-
public function getJobFromId(string $jobId): Job {
44+
public function getJobFromId(string $jobId): Job|NULL {
4545
$query = $this->connection->select('advancedqueue', 'a');
4646
$query->fields('a');
4747
$query->condition('job_id', $jobId, '=');
4848
$definition = $query->execute()->fetchAssoc();
4949

50+
if (empty($definition)) {
51+
return NULL;
52+
}
5053
// Match Job constructor id.
5154
$definition['id'] = $definition['job_id'];
5255

@@ -67,6 +70,9 @@ public function getJobFromId(string $jobId): Job {
6770
*/
6871
public function getSubmissionIdFromJob(string $jobId): ?int {
6972
$job = $this->getJobFromId($jobId);
73+
if (empty($job)) {
74+
return NULL;
75+
}
7076
$payload = $job->getPayload();
7177

7278
return $payload['submissionId'] ?? $payload['submission']['id'] ?? NULL;

src/Plugin/Action/RetryJob.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public static function create(ContainerInterface $container, $configuration, $pl
8686
*/
8787
public function execute(string $jobId = NULL): void {
8888
$job = $this->helper->getJobFromId($jobId);
89+
if (empty($job)) {
90+
return;
91+
}
8992
$queue_id = $job->getQueueId();
9093

9194
$queue_storage = $this->entityTypeManager->getStorage('advancedqueue_queue');

src/Plugin/views/field/AdvancedQueueBulkForm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public function viewsFormSubmit(&$form, FormStateInterface $form_state): void {
169169
foreach ($selected as $bulk_form_key) {
170170
// Skip execution if the user did not have access.
171171
$job = $this->helper->getJobFromId($bulk_form_key);
172+
if (empty($job)) {
173+
return;
174+
}
172175
if ('failure' !== $job->getState()) {
173176
$this->messenger->addError($this->t('Element with webform submission id: @webform_submit_id already has state: @state', [
174177
'@webform_submit_id' => $this->helper->getSubmissionSerialIdFromJob($bulk_form_key),

0 commit comments

Comments
 (0)