diff --git a/tracker/tracker/src/common/webworker.ts b/tracker/tracker/src/common/webworker.ts index 34ac7f582..3f2cc50d9 100644 --- a/tracker/tracker/src/common/webworker.ts +++ b/tracker/tracker/src/common/webworker.ts @@ -1,6 +1,7 @@ export interface Options { connAttemptCount?: number connAttemptGap?: number + workerLog?: WorkerActivityLogStatus } type Start = { @@ -16,4 +17,16 @@ type Auth = { beaconSizeLimit?: number } -export type WorkerMessageData = null | "stop" | Start | Auth | Array<{ _id: number }> \ No newline at end of file +type Log = { + type: "log" + log: WorkerActivityLogStatus +} + +export enum WorkerActivityLogStatus { + Off, + Console, + Error, +} + + +export type WorkerMessageData = null | "stop" | Start | Auth | Array<{ _id: number }> | Log diff --git a/tracker/tracker/src/main/app/index.ts b/tracker/tracker/src/main/app/index.ts index 4f5487bfa..ed3a025a7 100644 --- a/tracker/tracker/src/main/app/index.ts +++ b/tracker/tracker/src/main/app/index.ts @@ -14,7 +14,7 @@ import type { Options as ObserverOptions } from "./observer/top_observer.js"; import type { Options as SanitizerOptions } from "./sanitizer.js"; import type { Options as LoggerOptions } from "./logger.js" import type { Options as WebworkerOptions, WorkerMessageData } from "../../common/webworker.js"; - +import { WorkerActivityLogStatus } from "../../common/webworker.js"; // TODO: Unify and clearly describe options logic export interface StartOptions { @@ -120,6 +120,7 @@ export default class App { __debug_report_edp: null, localStorage: window.localStorage, sessionStorage: window.sessionStorage, + workerLog: WorkerActivityLogStatus.Off, }, options, ); @@ -351,6 +352,7 @@ export default class App { timestamp: startInfo.timestamp, connAttemptCount: this.options.connAttemptCount, connAttemptGap: this.options.connAttemptGap, + workerLog: this.options.workerLog, } this.worker.postMessage(startWorkerMsg) // brings delay of 10th ms? diff --git a/tracker/tracker/src/webworker/index.ts b/tracker/tracker/src/webworker/index.ts index 3e9341a2d..5eb31320d 100644 --- a/tracker/tracker/src/webworker/index.ts +++ b/tracker/tracker/src/webworker/index.ts @@ -1,5 +1,5 @@ import type Message from "../common/messages.js"; -import type { WorkerMessageData } from "../common/webworker.js"; +import { WorkerMessageData, WorkerActivityLogStatus } from "../common/webworker.js"; import { classes, @@ -9,12 +9,19 @@ import { import QueueSender from "./QueueSender.js"; import BatchWriter from "./BatchWriter.js"; - +enum WorkerStatus { + NotActive, + Starting, + Stopping, + Active +} const AUTO_SEND_INTERVAL = 10 * 1000 let sender: QueueSender | null = null let writer: BatchWriter | null = null +let workerStatus: WorkerStatus = WorkerStatus.NotActive; +let workerLogStatus: WorkerActivityLogStatus = WorkerActivityLogStatus.Off; function send(): void { if (!writer) { @@ -25,6 +32,7 @@ function send(): void { function reset() { + workerStatus = WorkerStatus.Stopping if (sendIntervalID !== null) { clearInterval(sendIntervalID); sendIntervalID = null; @@ -33,6 +41,7 @@ function reset() { writer.clean() writer = null } + workerStatus = WorkerStatus.NotActive } function resetCleanQueue() { @@ -51,7 +60,6 @@ self.onmessage = ({ data }: MessageEvent) => { send() // TODO: sendAll? return } - if (data === "stop") { send() reset() @@ -59,8 +67,22 @@ self.onmessage = ({ data }: MessageEvent) => { } if (Array.isArray(data)) { + if (workerStatus !== WorkerStatus.Active) { + if (workerLogStatus !== WorkerActivityLogStatus.Off) { + const msg = 'WebWorker: trying to send data without writer' + switch (workerLogStatus) { + case WorkerActivityLogStatus.Console: + return console.error(msg, 'STATUS:', workerStatus, data) + case WorkerActivityLogStatus.Error: + throw new Error(`${msg} ----- STATUS: ${workerStatus} --- ${JSON.stringify(data)}`); + default: + return; + } + } + return; + } if (!writer) { - throw new Error("WebWorker: writer not initialised.") + throw new Error("WebWorker: writer not initialised. Service Should be Started.") } const w = writer // Message[] @@ -80,6 +102,8 @@ self.onmessage = ({ data }: MessageEvent) => { } if (data.type === 'start') { + workerLogStatus = data.workerLog || WorkerActivityLogStatus.Off + workerStatus = WorkerStatus.Starting sender = new QueueSender( data.ingestPoint, () => { // onUnauthorised @@ -101,7 +125,7 @@ self.onmessage = ({ data }: MessageEvent) => { if (sendIntervalID === null) { sendIntervalID = setInterval(send, AUTO_SEND_INTERVAL) } - return + return workerStatus = WorkerStatus.Active } if (data.type === "auth") {