#Custom threading

1 messages · Page 1 of 1 (latest)

gleaming lake
#

Good morning friends.. I have a rather strange problem here. Even though ThreadRecord<T, V> should be available anywhere and so should its records... here are the files.

#
FIRED cb127fdd-5535-4551-b4d4-85c645099ec8
Main process 23664 running
FIRED 0f0d4d9c-ff93-400e-bf00-ec75cf34b7fb

Okey seems like the workers everytime create new instance of Record class but how. when i use singleton Pattern

// threadcord.ts

import { ThreadCallback } from ".";
import { v4 as uuidv4 } from "uuid";
require("cluster-shared-memory");

class ThreadRecord<T, V> {
  private sharedMemory: any;
  private static instance: ThreadRecord<any, any> | undefined;
  private uuid: string | undefined;
  private constructor() {}
  static getInstance() {
    if (ThreadRecord.instance) {
      console.log(ThreadRecord.instance.uuid);
      return ThreadRecord.instance;
    }
    const $this = new ThreadRecord<string, ThreadCallback>();
    $this.sharedMemory = require("cluster-shared-memory");
    $this.uuid = uuidv4();
    ThreadRecord.instance = $this;
    console.log("FIRED " + $this.uuid);
    return $this;
  }

  async addRecord(key: string, callback: ThreadCallback) {
    await this.sharedMemory.set(key, callback);
  }

  async getRecord(key: string): Promise<ThreadCallback | undefined> {
    const get = await this.sharedMemory.get(key);
    if (typeof get === "function") {
      return get;
    }
  }

  getAll(): any {
    return this.sharedMemory;
  }
}

export const memory = ThreadRecord.getInstance();