Skip to content
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

feature: allow scheduling of scheduled tasks #290

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.$t('frosh-tools.scheduledTaskScheduleStarted', {'name': item.name})
})
await this.froshToolsService.scheduleScheduledTask(item.id, immediately);
this.createNotificationSuccess({
message: this.$t('frosh-tools.scheduledTaskScheduleSucceed', {'name': item.name})
})
} catch (e) {
this.createNotificationError({
message: this.$t('frosh-tools.scheduledTaskScheduleFailed', {'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