Modified the app to run inside a docker container

This commit is contained in:
Martin Donnelly 2022-05-12 16:33:35 +01:00
parent 0b3e254bb7
commit 7e32e3974d
15 changed files with 2485 additions and 39 deletions

21
Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM node:current-slim
ARG VERSION
ENV VERSION ${VERSION:-development}
WORKDIR /app
COPY start.sh package*.json ecosystem.config.json /app/
COPY ./app/ /app/app
COPY ./libs/ /app/libs
COPY ./data/ /app/data
RUN npm install pm2 -g && npm install
# RUN ls -lh .
RUN chmod +x /app/start.sh
ENTRYPOINT ["/app/start.sh"]

33
Makefile Normal file
View File

@ -0,0 +1,33 @@
PROJECT = lot
VERSION = $(shell git rev-parse --short HEAD)
ECR_REPO = mail.caliban.io:5000
#APP_IMAGE = 482681734622.dkr.ecr.eu-west-1.amazonaws.com/$(PROJECT):$(VERSION)
APP_IMAGE = $(ECR_REPO)/$(PROJECT):$(VERSION)
NO_CACHE = true
#build docker image
build:
docker build . -t $(APP_IMAGE) --build-arg VERSION=$(VERSION) --no-cache=$(NO_CACHE) --compress
.PHONY: build
#push docker image to registry
push: build
docker push $(APP_IMAGE)
.PHONY: push
#push docker image to registry
run: build
docker run $(APP_IMAGE)
.PHONY: run
ver:
@echo '$(VERSION)'
#echo $ERSION
.PHONY: ver
tar:
# docker build . -t $(APP_IMAGE) --build-arg VERSION=$(VERSION) --no-cache=$(NO_CACHE)
tar -C ./ -czvf ./archive.tar.gz 'package.json' 'ncas/' 'helpers/' -X *.js
.PHONY: build

31
app/predict.js Normal file
View File

@ -0,0 +1,31 @@
const loader = require('../libs/loader');
const lot = require('../libs/lot');
const lotv2 = require('../libs/lotV2');
const pusher = require('../libs/pusher');
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = 'debug';
function run(data) {
const v1 = lot.calculate(data);
const v2 = lotv2.calculate(data);
const msg = `V2: ${v2} stars: ${v1.mainstars}
V1: ${v1.mainline} stars: ${v1.mainstars}
Other: ${v1.otherLine} stars: ${v1.otherStars}
`;
pusher.push(msg);
logger.debug(msg);
// loader.save(data);
}
function go() {
logger.info('GO!');
loader.load(run);
}
(() => {
go();
})();

5
app/retriever.js Normal file
View File

@ -0,0 +1,5 @@
const { retrieveNew } = require('../libs/retriever');
(() => {
retrieveNew();
})();

File diff suppressed because one or more lines are too long

22
ecosystem.config.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"name": "Predictor",
"script": "app/predict.js",
"env": {
"NODE_ENV": "production"
},
"autorestart": false,
"instances": 1,
"cron_restart": "10 15 * * 2,5"
},
{
"name": "Retriever",
"script": "app/retriever.js",
"env": {
"NODE_ENV": "production"
},
"autorestart": false,
"instances": 1,
"cron_restart": "45 9 * * 3,6"
}
]

View File

@ -5,22 +5,21 @@
* Time: 16:35 * Time: 16:35
* *
*/ */
var Pushover = require('node-pushover'), dateFormat = require('dateformat'); const Pushover = require('node-pushover')/* , dateFormat = require('dateformat')*/;
var push = new Pushover({ const push = new Pushover({
'token': 'aqnos2j4v4pjpry3hvnxq9646eup23', 'token': 'aqnos2j4v4pjpry3hvnxq9646eup23', 'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM'
'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM'
}); });
const logger = require('log4js').getLogger();
var logger = require('log4js').getLogger();
const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev'; const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev';
const title = `mdLot ${ prefix } Alert`; const title = `mdLot ${ prefix } Alert`;
module.exports = { module.exports = {
'push' : function(contents) { 'push' : function(contents) {
var now = new Date(); const now = new Date();
var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`; // var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`;
const msg = `Updated at ${new Date(now).toLocaleString()}\n\n${contents}`;
push.send(title, msg); push.send(title, msg);
} }
}; };

View File

@ -1,30 +1,36 @@
const jsonfile = require("jsonfile"); const jsonfile = require('jsonfile');
const fetch = require("node-fetch"); const fetch = require('node-fetch');
const fecha = require("fecha"); const fecha = require('fecha');
const { scrapeResults } = require("./scraper"); const { scrapeResults } = require('./scraper');
const file = "data/data.json"; const file = 'data/data.json';
// https://www.euro-millions.com/results function lastEvent() {
const dateThen = new Date();
// Tuesday and Friday
// Sun, Mon, *Tue*, Wed, Thu, *Fri*, Sat
// [2, 3 ,4 ,1 ,2 ,3 ,1]
const daySubtractor = [2, 3, 4, 1, 2, 3, 1];
const dayNow = dateThen.getDay();
dateThen.setDate(dateThen.getDate() - daySubtractor[dayNow]);
return fecha.format(dateThen, '/DD-MM-YYYY');
}
function retrieveNew() { function retrieveNew() {
console.log("retrieveNew..."); const ystring = lastEvent();
const now = new Date().getTime();
// https://www.euro-millions.com/results/05-02-2019
const yesterday = new Date(now - 8.64e7);
const ystring = fecha.format(yesterday, "/DD-MM-YYYY");
const lotData = jsonfile.readFileSync(file); const lotData = jsonfile.readFileSync(file);
const u = `https://www.euro-millions.com/results${ystring}`; const u = `https://www.euro-millions.com/results${ystring}`;
console.log("retrieving:", u); console.log('retrieving:', u);
// https://www.euro-millions.com/results/15-02-2019
fetch(u) fetch(u)
.then((res) => res.text()) .then((res) => res.text())

View File

@ -43,3 +43,6 @@ cron.schedule("45 9 * * 3,6", () => {
}); });
logger.info("Lot V3 started..."); logger.info("Lot V3 started...");
retrieveNew();

View File

@ -5,14 +5,13 @@
* Time: 16:35 * Time: 16:35
* *
*/ */
var Pushover = require('node-pushover'), dateFormat = require('dateformat'); var Pushover = require('node-pushover')/*, dateFormat = require('dateformat')*/;
var push = new Pushover({ var push = new Pushover({
'token': 'aqnos2j4v4pjpry3hvnxq9646eup23', 'token': 'aqnos2j4v4pjpry3hvnxq9646eup23',
'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM' 'user': 'BE2vgFxdHJw91lVGMRYvZDDmVa5cCM'
}); });
const logger = require('log4js').getLogger();
var logger = require('log4js').getLogger();
const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev'; const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev';
const title = `mdLot ${ prefix } Alert`; const title = `mdLot ${ prefix } Alert`;
@ -20,7 +19,8 @@ module.exports = {
'push' : function(contents) { 'push' : function(contents) {
var now = new Date(); var now = new Date();
var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`; // var msg = `Updated at ${ dateFormat(now, 'dddd, mmmm dS, yyyy, HH:MM:ss') }\n\n${ contents }`;
var msg = `Updated at ${ now.format(('dddd, mmmm dS, yyyy, HH:MM')) }\n\n${ contents }`;
push.send(title, msg); push.send(title, msg);
} }
}; };

2323
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,9 +11,10 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"jsonfile": "^2.2.3", "eslint": "^8.15.0",
"node-cron": "^1.0.0", "jsonfile": "^6.1.0",
"tape": "^4.9.2", "node-cron": "^3.0.0",
"tape": "^5.5.3",
"tape-promise": "^4.0.0", "tape-promise": "^4.0.0",
"ultrases": "^0.1.3" "ultrases": "^0.1.3"
}, },
@ -22,12 +23,10 @@
}, },
"dependencies": { "dependencies": {
"cheerio": "^1.0.0-rc.2", "cheerio": "^1.0.0-rc.2",
"dateformat": "^2.0.0", "fecha": "^4.2.3",
"fecha": "^3.0.2", "log4js": "^6.4.6",
"jsonfile": "^2.4.0", "node-fetch": "^3.2.4",
"log4js": "^2.3.3", "node-pushover": "^1.0.0",
"node-fetch": "^2.3.0",
"node-pushover": "^0.2.2",
"sugar": "^2.0.1", "sugar": "^2.0.1",
"sugar-date": "^2.0.0" "sugar-date": "^2.0.0"
} }

4
start.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
set -ex
pm2-runtime start ecosystem.config.json --raw --env production