Last active
March 29, 2021 01:42
-
-
Save mechamogeo/96d03fc589e758390153656f9aad4dca to your computer and use it in GitHub Desktop.
A running worker to maintain process of unispotify with idle and shutdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Worker from "./worker.js"; | |
const uri = 'https://webhook.site/209308bd-1ef5-40ce-b063-cf98a3321e02' | |
const runner = new Worker("12345678", [`${uri}/player`,`${uri}/song`]).start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "uniworker", | |
"version": "0.0.1", | |
"description": "A running worker to maintain process of unispotify", | |
"main": "run.js", | |
"repository": "https://github.com/onhackme/UniWorker", | |
"author": "onhackme", | |
"license": "MIT", | |
"type": "module", | |
"private": false, | |
"scripts": { | |
"start": "node main.js" | |
}, | |
"engines": { | |
"node": ">=14" | |
}, | |
"dependencies": { | |
"date-fns": "^2.16.1", | |
"got": "^11.8.2", | |
"memory-cache": "^0.2.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { differenceInMilliseconds, differenceInSeconds } from 'date-fns' | |
import got from 'got' | |
import cache from 'memory-cache' | |
export default class Worker { | |
hook(event, uri) { | |
return { | |
event, | |
fetch: (data) => got.post(uri, { 'json': data }) | |
} | |
} | |
async sendHook(key, data) { | |
let value = await cache.get(key) | |
if(value != null) { if(JSON.stringify(value) === JSON.stringify(data)) { return} } | |
cache.put(key, data,1000 * 60 * 60) // Max 1h ~ Echolyn – “mei” (49:33) duration | |
if(this.hooks.length) { | |
if (!this.hooks.some(h => h.event === key) && !this.hooks.some(h => h.event === 'all')) { return } | |
// Save the index to make fetch | |
let indx = !this.hooks.some(h => h.event === key) ? | |
this.hooks.findIndex(h => h.event === 'all') : | |
this.hooks.findIndex(h => h.event === key); | |
this.hooks[indx].fetch({ | |
user: this.key, | |
event: key, | |
data | |
}) | |
} | |
} | |
async fn() { | |
let willBeOk = (Math.floor(Math.random() * 100) <= 42) | |
let last = new Date() | |
// Create Post to Webhook | |
let { body } = await got.get('https://swapi.dev/api/vehicles/4/', { responseType: 'json' }); | |
await this.sendHook('player', { name: body.name }) | |
await this.sendHook('song', { name: body.name }) | |
await this.sendHook('code', { name: body.name }) | |
return new Promise((resolve, reject) => { | |
if(willBeOk) resolve({ status: 200, last }) | |
else reject({ status: 503 }) | |
}) | |
} | |
async loop() { | |
const response = await this.fn().catch(e => this.status = e.status) | |
if(response?.status) this.status = response.status | |
if(response?.last) this.last = response.last | |
console.log(`Created ${differenceInSeconds(new Date(), this.created)}s ago |`, this.event, this.status, this.last.toLocaleTimeString("pt-BR")) | |
if(this.event === "idle" && this.status === 200) return this.start() | |
const difference = differenceInMilliseconds(new Date(), this.last) | |
const shouldToggleEvent = (difference >= (this.interval[this.event] * this.retry[this.event])) | |
if(this.event === "start" && shouldToggleEvent) return this.idle() | |
if(this.event === "idle" && shouldToggleEvent) return this.kill() | |
} | |
start() { | |
this.event = "start"; | |
this.handler() | |
} | |
idle() { | |
this.event = "idle" | |
this.handler() | |
} | |
handler() { | |
if(this.running) this.stop(); | |
this.running = true; | |
this.last = new Date(); | |
this.run = setInterval(this.loop.bind(this), this.interval[this.event]) | |
} | |
kill() { | |
this.event = "kill" | |
this.stop() | |
} | |
stop() { | |
this.running = false; | |
this.last = null; | |
clearInterval(this.run); | |
if(this.event === "kill") { | |
console.log("Stopped unecessary process recursion"); | |
return process.exit(0); | |
} | |
} | |
constructor(key, hooks = {}) { | |
this.key = key; | |
this.hooks = []; | |
// Webhooks | |
if(typeof hooks === "string") { this.hooks.push(this.hook('all', hooks)) } | |
if(typeof hooks === "object" && Array.isArray(hooks) && hooks.length === 1) { this.hooks = [this.hook('all', hooks[0])] } | |
if(typeof hooks === "object" && Array.isArray(hooks) && hooks.length === 2) { this.hooks = [this.hook('player', hooks[0]), this.hook('song', hooks[1])] } | |
if(typeof hooks === "object" && Array.isArray(hooks) && hooks.length === 3) { this.hooks = [this.hook('player', hooks[0]), this.hook('song', hooks[1]), this.hook('code', hooks[2])] } | |
this.run = null; | |
this.event = null; | |
this.running = false; | |
this.created = new Date().getTime(); | |
this.status = null; | |
this.last = null; | |
this.interval = { | |
start: 2000, | |
idle: 6000 | |
} | |
this.retry = { | |
start: 5, | |
idle: 10 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment