Skip to content

feat: add docker support #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
npm-debug.log
.dockerignore
.git
Dockerfile
.logs
.vscode
src/logs/*
.history
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.local_chromium
node_modules
node_modules
dist
.history
src/logs/*.log
package-lock.json
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM node:16-bullseye-slim

ENV DEBIAN_FRONTEND noninteractive
ENV TZ Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 配置环境变量,跳过 npm 安装时候的 chromium 的下载
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
# 指定浏览器的路径
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

# 替换几个镜像源,以及安装 pm2,如果你不想使用 pm2 来运行,可以删掉
RUN sed -i 's#http://deb.debian.org#http://mirrors.aliyun.com#g;s#http://security.debian.org#http://mirrors.aliyun.com#g' /etc/apt/sources.list \
&& npm c set registry="https://registry.npmmirror.com" \
&& echo "puppeteer_download_host=https://registry.npmmirror.com/-/binary" >> ~/.npmrc \
&& npm install pm2 -g

# 安装必要的库
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y wget gnupg \
fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
libgtk2.0-0 libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 libgbm1 libasound2 && \
apt-get install -y chromium && \
apt-get clean

# debug 用
# RUN apt-get update \
# && apt-get -y install procps \
# && apt-get install -y vim

WORKDIR /data/srv/

COPY . ./

RUN npm install && npm run build

CMD ["pm2-runtime", "ecosystem.config.js"]
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

### 安装与运行 Install and Run

#### 手动安装 Manual installation

1. 安装依赖

```
Expand Down Expand Up @@ -325,6 +327,26 @@ pnpm start

- 开启 `PushPlus` 推送,保存微信公众发送消息里的二维码,扫码登录

#### Docker

```
docker run --name=xuexiqiangguo -d \
-e SCHEDULE_CONFIG='{"nick":"nick","token":"xxxxxxxx","cron":"0 0 12 * * ?","taskConfig":[true,true,true],"paperExitAfterWrong":false,"refreshCookieInterval":[60,120]}' \
-e PUSH_PLUS_ENABLED=true \
-e PUSH_PLUS_NICK=nick \
-e PUSH_PLUS_SERVER_NAME=server \
-e PUSH_PLUS_ADMIN_TOKEN=xxxxxx \
helios74/ilovestudysomuch
```
参数说明:
SCHEDULE_CONFIG: `src/config/schedule.ts` 中的任务配置。注意是单个的序列化后的配置
PUSH_PLUS_ENABLED: 是否启用 pushplus
PUSH_PLUS_NICK: pushplus 的配置,nick
PUSH_PLUS_SERVER_NAME: pushplus 的配置,服务器名称
PUSH_PLUS_ADMIN_TOKEN: pushplus 的配置,管理员 token

请注意,这个镜像只支持了配置 pushplus ,如果你需要使用其他的方式,请参照 `Dockerfile` 自行构建。

### 配置 Configuration

- Puppeteer 配置 `src/config/pup.ts` ([官方文档配置](https://pptr.dev/api/puppeteer.launchoptions 'Puppeteer 使用和配置'))
Expand Down
4 changes: 4 additions & 0 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ const startScheduleJobs = () => {
};

(() => {
if (SCHEDULE_CONFIG.length === 0) {
console.warn('当前没有配置的任务信息,中断执行。');
return;
}
// 开始定时任务
startScheduleJobs();
// 执行清除日志任务
Expand Down
14 changes: 14 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
apps: [
{
name: 'xuexiqiangguo',
script: './dist/bin/index.js',
cwd: './',
watch: false,
instances: 1,
autorestart: true,
max_memory_restart: '1024M',
env: { NODE_ENV: 'production' },
}
],
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"start": "ts-node bin/index.ts",
"dev": "nodemon bin/index.ts",
"build": "./node_modules/typescript/bin/tsc",
"test": "vitest"
},
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions src/config/pup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const PUP_CONFIG: pup.PuppeteerLaunchOptions = {
// dumpio: true,
/**
* @description 可执行文件的路径
* @example 'google-chrome' for linux, '.local_chromium/chrome.exe' from win
* @example 'google-chrome' for linux, '.local_chromium/chrome.exe' from win, '/usr/bin/chromium' for docker
*/
executablePath: '.local_chromium/chrome.exe',
executablePath: '/usr/bin/chromium',
/**
* @description 忽略默认配置
*/
Expand Down
8 changes: 4 additions & 4 deletions src/config/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const PUSH_CONFIG = {
* @description 启用推送
* @example true 启用推送 false 禁用推送
*/
enabled: false,
enabled: process.env.PUSH_PLUS_ENABLED === 'true',
/**
* @description 发送服务消息昵称
*/
nick: '管理员',
nick: process.env.PUSH_PLUS_NICK || '管理员',
/**
* @description 发送服务消息来源
*/
from: 'tech-study-node',
from: process.env.PUSH_PLUS_SERVER_NAME || 'tech-study-node',
/**
* @description 管理员的token
*/
token: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
token: process.env.PUSH_PLUS_ADMIN_TOKEN || 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
};
export default PUSH_CONFIG;
37 changes: 28 additions & 9 deletions src/config/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { Log } from '../utils/logs';

const scheduleConfigLogger = new Log();

scheduleConfigLogger.start();

/**
* @description 定时任务
*/
Expand Down Expand Up @@ -27,15 +33,28 @@ export type Schedule = {
refreshCookieInterval: [number, number];
};

const myScheduleConfig: Schedule[] = [];

const envScheduleConfig = process.env.SCHEDULE_CONFIG;

scheduleConfigLogger.info(`GET config: ${process.env.SCHEDULE_CONFIG}`);

if (typeof envScheduleConfig === 'string') {
try {
// 这里不保证参数的正确性,如果是错误参数则后面流程中进行校验
const configData = JSON.parse(envScheduleConfig);
myScheduleConfig.push(configData);
scheduleConfigLogger.info(`当前配置:${Object.keys(configData).map(configKey => `[${configKey}: ${configData[configKey]}]`).join('\t')}`);
} catch (error) {
scheduleConfigLogger.fail('任务参数可能不是有效的 JSON 字符串,请检查 ENV 参数。');
}
} else {
scheduleConfigLogger.fail('任务参数不合法,请检查 ENV 参数。');
}

scheduleConfigLogger.finish();

/**
* @description 定时任务配置
*/
export const SCHEDULE_CONFIG: Schedule[] = [
{
nick: 'xxx',
token: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
cron: '0 0 12 * * ?',
taskConfig: [true, true, true],
refreshCookieInterval: [60, 120],
},
];
export const SCHEDULE_CONFIG: Schedule[] = myScheduleConfig;
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"target": "ESNext",
"lib": ["DOM", "ESNext"],
"module": "commonjs",
"types": ["node"],
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
"skipLibCheck": true,
"outDir": "dist"
}
}