@steedos/workflow_time_trigger 2.3.2-beta.3

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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Steedos Licensing
2
+
3
+ SOFTWARE LICENSING
4
+
5
+ To determine under which license you may use a file from the Steedos source code,
6
+ please resort to the header of that file.
7
+
8
+ If the file has no header, the following rules apply
9
+ 1. enterprise features are licensed under Steedos Enterprise Terms, see License.enterprise.txt
10
+ 2. source code that is neither (1) is licensed under MIT, see https://opensource.org/licenses/MIT
11
+
12
+ On request, licenses under different terms are available.
13
+
14
+ Source code of enterprise features are files that
15
+ * are in folders named "ee" or start with "ee_", or in subfolders of such folders.
16
+ * contain the strings "ee_" in its filename name.
17
+ The files can be found by running the command `find . -iname ee -or -iname "*_ee*" -or -iname "*ee_*"`
18
+
19
+ STEEDOS TRADEMARK GUIDELINES
20
+
21
+ Your use of the mark Steedos is subject to Steedos, Inc's prior written approval. For trademark approval or any questions
22
+ you have about using these trademarks, please email zhuangjianguo@steedos.com
@@ -0,0 +1,4 @@
1
+ name: _error
2
+ label: 错误信息
3
+ type: text
4
+ index: true
@@ -0,0 +1,5 @@
1
+ name: _excuted
2
+ label: 已执行
3
+ type: boolean
4
+ required: true
5
+ index: true
@@ -0,0 +1,3 @@
1
+ name: _excuted_at
2
+ label: 执行完成时间
3
+ type: datetime
@@ -0,0 +1,5 @@
1
+ name: _excuting
2
+ label: 正在执行
3
+ type: boolean
4
+ required: true
5
+ index: true
@@ -0,0 +1,3 @@
1
+ name: record_id
2
+ label: 记录ID
3
+ type: text
@@ -0,0 +1,7 @@
1
+ name: updates_field_actions
2
+ type: lookup
3
+ multiple: true
4
+ label: 工作流操作:字段更新
5
+ is_wide: true
6
+ reference_to: action_field_updates
7
+ reference_to_field: name
@@ -0,0 +1,7 @@
1
+ name: workflow_notifications_actions
2
+ type: lookup
3
+ multiple: true
4
+ label: 工作流操作:工作流通知
5
+ is_wide: true
6
+ reference_to: workflow_notifications
7
+ reference_to_field: name
@@ -0,0 +1,7 @@
1
+ name: workflow_outbound_messages_actions
2
+ type: lookup
3
+ multiple: true
4
+ label: 工作流操作:出站消息
5
+ is_wide: true
6
+ reference_to: workflow_outbound_messages
7
+ reference_to_field: name
@@ -0,0 +1,11 @@
1
+ name: all
2
+ columns:
3
+ - field: record_id
4
+ filter_scope: space
5
+ filters: []
6
+ label: 所有
7
+ mobile_columns: []
8
+ scrolling_mode: standard
9
+ shared: true
10
+ sort_no: 100
11
+ type: grid
@@ -0,0 +1,14 @@
1
+ name: 工作流时间触发器执行队列.管理员
2
+ allowCreate: true
3
+ allowCreateFiles: true
4
+ allowDelete: true
5
+ allowDeleteFiles: true
6
+ allowEdit: true
7
+ allowEditFiles: true
8
+ allowRead: true
9
+ allowReadFiles: true
10
+ modifyAllFiles: true
11
+ modifyAllRecords: true
12
+ permission_set_id: admin
13
+ viewAllFiles: true
14
+ viewAllRecords: true
@@ -0,0 +1,14 @@
1
+ name: 工作流时间触发器执行队列.用户
2
+ allowCreate: false
3
+ allowCreateFiles: false
4
+ allowDelete: false
5
+ allowDeleteFiles: false
6
+ allowEdit: false
7
+ allowEditFiles: false
8
+ allowRead: true
9
+ allowReadFiles: true
10
+ modifyAllRecords: false
11
+ permission_set_id: user
12
+ viewAllFiles: true
13
+ viewAllRecords: true
14
+ viewCompanyRecords: true
@@ -0,0 +1,5 @@
1
+ name: workflow_time_trigger_queue
2
+ label: 工作流时间触发器执行队列
3
+ version: 2
4
+ hidden: true
5
+ icon: app
@@ -0,0 +1,105 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2022-11-12 13:18:28
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2022-11-17 09:47:40
6
+ * @Description: 工作流时间触发器定时任务,用于将工作流规则中配置的时间触发器放入工作流操作执行队列
7
+ */
8
+ 'use strict';
9
+ // @ts-check
10
+
11
+ const schedule = require('node-schedule');
12
+ const objectql = require('@steedos/objectql');
13
+ const moment = require('moment');
14
+ const _eval = require('eval')
15
+
16
+ module.exports = {
17
+ run: async function () {
18
+ console.log('time_trigger schedule running...')
19
+ const intervalMinitues = 5 // 每5分钟执行一次
20
+ const rule = `0 */${intervalMinitues} * * * *`
21
+ let next = true;
22
+ schedule.scheduleJob(rule, async function () {
23
+ try {
24
+ if (!next) {
25
+ return;
26
+ }
27
+ next = false;
28
+ const now = moment()
29
+ const workflowRuleObj = objectql.getObject('workflow_rule')
30
+ const queueObj = objectql.getObject('workflow_time_trigger_queue')
31
+ // 遍历已启用的、时间触发器字段不为空的工作流规则表
32
+ const workflowRuleDocs = await workflowRuleObj.find({
33
+ filters: [
34
+ ['active', '=', true],
35
+ ['time_triggers', '!=', null]
36
+ ]
37
+ })
38
+ /**
39
+ * 遍历工作流规则的时间触发器表格字段 根据配置计算出 对象记录的过滤条件
40
+ * 过滤条件计算方式:
41
+ * 首先根据时间触发器中的配置计算出 规则触发的时间点C,如 30 天 早于 合同到期日期,则时间点为 当前时间+30天;如 30天 晚于 合同签订日期,则时间点为 当前时间-30天
42
+ * 过滤的时间范围为 当前时间 到 当前时间+T,即 C <= 合同到期日期 <= C + T ; 意味着执行将会有最多约一个间隔的提前量
43
+ * 将过滤出的记录和配置的工作流操作,放到工作流操作执行队列,由单独的定时器执行
44
+ */
45
+ for (const ruleDoc of workflowRuleDocs) {
46
+ try {
47
+ const { time_triggers, object_name, _id } = ruleDoc
48
+ const filters = _eval(`module.exports = ${ruleDoc.filters}`) || []
49
+ const obj = objectql.getObject(object_name)
50
+ if (time_triggers && obj) {
51
+ for (const tTrigger of time_triggers) {
52
+ const { number, unit, type, date_field, updates_field_actions, workflow_notifications_actions, workflow_outbound_messages_actions } = tTrigger
53
+ if (updates_field_actions || workflow_notifications_actions || workflow_outbound_messages_actions) { // 配置了工作流操作才处理
54
+ let triggerTime;
55
+ if (type == 'earlier_than') {
56
+ triggerTime = moment(now).add(number, unit)
57
+ }
58
+ else if (type == 'later_than') {
59
+ triggerTime = moment(now).add(-number, unit)
60
+ }
61
+ else {
62
+ console.error('time_trigger schedule:', 'invalid type in time_triggers', `workflow_rule _id ${_id}`);
63
+ continue
64
+ }
65
+ const docs = await obj.find({
66
+ filters: [
67
+ filters, // 首先应该查找符合过滤条件的记录
68
+ [date_field, '>=', triggerTime.toDate()],
69
+ [date_field, '<=', moment(triggerTime).add(intervalMinitues, 'minute').toDate()]
70
+ ]
71
+ })
72
+ // 将过滤出的记录和配置的工作流操作,放到工作流操作执行队列,由单独的定时器执行
73
+ for (const doc of docs) {
74
+ await queueObj.insert({
75
+ 'record_id': doc._id,
76
+ 'updates_field_actions': updates_field_actions,
77
+ 'workflow_notifications_actions': workflow_notifications_actions,
78
+ 'workflow_outbound_messages_actions': workflow_outbound_messages_actions,
79
+ '_excuted': false,
80
+ '_excuting': false,
81
+ })
82
+ }
83
+
84
+
85
+ }
86
+ }
87
+ }
88
+ } catch (error) {
89
+ console.error(error.stack);
90
+ console.error('time_trigger schedule:', error.message);
91
+ console.error('ruleDoc:', ruleDoc);
92
+ }
93
+
94
+ }
95
+
96
+
97
+ next = true;
98
+ } catch (error) {
99
+ console.error(error.stack);
100
+ console.error('time_trigger schedule:', error.message);
101
+ next = true;
102
+ }
103
+ });
104
+ }
105
+ }
@@ -0,0 +1,99 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2022-11-12 17:53:55
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2022-11-17 09:24:21
6
+ * @Description: 执行工作流时间触发器队列
7
+ */
8
+
9
+ 'use strict';
10
+ // @ts-check
11
+
12
+ const schedule = require('node-schedule');
13
+ const objectql = require('@steedos/objectql');
14
+
15
+ module.exports = {
16
+ /*
17
+ options: {
18
+ // Controls the sending interval
19
+ sendInterval: Match.Optional(Number),
20
+ // Controls the sending batch size per interval
21
+ excuteBatchSize: Match.Optional(Number),
22
+ // Allow optional keeping notifications in collection
23
+ keepDocs: Match.Optional(Boolean)
24
+ }
25
+ */
26
+ run: async function (options = {}) {
27
+ console.log('time_trigger_queue schedule running...')
28
+ const opt = {
29
+ excuteBatchSize: 10,
30
+ keepDocs: true,
31
+ ...options
32
+ }
33
+ const rule = `*/10 * * * * *` // 每十秒执行一次
34
+ let next = true;
35
+ schedule.scheduleJob(rule, async function () {
36
+ try {
37
+ if (!next) {
38
+ return;
39
+ }
40
+ next = false;
41
+
42
+ // 查找队列记录
43
+ const queueObj = objectql.getObject('workflow_time_trigger_queue')
44
+ const queueDocs = await queueObj.find({
45
+ filters: [
46
+ ['_excuted', '=', false],
47
+ ['_excuting', '=', false],
48
+ ['_error', '=', null]
49
+ ],
50
+ limit: opt.excuteBatchSize
51
+ })
52
+ for (const doc of queueDocs) {
53
+ try {
54
+ await queueObj.update(doc._id, {
55
+ '_excuting': true // 正在执行,防止重复执行
56
+ })
57
+
58
+ const { record_id, updates_field_actions, workflow_notifications_actions, workflow_outbound_messages_actions } = doc
59
+ // 字段更新
60
+ if (updates_field_actions) {
61
+ await objectql.runFieldUpdateActions(updates_field_actions, record_id);
62
+ }
63
+ // 工作流通知
64
+ if (workflow_notifications_actions) {
65
+ await objectql.runWorkflowNotifyActions(workflow_notifications_actions, record_id);
66
+ }
67
+ // 出站消息
68
+ if (workflow_outbound_messages_actions) {
69
+ await objectql.runWorkflowOutboundMessageActions(workflow_outbound_messages_actions, record_id);
70
+ }
71
+
72
+ if (opt.keepDocs) {
73
+ await queueObj.update(doc._id, {
74
+ '_excuted': true, // 执行结束
75
+ '_excuting': false
76
+ })
77
+ }
78
+ else {
79
+ await queueObj.delete(doc._id)
80
+ }
81
+
82
+ } catch (error) {
83
+ console.error(error)
84
+ await queueObj.update(doc._id, {
85
+ '_error': error.stack
86
+ })
87
+ }
88
+
89
+ }
90
+
91
+ next = true;
92
+ } catch (error) {
93
+ console.error(error.stack);
94
+ console.error('time_trigger_queue schedule:', error.message);
95
+ next = true;
96
+ }
97
+ });
98
+ }
99
+ }
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@steedos/workflow_time_trigger",
3
+ "version": "2.3.2-beta.3",
4
+ "main": "package.service.js",
5
+ "scripts": {},
6
+ "license": "MIT",
7
+ "private": false,
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "dependencies": {
12
+ "eval": "0.1.8",
13
+ "node-schedule": "2.1.0"
14
+ },
15
+ "gitHead": "1f337e8da176f1c75a8596b29ec7889eca996dc1"
16
+ }
@@ -0,0 +1,75 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2022-11-12 13:05:05
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2022-11-12 20:19:51
6
+ * @Description:
7
+ */
8
+ "use strict";
9
+ const project = require('./package.json');
10
+ const packageName = project.name;
11
+ const packageLoader = require('@steedos/service-package-loader');
12
+
13
+ module.exports = {
14
+ name: packageName,
15
+ namespace: "steedos",
16
+ mixins: [packageLoader],
17
+ /**
18
+ * Settings
19
+ */
20
+ settings: {
21
+ packageInfo: {
22
+ path: __dirname,
23
+ name: packageName,
24
+ isPackage: false
25
+ }
26
+ },
27
+
28
+ /**
29
+ * Dependencies
30
+ */
31
+ dependencies: ['~packages-@steedos/standard-process-approval'],
32
+
33
+ /**
34
+ * Actions
35
+ */
36
+ actions: {
37
+
38
+ },
39
+
40
+ /**
41
+ * Events
42
+ */
43
+ events: {
44
+
45
+ },
46
+
47
+ /**
48
+ * Methods
49
+ */
50
+ methods: {
51
+
52
+ },
53
+
54
+ /**
55
+ * Service created lifecycle event handler
56
+ */
57
+ async created() {
58
+
59
+ },
60
+
61
+ /**
62
+ * Service started lifecycle event handler
63
+ */
64
+ async started() {
65
+ require('./main/default/schedule/time_trigger').run()
66
+ require('./main/default/schedule/time_trigger_queue').run()
67
+ },
68
+
69
+ /**
70
+ * Service stopped lifecycle event handler
71
+ */
72
+ async stopped() {
73
+
74
+ }
75
+ };