Skip to content

Commit

Permalink
feature: allow scheduling of scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
M-arcus committed Nov 13, 2024
1 parent b179ad6 commit bf1fc5f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Controller/ScheduledTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Shopware\Core\Framework\MessageQueue\ScheduledTask\Scheduler\TaskRunner;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

Expand Down Expand Up @@ -52,6 +53,30 @@ public function runTask(string $id, Context $context): JsonResponse
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}

#[Route(path: '/scheduled-task/schedule/{id}', name: 'api.frosh.tools.scheduled.task.schedule', methods: ['POST'])]
public function scheduleTask(Request $request, string $id, Context $context): JsonResponse
{
$scheduledTask = $this->fetchTask($id, $context);

if (!$scheduledTask instanceof ScheduledTaskEntity) {
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
}

$data = [
'id' => $id,
'status' => ScheduledTaskDefinition::STATUS_SCHEDULED,
];

$immediately = $request->request->has('immediately') && $request->request->get('immediately', false);
if ($immediately) {
$data['nextExecutionTime'] = new \DateTime();
}

$this->scheduledTaskRepository->update([$data], $context);

return new JsonResponse(null, Response::HTTP_NO_CONTENT);
}

#[Route(path: '/scheduled-tasks/register', name: 'api.frosh.tools.scheduled.tasks.register', methods: ['POST'])]
public function registerTasks(): JsonResponse
{
Expand Down
15 changes: 15 additions & 0 deletions src/Resources/app/administration/src/api/frosh-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class FroshTools extends ApiService {
});
}

scheduleScheduledTask(id, immediately = false) {
const apiRoute = `${this.getApiBasePath()}/scheduled-task/schedule/${id}`;
return this.httpClient.post(
apiRoute,
{
'immediately': immediately
},
{
headers: this.getBasicHeaders()
}
).then((response) => {
return ApiService.handleResponse(response);
});
}

scheduledTasksRegister() {
const apiRoute = `${this.getApiBasePath()}/scheduled-tasks/register`;
return this.httpClient.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ Component.register('frosh-tools-tab-scheduled', {
this.createdComponent();
},

async scheduleTask(item, immediately = false) {
this.isLoading = true;

try {
this.createNotificationInfo({
message: this.$tc('frosh-tools.scheduledTaskScheduleStarted', 0, {'name': item.name})
})
await this.froshToolsService.scheduleScheduledTask(item.id, immediately);
this.createNotificationSuccess({
message: this.$tc('frosh-tools.scheduledTaskScheduleSucceed', 0, {'name': item.name})
})
} catch (e) {
this.createNotificationError({
message: this.$tc('frosh-tools.scheduledTaskScheduleFailed', 0, {'name': item.name})
})

this.taskError = e.response.data;
}

this.createdComponent();
},

async registerScheduledTasks() {
this.isLoading = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<sw-context-menu-item variant="primary" @click="runTask(item)">
{{ $tc('frosh-tools.runManually') }}
</sw-context-menu-item>
<sw-context-menu-item variant="primary" @click="scheduleTask(item)">
{{ $tc('frosh-tools.setToScheduled') }}
</sw-context-menu-item>
<sw-context-menu-item variant="primary" @click="scheduleTask(item, true)">
{{ $tc('frosh-tools.setToScheduledImmediately') }}
</sw-context-menu-item>
</template>
</sw-entity-listing>
</sw-card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
"scheduledTaskStarted": "Die geplante Aufgabe für \"{name}\" wurde gestartet",
"scheduledTaskSucceed": "Die geplante Aufgabe für \"{name}\" war erfolgreich",
"scheduledTaskFailed": "Die geplante Aufgabe für \"{name}\" ist fehlgeschlagen",
"setToScheduled": "auf geplant setzen",
"setToScheduledImmediately": "auf sofort geplant setzen",
"scheduledTaskScheduleStarted": "Die geplante Aufgabe für \"{name}\" wird auf geplant gesetzt",
"scheduledTaskScheduleSucceed": "Die geplante Aufgabe für \"{name}\" wurde auf geplant gesetzt",
"scheduledTaskScheduleFailed": "Die geplante Aufgabe für \"{name}\" konnte nicht auf geplant gesetzt werden",
"scheduledTasksRegisterStarted": "Registriere geplante Aufgaben",
"scheduledTasksRegisterSucceed": "Geplante Aufgaben registriert",
"scheduledTasksRegisterFailed": "Es ist ein Fehler beim Registrieren aufgetreten",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
"scheduledTaskStarted": "The scheduled task execution for \"{name}\" started",
"scheduledTaskSucceed": "The scheduled task execution for \"{name}\" succeed",
"scheduledTaskFailed": "The scheduled task execution for \"{name}\" failed",
"setToScheduled": "Set task status to scheduled",
"setToScheduledImmediately": "Set task status to scheduled immediately",
"scheduledTaskScheduleStarted": "The scheduled task for \"{name}\" is being scheduled",
"scheduledTaskScheduleSucceed": "The scheduled task for \"{name}\" has been scheduled",
"scheduledTaskScheduleFailed": "The scheduled task for \"{name}\" could not be scheduled",
"scheduledTasksRegisterStarted": "Register scheduled tasks",
"scheduledTasksRegisterSucceed": "Scheduled tasks registered",
"scheduledTasksRegisterFailed": "Scheduled tasks registration failed",
Expand Down

0 comments on commit bf1fc5f

Please sign in to comment.