Skip to content

Commit d1da3b6

Browse files
committed
Dockerize strava bot
1 parent d4b149a commit d1da3b6

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.env
3+
database.json
4+
old.database.json
5+
tokens.json

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
node_modules
22
.env
3-
access_token
4-
refresh_token
5-
database
63
database.json
4+
old.database.json
75
tokens.json

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM node
2+
3+
RUN apt-get update && apt-get upgrade -y && apt-get install cron -y
4+
5+
WORKDIR /app
6+
COPY package-lock.json package.json ./
7+
RUN npm install
8+
9+
COPY . .
10+
RUN chmod 0644 crontab
11+
RUN crontab crontab
12+
RUN touch /var/log/cron.log
13+
14+
RUN echo "{}" > src/database.json
15+
RUN echo "{}" > src/old.database.json
16+
17+
ENV client_id=
18+
ENV client_secret=
19+
ENV refresh_token=
20+
21+
ENV club_id=
22+
ENV hook_token=
23+
ENV title="Sammanfattning sedan dag 1 LV1"
24+
25+
CMD /usr/bin/printenv > /app/.env && cron -f

compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
bot:
3+
build:
4+
context: .
5+
environment:
6+
hook_token: $hook_token
7+
client_secret: $client_secret
8+
refresh_token: $refresh_token
9+
club_id: $club_id
10+
title: $title

crontab

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Edit this file to introduce tasks to be run by cron.
2+
#
3+
# Each task to run has to be defined through a single line
4+
# indicating with different fields when the task will be run
5+
# and what command to run for the task
6+
#
7+
# To define the time you can provide concrete values for
8+
# minute (m), hour (h), day of month (dom), month (mon),
9+
# and day of week (dow) or use '*' in these fields (for 'any').
10+
#
11+
# Notice that tasks will be started based on the cron's system
12+
# daemon's notion of time and timezones.
13+
#
14+
# Output of the crontab jobs (including errors) is sent through
15+
# email to the user the crontab file belongs to (unless redirected).
16+
#
17+
# For example, you can run a backup of all your user accounts
18+
# at 5 a.m every week with:
19+
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
20+
#
21+
# For more information see the manual pages of crontab(5) and cron(8)
22+
#
23+
# m h dom mon dow command
24+
0 12 * * 1 /usr/local/bin/node /app/src/index.js
25+
30 */6 * * * /usr/local/bin/node /app/src/refresh_tokens.js

src/index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,14 @@ const getTokens = () => {
1212
const tokens_path = join(__dirname, "tokens.json");
1313

1414
if (!fs.existsSync(tokens_path)) {
15-
console.log("Token file not found:", tokens_path);
16-
exit(1);
17-
}
18-
19-
try {
20-
return JSON.parse(fs.readFileSync(tokens_path));
21-
} catch {
22-
fs.unlinkSync(tokens_path);
2315
refresh_token()
2416
.then(() => console.log("Token refreshed!"))
2517
.catch(error => {
2618
console.log(error);
2719
throw "Unable to refresh token!";
2820
});
2921
}
30-
return {};
22+
return JSON.parse(fs.readFileSync(tokens_path));
3123
};
3224

3325
const tokens = getTokens();

0 commit comments

Comments
 (0)