fix(tracker): potential fix for writer busy status

This commit is contained in:
sylenien 2022-06-15 10:59:30 +02:00
parent 4a5093addf
commit 869a25169f
3 changed files with 46 additions and 7 deletions

View file

@ -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 }>
type Log = {
type: "log"
log: WorkerActivityLogStatus
}
export enum WorkerActivityLogStatus {
Off,
Console,
Error,
}
export type WorkerMessageData = null | "stop" | Start | Auth | Array<{ _id: number }> | Log

View file

@ -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?

View file

@ -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<WorkerMessageData>) => {
send() // TODO: sendAll?
return
}
if (data === "stop") {
send()
reset()
@ -59,8 +67,22 @@ self.onmessage = ({ data }: MessageEvent<WorkerMessageData>) => {
}
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<WorkerMessageData>) => {
}
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<WorkerMessageData>) => {
if (sendIntervalID === null) {
sendIntervalID = setInterval(send, AUTO_SEND_INTERVAL)
}
return
return workerStatus = WorkerStatus.Active
}
if (data.type === "auth") {