Added licence notice to many files, other minor type fixes

This commit is contained in:
mrfry 2022-03-22 11:19:32 +01:00
parent d9175c1fc2
commit 75d93af246
22 changed files with 483 additions and 107 deletions

View file

@ -1,10 +1,30 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import { Worker } from 'worker_threads'
import { v4 as uuidv4 } from 'uuid'
import { EventEmitter } from 'events'
import os from 'os'
import logger from './logger'
import { Result } from './actions'
import { Result, Edits } from './actions'
import type { Question, QuestionDb, QuestionData } from '../types/basicTypes'
import type { WorkerResult } from './classes'
@ -27,7 +47,7 @@ interface TaskObject {
searchTillMatchPercent?: number
[key: string]: any
}
| { dbIndex: number; edits: any }
| { dbIndex: number; edits: Edits }
| QuestionDb
| Result
}
@ -76,7 +96,7 @@ function handleWorkerError(worker: WorkerObj, err: Error) {
}
// TODO: accuire all workers here, and handle errors so they can be removed if threads exit
export function msgAllWorker(data: TaskObject): Promise<any> {
export function msgAllWorker(data: TaskObject): Promise<WorkerResult[]> {
logger.DebugLog('MSGING ALL WORKER', 'job', 1)
return new Promise((resolve) => {
const promises: Promise<WorkerResult>[] = []
@ -93,7 +113,7 @@ export function msgAllWorker(data: TaskObject): Promise<any> {
export function doALongTask(
obj: TaskObject,
targetWorkerIndex?: number
): Promise<any> {
): Promise<WorkerResult> {
if (Object.keys(pendingJobs).length > alertOnPendingCount) {
logger.Log(
`More than ${alertOnPendingCount} callers waiting for free resource! (${
@ -120,7 +140,7 @@ export function doALongTask(
})
}
export function initWorkerPool(initData: any): Array<WorkerObj> {
export function initWorkerPool(initData: Array<QuestionDb>): Array<WorkerObj> {
if (workers) {
logger.Log('WORKERS ALREADY EXISTS', logger.GetColor('redbg'))
return null
@ -150,7 +170,6 @@ export function initWorkerPool(initData: any): Array<WorkerObj> {
function processJob() {
if (Object.keys(pendingJobs).length > 0) {
// FIXME: FIFO OR ANYTHING ELSE (JOB PROCESSING ORDER)
const keys = Object.keys(pendingJobs)
let jobKey: string, freeWorker: WorkerObj
let i = 0
@ -239,11 +258,18 @@ function doSomething(currWorker: WorkerObj, obj: TaskObject) {
})
}
const workerTs = (file: string, wkOpts: any) => {
wkOpts.eval = true
if (!wkOpts.workerData) {
wkOpts.workerData = {}
const workerTs = (
file: string,
wkOpts: {
workerData: {
workerIndex: number
initData: QuestionDb[]
__filename?: string
}
eval?: boolean
}
) => {
wkOpts.eval = true
wkOpts.workerData.__filename = file
return new Worker(
`