2022-11-25 16:01:46 +01:00
|
|
|
import { rowEmission, tableEmission } from "./utils"
|
|
|
|
import mainEmitter from "./index"
|
|
|
|
import env from "../environment"
|
|
|
|
import { Table, Row } from "@budibase/types"
|
2020-12-07 18:23:53 +01:00
|
|
|
|
|
|
|
// max number of automations that can chain on top of each other
|
2022-08-23 13:35:53 +02:00
|
|
|
// TODO: in future make this configurable at the automation level
|
|
|
|
const MAX_AUTOMATION_CHAIN = env.SELF_HOSTED ? 5 : 0
|
2020-12-07 18:23:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Special emitter which takes the count of automation runs which have occurred and blocks an
|
|
|
|
* automation from running if it has reached the maximum number of chained automations runs.
|
|
|
|
* This essentially "fakes" the normal emitter to add some functionality in-between to stop automations
|
|
|
|
* from getting stuck endlessly chaining.
|
|
|
|
*/
|
|
|
|
class AutomationEmitter {
|
2022-11-25 16:01:46 +01:00
|
|
|
chainCount: number
|
|
|
|
metadata: { automationChainCount: number }
|
|
|
|
|
|
|
|
constructor(chainCount: number) {
|
2020-12-07 18:23:53 +01:00
|
|
|
this.chainCount = chainCount
|
|
|
|
this.metadata = {
|
|
|
|
automationChainCount: chainCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:01:46 +01:00
|
|
|
emitRow(eventName: string, appId: string, row: Row, table?: Table) {
|
2020-12-07 18:23:53 +01:00
|
|
|
// don't emit even if we've reached max automation chain
|
2020-12-07 18:55:35 +01:00
|
|
|
if (this.chainCount >= MAX_AUTOMATION_CHAIN) {
|
2020-12-07 18:23:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rowEmission({
|
|
|
|
emitter: mainEmitter,
|
|
|
|
eventName,
|
|
|
|
appId,
|
|
|
|
row,
|
|
|
|
table,
|
|
|
|
metadata: this.metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:01:46 +01:00
|
|
|
emitTable(eventName: string, appId: string, table?: Table) {
|
2020-12-07 18:23:53 +01:00
|
|
|
// don't emit even if we've reached max automation chain
|
|
|
|
if (this.chainCount > MAX_AUTOMATION_CHAIN) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-25 16:01:46 +01:00
|
|
|
|
2020-12-07 18:23:53 +01:00
|
|
|
tableEmission({
|
|
|
|
emitter: mainEmitter,
|
|
|
|
eventName,
|
|
|
|
appId,
|
|
|
|
table,
|
|
|
|
metadata: this.metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:01:46 +01:00
|
|
|
export = AutomationEmitter
|