Skip to content

Commit ee643c2

Browse files
committed
feat: add cron with api response
1 parent 2fc709c commit ee643c2

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM node:current-slim
1+
FROM arm64v8/node:18.14-buster-slim
22

3-
RUN apt-get update -y && apt-get upgrade -y
3+
RUN apt-get update && apt-get upgrade -y
44

55

66
WORKDIR /SIMPLE-NODE-API
@@ -11,6 +11,8 @@ RUN npm install
1111
COPY ./routes.js routes.js
1212
COPY ./server.js server.js
1313
COPY ./todos.js todos.js
14+
COPY ./cron.js cron.js
15+
1416

1517
EXPOSE 1337
1618
ENTRYPOINT [ "node", "server" ]

cron.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
var CronJob = require('cron').CronJob;
2+
const { default: axios } = require('axios');
23
var express = require('express');
34
const http = require(`http`);
45
const app = express();
6+
app.get(`/health`, (req, res) => res.status(200).json(`OK`));
7+
const server = http.createServer(app);
8+
server.listen(
9+
1337,
10+
() => console.log(`-- Backend Service (1337) --`)
11+
);
12+
13+
const job = new CronJob('0 */1 * * * *', async function () {
14+
try {
15+
let response = await axios.get(process.env.CLIENT_URL)//configurar o CLIENT_URL=https://something/health
16+
console.log('Resposta so servidor: OK', JSON.stringify(response.data))
17+
} catch (error) {
18+
console.log('ocorreu um erro:', JSON.stringify(error))
19+
}
520

6-
const job = new CronJob('0 */1 * * * *', function () {
7-
app.get(`/health`, (req, res) => res.status(200).json(`OK`));
8-
const server = http.createServer(app);
9-
server.listen(
10-
1337,
11-
() => console.log(`-- Backend Service (1337) --`)
12-
);
1321
});
1422
console.log('Resposta do servidor a cada minuto');
1523
job.start();

package-lock.json

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"axios": "^1.3.4",
1314
"cron": "^2.3.0",
1415
"express": "^4.16.4"
1516
}

0 commit comments

Comments
 (0)