feat: working jobs page with sse

This commit is contained in:
2024-03-08 02:08:01 +01:00
parent 28b16aee7a
commit e87a9bef18
26 changed files with 1521 additions and 535 deletions
+81
View File
@@ -0,0 +1,81 @@
import { readFile, writeFile } from 'fs/promises'
import { Mutex } from 'async-mutex'
import { exists } from './utils'
import yaml from 'js-yaml'
export function createJsonDatabase<T>(filename: string, initialValue: T) {
const mutex = new Mutex()
function ensureExists() {
return mutex.runExclusive(async () => {
if (!(await exists(filename))) {
await writeFile(filename, JSON.stringify(initialValue, null, 2))
}
})
}
return {
mutex,
async load(): Promise<T> {
await ensureExists()
return await mutex.runExclusive(async () => {
const data = await readFile(filename, 'utf8')
return JSON.parse(data) as T
})
},
async update<R>(fn: (value: T) => Promise<R>) {
await ensureExists()
return await mutex.runExclusive(async () => {
const data = await readFile(filename, 'utf8')
const value = JSON.parse(data) as T
const result = await fn(value)
await writeFile(filename, JSON.stringify(value, null, 2))
return result
})
},
}
}
export function createYamlDatabase<T>(filename: string, initialValue: T) {
const mutex = new Mutex()
function ensureExists() {
return mutex.runExclusive(async () => {
if (!(await exists(filename))) {
await writeFile(filename, yaml.dump(initialValue))
}
})
}
return {
async load(): Promise<T> {
await ensureExists()
return await mutex.runExclusive(async () => {
const data = await readFile(filename, 'utf8')
return yaml.load(data) as T
})
},
async update<R>(fn: (value: T) => Promise<R>) {
await ensureExists()
return await mutex.runExclusive(async () => {
const data = await readFile(filename, 'utf8')
const value = yaml.load(data) as T
const result = await fn(value)
await writeFile(filename, yaml.dump(value))
return result
})
},
}
}
+38
View File
@@ -0,0 +1,38 @@
import { access } from 'fs/promises'
export const normalizeURL = (url: string) => {
if (!url.startsWith('/') && !url.startsWith('https://') && !url.startsWith('http://')) {
url = `https://${url}`
}
return url
}
export const clsx = (...args: any[]) =>
args
.filter(Boolean)
.flatMap(s => (typeof s === 'string' ? s.split(' ') : [s]))
.join(' ')
/**
* Modern alternative to fs.existsSync
*/
export async function exists(path: string) {
try {
await access(path)
return true
} catch (err) {
// @ts-ignore
if (err.code === 'ENOENT') {
return false
} else {
throw err
}
}
}
export function sleep(timeout: number) {
return new Promise(resolve => {
setTimeout(resolve, timeout)
})
}