Skip to content

Commit 4a0ea82

Browse files
committed
feat: add support of the websocket
1 parent a18522e commit 4a0ea82

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

custom/NavbarJobs.vue

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="loader "></div>
55
<div class="absolute -bottom-1 -right-1 rounded-full bg-lightPrimary w-4 h-4 text-xs flex items-center justify-center text-white"> {{ jobsCount }}</div>
66
</div>
7-
<div class="flex items-center justify-center" v-else-if="jobsCount > 0">
7+
<div class="flex items-center justify-center" v-else-if="jobs.length > 0">
88
<Tooltip>
99
<IconCheckCircleOutline class="w-8 h-8 text-green-500" />
1010
<template #tooltip>
@@ -24,13 +24,14 @@
2424

2525
<script setup lang="ts">
2626
import type { AdminUser } from 'adminforth';
27-
import { onMounted, ref, computed } from 'vue';
27+
import { onMounted, onUnmounted, ref, computed } from 'vue';
2828
import { IconCheckCircleOutline } from '@iconify-prerendered/vue-flowbite';
2929
import { Tooltip } from '@/afcl';
3030
import { useI18n } from 'vue-i18n';
3131
import JobsList from './JobsList.vue';
3232
import type { IJob } from './utils';
3333
import { callAdminForthApi } from '@/utils';
34+
import websocket from '@/websocket';
3435
3536
const { t } = useI18n();
3637
@@ -49,10 +50,33 @@
4950
})
5051
5152
const jobsCount = computed(() => {
52-
return jobs.value.length;
53+
return jobs.value.filter(job => job.status === 'IN_PROGRESS').length;
5354
})
5455
56+
57+
5558
onMounted(async () => {
59+
websocket.subscribe('/background-jobs', (data) => {
60+
const jobIndex = jobs.value.findIndex(job => job.id === data.jobId);
61+
if (jobIndex !== -1) {
62+
if (data.status) {
63+
jobs.value[jobIndex].status = data.status;
64+
}
65+
if (data.progress !== undefined) {
66+
jobs.value[jobIndex].progress = data.progress;
67+
}
68+
} else {
69+
jobs.value.push({
70+
id: data.jobId,
71+
name: data.name || 'Unknown Job',
72+
status: data.status || 'IN_PROGRESS',
73+
progress: data.progress || 0,
74+
createdAt: data.createdAt || new Date().toISOString(),
75+
});
76+
}
77+
});
78+
79+
5680
try {
5781
const res = await callAdminForthApi({
5882
path: `/plugin/${props.meta.pluginInstanceId}/get-list-of-jobs`,
@@ -64,6 +88,11 @@
6488
}
6589
});
6690
91+
92+
onUnmounted(() => {
93+
websocket.unsubscribe('/background-jobs');
94+
});
95+
6796
</script>
6897

6998

index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default class extends AdminForthPlugin {
9999
pathToComponentToRenderState?: string
100100
) {
101101

102+
//create a record for the job in the database with status in progress
102103
const objectToSave = {
103104
[this.options.nameField]: jobName,
104105
[this.options.startedByField]: adminUser.pk,
@@ -113,9 +114,19 @@ export default class extends AdminForthPlugin {
113114
createdRecord = creationResult.createdRecord;
114115
} else {
115116
throw new Error(`Failed to create a record for the job. Error: ${creationResult.error}`);
116-
}
117-
117+
}
118118
const jobId = createdRecord[this.getResourcePk()];
119+
120+
this.adminforth.websocket.publish('/background-jobs', {
121+
jobId,
122+
status: 'IN_PROGRESS',
123+
name: jobName,
124+
progress: 0,
125+
createdAt: createdRecord[this.options.createdAtField],
126+
});
127+
128+
129+
//create a level db instance for the job with name as jobId
119130
const jobLevelDb = new Level(`${this.options.levelDbPath || './background-jobs-dbs/'}job_${jobId}`, { valueEncoding: 'json' });
120131

121132

@@ -151,6 +162,7 @@ export default class extends AdminForthPlugin {
151162
await this.adminforth.resource(this.getResourceId()).update(jobId, {
152163
[this.options.progressField]: progress,
153164
})
165+
this.adminforth.websocket.publish('/background-jobs', { jobId, progress });
154166
}
155167
}
156168

@@ -164,6 +176,7 @@ export default class extends AdminForthPlugin {
164176
await this.adminforth.resource(this.getResourceId()).update(jobId, {
165177
[this.options.statusField]: 'DONE',
166178
})
179+
this.adminforth.websocket.publish('/background-jobs', { jobId, status: 'DONE' });
167180

168181
}
169182

0 commit comments

Comments
 (0)