Skip to content

Commit 9e8c094

Browse files
committed
Set task scheduled if a delay has been set
1 parent 1ec96f6 commit 9e8c094

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

dashboard/src/api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function fetchTasks(into, query = {}) {
1818

1919
paused = true
2020
fetch(
21-
`${import.meta.env.VITE_API_URL}/cloud-tasks-api/tasks?${queryParams.toString()}`
21+
`${import.meta.env.VITE_API_URL || ''}/cloud-tasks-api/tasks?${queryParams.toString()}`
2222
)
2323
.then((response) => response.json())
2424
.then((response) => {

dashboard/src/components/Dashboard.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const dashboard = ref({
1616
1717
const tsLoaded = Math.floor(Date.now() / 1000)
1818
19-
fetch(`${import.meta.env.VITE_API_URL}/cloud-tasks-api/dashboard`)
19+
fetch(`${import.meta.env.VITE_API_URL || ''}/cloud-tasks-api/dashboard`)
2020
.then((response) => response.json())
2121
.then((response) => (dashboard.value = response))
2222
</script>

dashboard/src/components/Status.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ function ucfirst(input) {
2828
.task-error {
2929
@apply bg-red-100/50 text-red-600/50
3030
}
31-
.task-queued {
31+
.task-queued, .task-scheduled {
3232
@apply bg-gray-100 text-gray-500
3333
}
3434
.task-running {
3535
@apply bg-blue-100 text-blue-800
3636
}
37-
</style>
37+
</style>

dashboard/src/components/Task.vue

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ const task = ref({
1111
status: 'loading',
1212
})
1313
14-
fetch(`${import.meta.env.VITE_API_URL}/cloud-tasks-api/task/${route.params.uuid}`)
14+
fetch(`${import.meta.env.VITE_API_URL || ''}/cloud-tasks-api/task/${route.params.uuid}`)
1515
.then((response) => response.json())
1616
.then((response) => (task.value = response))
1717
1818
const titles = {
19+
scheduled: 'Scheduled',
1920
queued: 'Added to the queue',
2021
running: 'Running',
2122
successful: 'Successful',
@@ -47,6 +48,13 @@ const titles = {
4748
>{{ task.queue }}</span
4849
>
4950
</div>
51+
<div v-if="event['scheduled_at']">
52+
<span
53+
class="bg-gray-200 text-gray-800 text-xs font-medium mr-2 inline-block mb-1 px-1.5 py-0.5 rounded dark:bg-blue-200 dark:text-blue-800"
54+
>
55+
Scheduled: {{ event['scheduled_at'] }} (UTC)
56+
</span>
57+
</div>
5058
</h3>
5159
<Popper
5260
:content="event.datetime"

src/CloudTasksQueue.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
122122

123123
$createdTask = CloudTasksApi::createTask($queueName, $task);
124124

125-
event((new TaskCreated)->queue($queue)->task($createdTask));
125+
event((new TaskCreated)->queue($queue)->task($task));
126126
}
127127

128128
private function withUuid(string $payload): string

src/MonitoringService.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,27 @@ public function addToMonitor(string $queue, Task $task): void
3333
{
3434
$metadata = new TaskMetadata();
3535
$metadata->payload = $this->getTaskBody($task);
36-
$metadata->addEvent('queued', [
36+
37+
$data = [
3738
'queue' => $queue,
38-
]);
39+
];
40+
41+
if ($task->hasScheduleTime()) {
42+
$status = 'scheduled';
43+
$data['scheduled_at'] = $task->getScheduleTime()->toDateTime()->format('Y-m-d H:i:s');
44+
} else {
45+
$status = 'queued';
46+
}
47+
48+
$metadata->addEvent($status, $data);
3949

4050
DB::table('stackkit_cloud_tasks')
4151
->insert([
4252
'task_uuid' => $this->getTaskUuid($task),
4353
'name' => $this->getTaskName($task),
4454
'queue' => $queue,
4555
'payload' => $this->getTaskBody($task),
46-
'status' => 'queued',
56+
'status' => $status,
4757
'metadata' => $metadata->toJson(),
4858
'created_at' => now()->utc(),
4959
'updated_at' => now()->utc(),

tests/CloudTasksMonitoringTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,31 @@ public function when_monitoring_is_disabled_jobs_will_not_be_added_to_the_monito
280280
$this->assertDatabaseCount((new StackkitCloudTask())->getTable(), 0);
281281
}
282282

283+
/**
284+
* @test
285+
*/
286+
public function when_a_job_is_scheduled_it_will_be_added_as_such()
287+
{
288+
// Arrange
289+
CloudTasksApi::fake();
290+
Carbon::setTestNow(now());
291+
$tasksBefore = StackkitCloudTask::count();
292+
293+
$job = $this->dispatch((new SimpleJob())->delay(now()->addSeconds(10)));
294+
$tasksAfter = StackkitCloudTask::count();
295+
296+
// Assert
297+
$task = StackkitCloudTask::first();
298+
$this->assertSame(0, $tasksBefore);
299+
$this->assertSame(1, $tasksAfter);
300+
$this->assertDatabaseHas((new StackkitCloudTask())->getTable(), [
301+
'queue' => 'barbequeue',
302+
'status' => 'scheduled',
303+
'name' => SimpleJob::class,
304+
]);
305+
$this->assertEquals(now()->addSeconds(10)->toDateTimeString(), $task->getEvents()[0]['scheduled_at']);
306+
}
307+
283308
/**
284309
* @test
285310
*/

0 commit comments

Comments
 (0)