Skip to content

Commit

Permalink
兼容会导致多个.trigger.js监听同一个对象和事件时导致触发器重复执行,且本身.trigger.js不支持微服务模式,故.trigg…
Browse files Browse the repository at this point in the history
…er.js调整为仍按老的方式执行,不走action代理。 (#4647)

#4647
  • Loading branch information
sunhaolin committed Apr 25, 2024
1 parent b56fbc3 commit 869f9bb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 96 deletions.
4 changes: 2 additions & 2 deletions packages/objectql/src/types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,15 +1987,15 @@ export class SteedosObjectType extends SteedosObjectProperties {
let meteorWhen = `before${method.charAt(0).toLocaleUpperCase()}${_.rest([...method]).join('')}`
await this.runBaseTriggers(meteorWhen, context);
await this.runFunctionTriggers(meteorWhen, context);
// await this.runTriggers(meteorWhen, context);
await this.runTriggers(meteorWhen, context);
return await this.runTriggerActions(meteorWhen, context)
}

private async runAfterTriggers(method: string, context: SteedosTriggerContextConfig) {
let meteorWhen = `after${method.charAt(0).toLocaleUpperCase()}${_.rest([...method]).join('')}`
await this.runBaseTriggers(meteorWhen, context);
await this.runFunctionTriggers(meteorWhen, context);
// await this.runTriggers(meteorWhen, context);
await this.runTriggers(meteorWhen, context);
return await this.runTriggerActions(meteorWhen, context)
}

Expand Down
188 changes: 94 additions & 94 deletions services/service-package-loader/src/triggerLoader/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/*
* @Author: [email protected]
* @Date: 2022-06-12 19:08:48
* @LastEditors: baozhoutao@steedos.com
* @LastEditTime: 2023-05-30 09:52:23
* @LastEditors: 孙浩林 sunhaolin@steedos.com
* @LastEditTime: 2024-04-25 11:22:11
* @Description: 加载*.trigger.js文件注册为新版action trigger
*/
import * as _ from "underscore";
import * as path from "path";
import { JSONStringify, getMD5 } from "@steedos/metadata-core";
// import { JSONStringify, getMD5 } from "@steedos/metadata-core";
import { loadObjectTriggers } from "@steedos/metadata-registrar";



const TRIGGERKEYS = ['beforeFind', 'beforeInsert', 'beforeUpdate', 'beforeDelete', 'afterFind', 'afterInsert', 'afterUpdate', 'afterDelete', 'afterFindOne', 'afterCount']
// const TRIGGERKEYS = ['beforeFind', 'beforeInsert', 'beforeUpdate', 'beforeDelete', 'afterFind', 'afterInsert', 'afterUpdate', 'afterDelete', 'afterFindOne', 'afterCount']

function getObject(objectName: string) {
try {
const objectql = require('@steedos/objectql');
return objectql.getObject(objectName);
} catch (error) {
return null
}
}
// function getObject(objectName: string) {
// try {
// const objectql = require('@steedos/objectql');
// return objectql.getObject(objectName);
// } catch (error) {
// return null
// }
// }

export async function load(broker: any, packagePath: string, packageServiceName: string) {
let actions = {};
let serviceName = `~triggers-${packageServiceName}`;
// let actions = {};
// let serviceName = `~triggers-${packageServiceName}`;
let filePath = path.join(packagePath, "**");
let objTriggers = loadObjectTriggers(filePath, packageServiceName);
if (_.isEmpty(objTriggers)) {
Expand Down Expand Up @@ -58,96 +58,96 @@ export async function load(broker: any, packagePath: string, packageServiceName:
}
}
*/
const actionTriggerName = getMD5(JSONStringify(trigger));
actions[actionTriggerName] = generateActionTrigger(trigger)
// const actionTriggerName = getMD5(JSONStringify(trigger));
// actions[actionTriggerName] = generateActionTrigger(trigger)

broker.emit('trigger.loaded', {
objectName: trigger['listenTo']
})
}

let serviceConfig = {
name: serviceName,
actions: actions
};
let service = broker.createService(serviceConfig);
if (!broker.started) {
await broker._restartService(service)
}
// let serviceConfig = {
// name: serviceName,
// actions: actions
// };
// let service = broker.createService(serviceConfig);
// if (!broker.started) {
// await broker._restartService(service)
// }

}

// 生成action trigger
function generateActionTrigger(trigger) {
const when = [];
for (const key in trigger) {
if (Object.hasOwnProperty.call(trigger, key)) {
if (_.contains(TRIGGERKEYS, key)) {
when.push(key);
}
}
}
const actionTrigger = {
trigger: {
listenTo: trigger.listenTo,
when: when
},
async handler(ctx) {
// 调用trigger.js的处理函数
const {
isInsert, isUpdate, isDelete, isFind, isBefore, isAfter, isFindOne, isCount,
id, doc, previousDoc,
// size,
userId, spaceId, objectName, query, data }: any = ctx.params;
// function generateActionTrigger(trigger) {
// const when = [];
// for (const key in trigger) {
// if (Object.hasOwnProperty.call(trigger, key)) {
// if (_.contains(TRIGGERKEYS, key)) {
// when.push(key);
// }
// }
// }
// const actionTrigger = {
// trigger: {
// listenTo: trigger.listenTo,
// when: when
// },
// async handler(ctx) {
// // 调用trigger.js的处理函数
// const {
// isInsert, isUpdate, isDelete, isFind, isBefore, isAfter, isFindOne, isCount,
// id, doc, previousDoc,
// // size,
// userId, spaceId, objectName, query, data }: any = ctx.params;

const context: any = {
id,
userId,
spaceId,
doc,
previousDoc,
query,
data,
objectName
}
// const context: any = {
// id,
// userId,
// spaceId,
// doc,
// previousDoc,
// query,
// data,
// objectName
// }

let when = ''
if (isBefore) {
if (isFind) {
when = 'beforeFind'
} else if (isInsert) {
when = 'beforeInsert'
} else if (isUpdate) {
when = 'beforeUpdate'
} else if (isDelete) {
when = 'beforeDelete'
}
}
else if (isAfter) {
if (isFind) {
when = 'afterFind'
} else if (isInsert) {
when = 'afterInsert'
} else if (isUpdate) {
when = 'afterUpdate'
} else if (isDelete) {
when = 'afterDelete'
} else if (isFindOne) {
when = 'afterFindOne'
} else if (isCount) {
when = 'afterCount'
}
}
// let when = ''
// if (isBefore) {
// if (isFind) {
// when = 'beforeFind'
// } else if (isInsert) {
// when = 'beforeInsert'
// } else if (isUpdate) {
// when = 'beforeUpdate'
// } else if (isDelete) {
// when = 'beforeDelete'
// }
// }
// else if (isAfter) {
// if (isFind) {
// when = 'afterFind'
// } else if (isInsert) {
// when = 'afterInsert'
// } else if (isUpdate) {
// when = 'afterUpdate'
// } else if (isDelete) {
// when = 'afterDelete'
// } else if (isFindOne) {
// when = 'afterFindOne'
// } else if (isCount) {
// when = 'afterCount'
// }
// }

if (when) {
const object = getObject(objectName);
if(!object){
return ;
}
await object.runTriggers(when, context);
return context;
}
}
};
return actionTrigger;
}
// if (when) {
// const object = getObject(objectName);
// if(!object){
// return ;
// }
// await object.runTriggers(when, context);
// return context;
// }
// }
// };
// return actionTrigger;
// }

0 comments on commit 869f9bb

Please sign in to comment.