Skip to content

Commit

Permalink
debug multiple api call issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ozer550 committed Mar 7, 2025
1 parent 38d341c commit 9a817e8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import { now } from 'kolibri/utils/serverClock';
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
import { TaskStatuses, TaskTypes } from 'kolibri-common/utils/syncTaskUtils';
import useTaskPooling from '../../composables/useTaskPooling';
import { KDP_ID, oneHour, oneDay, oneWeek, twoWeeks, oneMonth } from './constants';
import { kdpNameTranslator } from './i18n';
Expand Down Expand Up @@ -192,6 +193,10 @@
BottomAppBar,
},
mixins: [commonCoreStrings],
setup () {
const { tasks } = useTaskPooling('facility_task');
return { tasks };
},
props: {
icon: {
type: String,
Expand All @@ -217,7 +222,7 @@
device: null,
now: null,
selectedItem: {},
tasks: [],
// tasks: [],
selectedDay: {},
selectedTime: {},
};
Expand Down Expand Up @@ -266,16 +271,33 @@
};
});
},
filteredTasks() {
console.log(this.tasks);
this.tasks.forEach(task => {
console.log(task);
console.log(task.extra_metadata.device_id);
console.log(this.device.id);
console.log(this.facilityId);
});
return this.tasks.filter(
task =>
(this.isKdp || task.extra_metadata.device_id === this.device.id) &&
task.facility_id === this.facilityId &&
task.type === this.taskType &&
// Only show tasks that are repeating indefinitely
task.repeat === null,
);
},
deviceName() {
return this.device && this.device.nickname && this.device.nickname.length
? this.device.nickname
: this.device.device_name;
},
currentTask() {
return this.tasks && this.tasks.length ? this.tasks[0] : null;
return this.filteredTasks.length ? this.filteredTasks[0] : null;
},
currentTaskRunning() {
return this.currentTask && this.currentTask.status === TaskStatuses.RUNNING;
return this.currentTask?.status === TaskStatuses.RUNNING;
},
timeRequired() {
return this.selectedItem.value > oneHour;
Expand Down Expand Up @@ -402,56 +424,46 @@
goBack() {
this.$router.push(this.goBackRoute);
},
pollFetchSyncTasks() {
this.pollInterval = setInterval(() => {
this.fetchSyncTasks();
}, 10000);
},
// pollFetchSyncTasks() {
// this.pollInterval = setInterval(() => {
// this.fetchSyncTasks();
// }, 10000);
// },
fetchSyncTasks() {
TaskResource.list({ queue: 'facility_task' }).then(tasks => {
this.tasks = tasks.filter(
task =>
(this.isKdp || task.extra_metadata.device_id === this.device.id) &&
task.facility_id === this.facilityId &&
task.type === this.taskType &&
// Only show tasks that are repeating indefinitely
task.repeat === null,
);
this.$nextTick(() => {
if (this.currentTask) {
const enqueueAt = new Date(Date.parse(this.currentTask.scheduled_datetime));
const day = enqueueAt.getDay();
const hours = enqueueAt.getHours();
const minutes = enqueueAt.getMinutes();
this.selectedItem =
this.selectArray.find(item => item.value === this.currentTask.repeat_interval) ||
{};
this.selectedDay = this.getDays.find(item => item.value === day) || {};
for (const time of this.SyncTime) {
// Because there can be some drift in the task scheduling process,
// we round the 'scheduled' time to the nearest 30 minutes
if (
time.minutes === 0 &&
((time.hours === hours && minutes < 15) ||
(time.hours === hours + 1 && minutes >= 45))
) {
this.selectedTime = time;
break;
}
if (time.minutes === 30 && time.hours === hours && minutes >= 15 && minutes < 45) {
this.selectedTime = time;
break;
}
this.$nextTick(() => {
if (this.currentTask) {
const enqueueAt = new Date(Date.parse(this.currentTask.scheduled_datetime));
const day = enqueueAt.getDay();
const hours = enqueueAt.getHours();
const minutes = enqueueAt.getMinutes();
this.selectedItem =
this.selectArray.find(item => item.value === this.currentTask.repeat_interval) ||
{};
this.selectedDay = this.getDays.find(item => item.value === day) || {};
for (const time of this.SyncTime) {
// Because there can be some drift in the task scheduling process,
// we round the 'scheduled' time to the nearest 30 minutes
if (
time.minutes === 0 &&
((time.hours === hours && minutes < 15) ||
(time.hours === hours + 1 && minutes >= 45))
) {
this.selectedTime = time;
break;
}
this.retryFlag = Boolean(this.currentTask.retry_interval);
if (this.currentTaskRunning) {
this.pollFetchSyncTasks();
} else {
clearInterval(this.pollInterval);
this.pollInterval = null;
if (time.minutes === 30 && time.hours === hours && minutes >= 15 && minutes < 45) {
this.selectedTime = time;
break;
}
}
});
this.retryFlag = Boolean(this.currentTask.retry_interval);
// if (this.currentTaskRunning) {
// this.pollFetchSyncTasks();
// } else {
// clearInterval(this.pollInterval);
// this.pollInterval = null;
// }
}
});
},
fetchDevice() {
Expand Down
11 changes: 8 additions & 3 deletions packages/kolibri-common/composables/useTaskPooling.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { ref, onMounted, onUnmounted } from 'vue';
import { useTimeoutPoll } from '@vueuse/core';
import TaskResource from 'kolibri/apiResources/TaskResource';

const taskPollers = new Map();

export function useTaskPooling(queueName, fetchTaskFunction, interval = 5000) {
export default function useTaskPooling(queueName) {
if (!taskPollers.has(queueName)) {
const consumers = ref(0);
const tasks = ref([]);

const { pause, resume, isActive } = useTimeoutPoll(
async () => {
try {
tasks.value = await fetchTaskFunction();
tasks.value = await TaskResource.list({ queue: queueName });
} catch (e) {
console.error(e);
}
},
interval,
5000,
{ immediate: true },
);

Expand All @@ -29,6 +30,10 @@ export function useTaskPooling(queueName, fetchTaskFunction, interval = 5000) {
const poller = taskPollers.get(queueName);
poller.consumers.value++;

console.log("Current number of consuemrs for the queue ", queueName, poller.consumers.value);
//log the current value of taskPollers map
console.log("Current value of taskPollers map", taskPollers);

onMounted(() => {
if (!poller.isActive) {
poller.resume();
Expand Down

0 comments on commit 9a817e8

Please sign in to comment.