Browse Source
In this way, no need to pass all the context that methods need to work to the worker.pull/1/head
Matthew Raymer
1 year ago
3 changed files with 12 additions and 11 deletions
@ -1,27 +1,20 @@ |
|||
import { parentPort } from 'worker_threads'; |
|||
import { Message, NotificationService } from './notificationService.js' |
|||
|
|||
class WorkerThread { |
|||
private interval: number; |
|||
private notificationService: NotificationService; |
|||
private message: Message; |
|||
|
|||
constructor(interval: number) { |
|||
this.interval = interval; |
|||
|
|||
this.notificationService = new NotificationService(); |
|||
this.message = { "title": "Check the app." }; |
|||
|
|||
this.startPeriodicTask(); |
|||
} |
|||
|
|||
private startPeriodicTask(): void { |
|||
setInterval(() => { |
|||
if (parentPort) { |
|||
this.notificationService.broadcast(this.message); |
|||
parentPort.postMessage("send notifications") |
|||
} |
|||
}, this.interval); |
|||
} |
|||
} |
|||
|
|||
new WorkerThread(3600 * 24); // pole once per day
|
|||
new WorkerThread(24*3600*1000); // pole once per day
|
|||
|
Loading…
Reference in new issue