Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
feat(data-mechanism): wip cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
ItaloSa committed Jul 18, 2021
1 parent fab9f78 commit f8b9643
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"author": "Italo Sousa",
"license": "ISC",
"dependencies": {
"@types/cron": "^1.7.3",
"cors": "^2.8.5",
"cron": "^1.8.2",
"discord.js": "^12.5.3",
"dotenv": "^10.0.0",
"express": "^4.17.1",
Expand Down
40 changes: 40 additions & 0 deletions src/core/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable implicit-arrow-linebreak */
/* eslint-disable class-methods-use-this */
import { CronJob } from 'cron';
import AppQueue from './queue';

export default class AppCron {
cron: CronJob;

queue: AppQueue;

constructor(queueInstance: AppQueue) {
this.cron = new CronJob(
'*/1 * * * *',
this.job(),
null,
true,
'America/Sao_Paulo',
);
this.queue = queueInstance;
}

setup() {
this.cron.start();
}

job() {
return () => {
console.log('[Cron] slow job started');
this.queue.enqueue(
() =>
new Promise((resolve) => {
setTimeout(() => {
console.log('[Cron] slow job finished');
resolve('ok');
}, 3000);
}),
);
};
}
}
37 changes: 26 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
/* eslint-disable implicit-arrow-linebreak */
import dotenv from 'dotenv';
import express from 'express';

import { DiscordBot } from './discord';
import Controller from './core/controller';
// import { DiscordBot } from './discord';
// import Controller from './core/controller';
// import { parseFeed } from './feed';
// import { aaa } from './test';
import AppQueue from './core/queue';
import AppCron from './core/cron';

dotenv.config();

const app = express();
const PORT = 8000;
let queueInstance: AppQueue;
let cronInstance: AppCron;

const setupDiscord = () => {
const controller = new Controller();
const discord = new DiscordBot(controller);
discord.start();
};
// const setupDiscord = () => {
// const controller = new Controller();
// const discord = new DiscordBot(controller);
// discord.start();
// };

app.use(express.json());

app.get('/', (req, res) => res.send('Express + TypeScript Server'));

app.get('/test', async (req, res) => {
// const ress = await parseFeed();
// res.json(ress);
// aaa();
queueInstance.enqueue(
() =>
new Promise((resolve) => {
setTimeout(() => {
console.log('slow job finished');
resolve('ok');
}, 3000);
}),
);
res.json({});
});

app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${PORT}`);
setupDiscord();
// setupDiscord();
queueInstance = new AppQueue();
queueInstance.start();
cronInstance = new AppCron(queueInstance);
cronInstance.setup();
});

0 comments on commit f8b9643

Please sign in to comment.