@kne/fastify-task 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+
2
+ # fastify-task
3
+
4
+
5
+ ### 描述
6
+
7
+ 用于编排任务,追踪任务执行状态,结果
8
+
9
+
10
+ ### 安装
11
+
12
+ ```shell
13
+ npm i --save @kne/fastify-task
14
+ ```
15
+
16
+ ### 示例
17
+
18
+ #### 示例代码
19
+
20
+
21
+
22
+ ### API
23
+
24
+ | 属性名 | 说明 | 类型 | 默认值 |
25
+ |-----|----|----|-----|
26
+ | | | | |
27
+
package/index.js ADDED
@@ -0,0 +1,62 @@
1
+ const fp = require('fastify-plugin');
2
+ const path = require('node:path');
3
+
4
+ module.exports = fp(async (fastify, options) => {
5
+ options = Object.assign(
6
+ {},
7
+ {
8
+ dbTableNamePrefix: 't_',
9
+ prefix: '/api/task',
10
+ name: 'task',
11
+ limit: 10,
12
+ dir: path.resolve(process.cwd(), 'libs', 'tasks'),
13
+ syncCron: '*/10 * * * *',
14
+ scriptName: 'index',
15
+ maxPollTimes: 100,
16
+ pollInterval: 5000,
17
+ getUserModel: () => {
18
+ return fastify.account.user;
19
+ },
20
+ getAuthenticate: () => {
21
+ return [fastify.account.authenticate.user, fastify.account.authenticate.admin];
22
+ },
23
+ task: {}
24
+ },
25
+ options
26
+ );
27
+
28
+ fastify.register(require('@kne/fastify-namespace'), {
29
+ options,
30
+ name: options.name,
31
+ modules: [
32
+ ['controllers', path.resolve(__dirname, './libs/controllers')],
33
+ [
34
+ 'models',
35
+ await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
36
+ prefix: options.dbTableNamePrefix,
37
+ getUserModel: options.getUserModel,
38
+ modelPrefix: options.name
39
+ })
40
+ ],
41
+ ['services', path.resolve(__dirname, './libs/services')]
42
+ ]
43
+ });
44
+
45
+ fastify.register(
46
+ require('fastify-plugin')(async fastify => {
47
+ options.syncCron &&
48
+ fastify.register(require('fastify-cron'), {
49
+ jobs: [
50
+ {
51
+ cronTime: options.syncCron,
52
+ onTick: async () => {
53
+ const { runner } = fastify[options.name].services;
54
+ await runner();
55
+ },
56
+ start: true
57
+ }
58
+ ]
59
+ });
60
+ })
61
+ );
62
+ });
@@ -0,0 +1,89 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { services } = fastify[options.name];
5
+ fastify.get(
6
+ `${options.prefix}/list`,
7
+ {
8
+ onRequest: options.getAuthenticate('read'),
9
+ schema: {
10
+ summary: '获取任务列表',
11
+ query: {
12
+ type: 'object',
13
+ properties: {
14
+ perPage: {
15
+ type: 'number',
16
+ default: 20
17
+ },
18
+ currentPage: {
19
+ type: 'number',
20
+ default: 1
21
+ },
22
+ filter: {
23
+ type: 'object'
24
+ }
25
+ }
26
+ }
27
+ }
28
+ },
29
+ async request => {
30
+ return services.list(request.query);
31
+ }
32
+ );
33
+
34
+ fastify.post(
35
+ `${options.prefix}/complete`,
36
+ {
37
+ onRequest: options.getAuthenticate('write'),
38
+ schema: {
39
+ summary: '手动完成任务',
40
+ body: {
41
+ type: 'object',
42
+ properties: {
43
+ id: {
44
+ type: 'string'
45
+ },
46
+ status: {
47
+ type: 'string',
48
+ enum: ['success', 'failed']
49
+ },
50
+ error: {
51
+ type: 'string'
52
+ },
53
+ msg: {
54
+ type: 'string'
55
+ },
56
+ output: {
57
+ type: 'object'
58
+ }
59
+ },
60
+ required: ['id', 'status']
61
+ }
62
+ }
63
+ },
64
+ async request => {
65
+ return services.complete(Object.assign({}, request.body, { userId: request.userInfo?.id }));
66
+ }
67
+ );
68
+
69
+ fastify.post(
70
+ `${options.prefix}/cancel`,
71
+ {
72
+ onRequest: options.getAuthenticate('write'),
73
+ schema: {
74
+ summary: '取消任务',
75
+ body: {
76
+ type: 'object',
77
+ properties: {
78
+ id: {
79
+ type: 'string'
80
+ }
81
+ }
82
+ }
83
+ }
84
+ },
85
+ async request => {
86
+ return services.cancel(request.body);
87
+ }
88
+ );
89
+ });
@@ -0,0 +1,18 @@
1
+ module.exports = ({DataTypes, options}) => {
2
+ return {
3
+ //name: 'modelName',//此处定义modelName,默认根据文件名转驼峰命名,可以缺省
4
+ model: {
5
+ name: {
6
+ type: DataTypes.STRING,
7
+ comment: '名称'
8
+ }
9
+ }, associate: ({/*这里可以获取models*/}, fastify) => {
10
+ //可以这样获取某个namespace的models fastify.account.models
11
+ // options.getUserModel() 可以获取 user model
12
+ }, options: {
13
+ comment: '',
14
+ indexes: []
15
+ }
16
+ };
17
+ };
18
+
@@ -0,0 +1,83 @@
1
+ module.exports = ({ DataTypes, definePrimaryType, options }) => {
2
+ return {
3
+ model: {
4
+ type: {
5
+ type: DataTypes.STRING,
6
+ comment: '任务类型'
7
+ },
8
+ scriptName: {
9
+ type: DataTypes.STRING,
10
+ comment: '任务脚本名称'
11
+ },
12
+ targetId: definePrimaryType('targetId', {
13
+ comment: '任务目标对象id',
14
+ allowNull: false
15
+ }),
16
+ targetType: {
17
+ type: DataTypes.STRING,
18
+ comment: '任务目标对象类型',
19
+ allowNull: false
20
+ },
21
+ runnerType: {
22
+ type: DataTypes.ENUM('manual', 'system'),
23
+ comment: '任务执行者类型: manual为手动执行, system为系统自动执行',
24
+ defaultValue: 'manual',
25
+ allowNull: false
26
+ },
27
+ startTime: {
28
+ type: DataTypes.DATE,
29
+ comment: '任务最早执行时间',
30
+ defaultValue: new Date()
31
+ },
32
+ completedAt: {
33
+ type: DataTypes.DATE,
34
+ comment: '任务完成时间'
35
+ },
36
+ input: {
37
+ type: DataTypes.JSON,
38
+ comment: '输入数据'
39
+ },
40
+ output: {
41
+ type: DataTypes.JSON,
42
+ comment: '输出数据'
43
+ },
44
+ error: {
45
+ type: DataTypes.JSON,
46
+ comment: '错误信息'
47
+ },
48
+ status: {
49
+ type: DataTypes.ENUM('pending', 'running', 'success', 'failed', 'canceled'),
50
+ comment: '任务状态',
51
+ defaultValue: 'pending'
52
+ },
53
+ progress: {
54
+ type: DataTypes.INTEGER,
55
+ comment: '任务进度(0-100)',
56
+ defaultValue: 0
57
+ },
58
+ msg: {
59
+ type: DataTypes.TEXT,
60
+ comment: '任务消息或错误信息'
61
+ },
62
+ options: {
63
+ type: DataTypes.JSON,
64
+ comment: '任务扩展选项'
65
+ }
66
+ },
67
+ associate: ({ task }) => {
68
+ task.belongsTo(options.getUserModel(), {
69
+ foreignKey: 'userId',
70
+ as: 'createdUser',
71
+ comment: '创建人(为空时为系统创建)'
72
+ });
73
+ task.belongsTo(options.getUserModel(), {
74
+ foreignKey: 'completedUserId',
75
+ as: 'completedUser',
76
+ comment: '任务完成人'
77
+ });
78
+ },
79
+ options: {
80
+ comment: '任务'
81
+ }
82
+ };
83
+ };
@@ -0,0 +1,10 @@
1
+ const fp = require('fastify-plugin');
2
+
3
+ module.exports = fp(async (fastify, options) => {
4
+ const { models, services } = fastify[options.name];
5
+ const { Op } = fastify.sequelize.Sequelize;
6
+
7
+ Object.assign(fastify[options.name].services, {
8
+
9
+ });
10
+ });
@@ -0,0 +1,165 @@
1
+ const fp = require('fastify-plugin');
2
+ const path = require('node:path');
3
+ const fs = require('node:fs/promises');
4
+
5
+ module.exports = fp(async (fastify, options) => {
6
+ const { models } = fastify[options.name];
7
+ const { Op } = fastify.sequelize.Sequelize;
8
+ const create = async ({ userId, input, type, targetId, targetType, runnerType, delay, scriptName, options }) => {
9
+ if (typeof options.task[type] !== 'function') {
10
+ throw new Error('未找到合法的任务声明');
11
+ }
12
+ return await models.task.create({
13
+ userId,
14
+ input,
15
+ type,
16
+ targetId,
17
+ targetType,
18
+ runnerType,
19
+ startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
20
+ scriptName,
21
+ options
22
+ });
23
+ };
24
+
25
+ const processSystemTask = async task => {
26
+ const taskModulePath = path.resolve(options.dir, task.scriptName || options.scriptName);
27
+ if (!(await fs.exists(taskModulePath))) {
28
+ await task.update({
29
+ status: 'failed',
30
+ error: '未匹配到任务执行器'
31
+ });
32
+ }
33
+ await task.update({
34
+ status: 'running',
35
+ progress: 0
36
+ });
37
+ require(taskModulePath)(fastify, options, {
38
+ task,
39
+ progress: async progress => {
40
+ await task.update({
41
+ progress
42
+ });
43
+ },
44
+ complete: async result => {
45
+ await options.task[task.type](result);
46
+ await task.update({
47
+ status: 'success',
48
+ output: result,
49
+ progress: 100,
50
+ completeAt: new Date()
51
+ });
52
+ },
53
+ polling: async (callback, currentOptions) => {
54
+ let pollCount = 0;
55
+ const maxPollTimes = currentOptions.maxPollTimes || options.maxPollTimes;
56
+ const pollInterval = currentOptions.pollInterval || options.pollInterval;
57
+ await new Promise((resolve, reject) => {
58
+ const timer = setInterval(async () => {
59
+ pollCount++;
60
+ if (pollCount > maxPollTimes) {
61
+ clearInterval(timer);
62
+ reject(`轮询超时(${maxPollTimes}次),任务未完成`);
63
+ }
64
+ const { result, message } = await callback();
65
+ if (result === 'failed') {
66
+ clearInterval(timer);
67
+ reject(`任务处理失败:${message}`);
68
+ }
69
+ if (result === 'success') {
70
+ clearInterval(timer);
71
+ resolve();
72
+ }
73
+ }, pollInterval);
74
+ });
75
+ }
76
+ }).catch(error => {
77
+ return task.update({
78
+ status: 'failed',
79
+ error: error.toString(),
80
+ completeAt: new Date()
81
+ });
82
+ });
83
+ };
84
+
85
+ const runner = async () => {
86
+ const pendingTasks = await models.task.findAll({
87
+ where: {
88
+ runnerType: 'system',
89
+ status: 'pending',
90
+ startTime: {
91
+ [Op.lte]: new Date()
92
+ }
93
+ },
94
+ limit: options.limit
95
+ });
96
+
97
+ const count = await models.task.count({
98
+ where: {
99
+ runnerType: 'system',
100
+ status: 'pending'
101
+ }
102
+ });
103
+
104
+ if (pendingTasks.length > 0) {
105
+ fastify.log.info(`本轮执行 ${pendingTasks.length}/${count} 个待处理的系统任务`);
106
+ await Promise.all(pendingTasks.map(async task => processSystemTask(task)));
107
+ }
108
+ };
109
+
110
+ const detail = async ({ id }) => {
111
+ const task = await models.task.findByPk(id);
112
+ if (!task) {
113
+ throw new Error('任务不存在');
114
+ }
115
+ return task;
116
+ };
117
+
118
+ const cancel = async ({ id }) => {
119
+ const task = await detail({ id });
120
+ return await task.update({
121
+ status: 'canceled'
122
+ });
123
+ };
124
+
125
+ const complete = async ({ id, userId, ...props }) => {
126
+ const task = await detail({ id });
127
+ await task.update(
128
+ Object.assign({}, props, {
129
+ completeAt: new Date(),
130
+ completedUserId: userId
131
+ })
132
+ );
133
+ };
134
+
135
+ const list = async ({ filter, perPage, currentPage }) => {
136
+ const whereQuery = {};
137
+
138
+ ['targetId', 'type', 'status', 'runnerType'].forEach(key => {
139
+ if (filter && filter[key]) {
140
+ whereQuery[key] = filter[key];
141
+ }
142
+ });
143
+
144
+ const { rows, count } = await models.task.findAndCountAll({
145
+ where: Object.assign({}, whereQuery),
146
+ offset: perPage * (currentPage - 1),
147
+ limit: perPage,
148
+ order: [['createdAt', 'DESC']]
149
+ });
150
+
151
+ return {
152
+ pageData: rows,
153
+ totalCount: count
154
+ };
155
+ };
156
+
157
+ Object.assign(fastify[options.name].services, {
158
+ create,
159
+ detail,
160
+ list,
161
+ complete,
162
+ cancel,
163
+ runner
164
+ });
165
+ });
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@kne/fastify-task",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "用于编排任务,追踪任务执行状态,结果",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "init": "husky",
8
+ "build:md": "npx @kne/md-doc",
9
+ "start:md": "npx @kne/md-doc --watch",
10
+ "prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
11
+ "lint-staged": "npx lint-staged"
12
+ },
13
+ "lint-staged": {
14
+ "{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}": [
15
+ "prettier --config .prettierrc --write",
16
+ "git add"
17
+ ]
18
+ },
19
+ "files": [
20
+ "index.js",
21
+ "libs"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/kne-union/fastify-task.git"
26
+ },
27
+ "keywords": [],
28
+ "author": "linzp",
29
+ "license": "ISC",
30
+ "bugs": {
31
+ "url": "https://github.com/kne-union/fastify-task/issues"
32
+ },
33
+ "homepage": "https://github.com/kne-union/fastify-task#readme",
34
+ "peerDependencies": {
35
+ "@kne/fastify-namespace": "*",
36
+ "@kne/fastify-sequelize": "*",
37
+ "fastify-plugin": ">=5"
38
+ },
39
+ "devDependencies": {
40
+ "@fastify/env": "^5.0.2",
41
+ "@kne/fastify-sequelize": "^3.0.6",
42
+ "fastify": "^5.4.0",
43
+ "fastify-plugin": "^5.0.1",
44
+ "husky": "^9.0.11",
45
+ "nodemon": "^3.1.3",
46
+ "prettier": "^3.2.5",
47
+ "sqlite3": "^5.1.7"
48
+ },
49
+ "dependencies": {
50
+ "fastify-cron": "^1.4.0"
51
+ }
52
+ }