Skip to content

Commit

Permalink
Able to stop workers
Browse files Browse the repository at this point in the history
  • Loading branch information
garyghayrat committed Jan 24, 2024
1 parent 9bd4900 commit c3afeb3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions frontend/src/pages/AccountReceive.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
</div>
<div v-else class="text-center text-italic">{{ $t('Receive.fetching-latest') }}</div>
</div>
<base-button @click="terminateWorkers()" class="text-center" label="stop workers" />
</div>
</q-page>
</template>
Expand Down Expand Up @@ -130,6 +131,7 @@ function useScan() {
const scanStatus = ref<ScanStatus>('waiting');
const scanPercentage = ref<number>(0);
const userAnnouncements = ref<UserAnnouncement[]>([]);
const workers: Worker[] = [];
// Start and end blocks for advanced mode settings
const { advancedMode, startBlock, endBlock, setScanBlocks, setScanPrivateKey, scanPrivateKey, resetScanSettings } =
Expand Down Expand Up @@ -199,6 +201,10 @@ function useScan() {
await scan();
}
function terminateWorkers() {
workers.forEach((worker) => worker.terminate());
}
async function scan() {
if (!umbra.value) throw new Error('No umbra instance found. Please make sure you are on a supported network');
scanStatus.value = 'fetching latest';
Expand Down Expand Up @@ -228,6 +234,7 @@ function useScan() {
spendingPublicKey,
viewingPrivateKey,
announcements,
workers,
(percent) => {
scanPercentage.value = Math.floor(percent);
},
Expand Down Expand Up @@ -296,6 +303,9 @@ function useScan() {
}
// Wait for the first batch of web workers to finish scanning before creating new workers
await firstScanPromise;
terminateWorkers();
// Clear out existing workers
workers.length = 0;
scanStatus.value = 'scanning';
await filterUserAnnouncementsAsync(spendingPubKey, viewingPrivKey, announcementsQueue);
scanStatus.value = 'complete';
Expand Down Expand Up @@ -337,6 +347,7 @@ function useScan() {
startBlockLocal,
userAddress,
userAnnouncements,
terminateWorkers,
};
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const filterUserAnnouncements = (
spendingPublicKey: string,
viewingPrivateKey: string,
announcements: AnnouncementDetail[],
workers: Worker[],
progress: (percentage: number) => void,
completion: (userAnnouncements: UserAnnouncement[]) => void
) => {
Expand Down Expand Up @@ -58,7 +59,6 @@ export const filterUserAnnouncements = (
}

// assign tasks to workers
const workers: Worker[] = [];
const progressRecorder: number[] = [];
let progressSum = 0;
// Here we will initialize `nCores` workers by constructing `Worker()` imported from worker script `filter.worker.ts`
Expand Down

0 comments on commit c3afeb3

Please sign in to comment.