groundwork for cron automation trigger
This commit is contained in:
parent
ea387e1fa2
commit
7c3b3f78a7
|
@ -4,8 +4,15 @@ const logic = require("../../automations/logic")
|
|||
const triggers = require("../../automations/triggers")
|
||||
const webhooks = require("./webhook")
|
||||
const { getAutomationParams, generateAutomationID } = require("../../db/utils")
|
||||
const { JobQueues } = require("../../constants")
|
||||
const { utils } = require("@budibase/auth/redis")
|
||||
const Queue = env.isTest()
|
||||
? require("../utilities/queue/inMemoryQueue")
|
||||
: require("bull")
|
||||
|
||||
const WH_STEP_ID = triggers.BUILTIN_DEFINITIONS.WEBHOOK.stepId
|
||||
const CRON_STEP_ID = triggers.BUILTIN_DEFINITIONS.CRON.stepId
|
||||
|
||||
/*************************
|
||||
* *
|
||||
* BUILDER FUNCTIONS *
|
||||
|
@ -32,6 +39,62 @@ function cleanAutomationInputs(automation) {
|
|||
return automation
|
||||
}
|
||||
|
||||
/**
|
||||
* This function handles checking of any cron jobs need to be created or deleted for automations.
|
||||
* @param {string} appId The ID of the app in which we are checking for webhooks
|
||||
* @param {object|undefined} oldAuto The old automation object if updating/deleting
|
||||
* @param {object|undefined} newAuto The new automation object if creating/updating
|
||||
* @returns {Promise<object|undefined>} After this is complete the new automation object may have been updated and should be
|
||||
* written to DB (this does not write to DB as it would be wasteful to repeat).
|
||||
*/
|
||||
async function checkForCronTriggers({ appId, oldAuto, newAuto }) {
|
||||
const oldTrigger = oldAuto ? oldAuto.definition.trigger : null
|
||||
const newTrigger = newAuto ? newAuto.definition.trigger : null
|
||||
function isCronTrigger(auto) {
|
||||
return (
|
||||
auto &&
|
||||
auto.definition.trigger &&
|
||||
auto.definition.trigger.stepId === CRON_STEP_ID
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: AUTO GENERATED ID THAT CAN BE USED TO KILL THE CRON JOB ON CHANGE
|
||||
|
||||
// need to delete cron trigger
|
||||
if (
|
||||
isCronTrigger(oldAuto) &&
|
||||
!isCronTrigger(newAuto) &&
|
||||
oldTrigger.webhookId
|
||||
) {
|
||||
triggers.automationQueue.add("cron", { repeat: { cron: newAuto.inputs.cron } });
|
||||
// let db = new CouchDB(appId)
|
||||
// // need to get the webhook to get the rev
|
||||
// const webhook = await db.get(oldTrigger.webhookId)
|
||||
// const ctx = {
|
||||
// appId,
|
||||
// params: { id: webhook._id, rev: webhook._rev },
|
||||
// }
|
||||
// // might be updating - reset the inputs to remove the URLs
|
||||
// if (newTrigger) {
|
||||
// delete newTrigger.webhookId
|
||||
// newTrigger.inputs = {}
|
||||
// }
|
||||
// await webhooks.destroy(ctx)
|
||||
}
|
||||
// need to create cron job
|
||||
else if (!isCronTrigger(oldAuto) && isCronTrigger(newAuto)) {
|
||||
automationQueue.add(newAuto, { repeat: { cron: newAuto.inputs.cron } });
|
||||
|
||||
const id = ctx.body.webhook._id
|
||||
// newTrigger.webhookId = id
|
||||
// newTrigger.inputs = {
|
||||
// schemaUrl: `api/webhooks/schema/${appId}/${id}`,
|
||||
// triggerUrl: `api/webhooks/trigger/${appId}/${id}`,
|
||||
// }
|
||||
}
|
||||
return newAuto
|
||||
}
|
||||
|
||||
/**
|
||||
* This function handles checking if any webhooks need to be created or deleted for automations.
|
||||
* @param {string} appId The ID of the app in which we are checking for webhooks
|
||||
|
@ -111,6 +174,10 @@ exports.create = async function (ctx) {
|
|||
appId: ctx.appId,
|
||||
newAuto: automation,
|
||||
})
|
||||
automation = await checkForCronTriggers({
|
||||
appId: ctx.appId,
|
||||
newAuto: automation,
|
||||
})
|
||||
const response = await db.put(automation)
|
||||
automation._rev = response.rev
|
||||
|
||||
|
@ -135,6 +202,11 @@ exports.update = async function (ctx) {
|
|||
oldAuto: oldAutomation,
|
||||
newAuto: automation,
|
||||
})
|
||||
automation = await checkForCronTriggers({
|
||||
appId: ctx.appId,
|
||||
oldAuto: oldAutomation,
|
||||
newAuto: automation,
|
||||
})
|
||||
const response = await db.put(automation)
|
||||
automation._rev = response.rev
|
||||
|
||||
|
|
|
@ -7,9 +7,10 @@ const Queue = env.isTest()
|
|||
const { getAutomationParams } = require("../db/utils")
|
||||
const { coerce } = require("../utilities/rowProcessor")
|
||||
const { utils } = require("@budibase/auth/redis")
|
||||
const { JobQueues } = require("../constants")
|
||||
|
||||
const { opts } = utils.getRedisOptions()
|
||||
let automationQueue = new Queue("automationQueue", { redis: opts })
|
||||
let automationQueue = new Queue(JobQueues.AUTOMATIONS, { redis: opts })
|
||||
|
||||
const FAKE_STRING = "TEST"
|
||||
const FAKE_BOOL = false
|
||||
|
@ -196,6 +197,46 @@ const BUILTIN_DEFINITIONS = {
|
|||
},
|
||||
type: "TRIGGER",
|
||||
},
|
||||
CRON: {
|
||||
name: "Cron Trigger",
|
||||
event: "cron:trigger",
|
||||
icon: "ri-timer-line",
|
||||
tagline: "Cron expression",
|
||||
description: "Triggers automation on a cron schedule.",
|
||||
stepId: "CRON",
|
||||
inputs: {},
|
||||
schema: {
|
||||
inputs: {
|
||||
properties: {
|
||||
cron: {
|
||||
type: "string",
|
||||
customType: "cron",
|
||||
title: "Cron Expression",
|
||||
},
|
||||
},
|
||||
required: ["cron"],
|
||||
},
|
||||
outputs: {
|
||||
properties: {
|
||||
// row: {
|
||||
// type: "object",
|
||||
// customType: "row",
|
||||
// description: "The row that was updated",
|
||||
// },
|
||||
// id: {
|
||||
// type: "string",
|
||||
// description: "Row ID - can be used for updating",
|
||||
// },
|
||||
// revision: {
|
||||
// type: "string",
|
||||
// description: "Revision of row",
|
||||
// },
|
||||
},
|
||||
// required: ["row", "id"],
|
||||
},
|
||||
},
|
||||
type: "TRIGGER",
|
||||
},
|
||||
}
|
||||
|
||||
async function queueRelevantRowAutomations(event, eventType) {
|
||||
|
|
|
@ -5,6 +5,10 @@ const { ObjectStoreBuckets } = require("@budibase/auth").objectStore
|
|||
exports.LOGO_URL =
|
||||
"https://d33wubrfki0l68.cloudfront.net/aac32159d7207b5085e74a7ef67afbb7027786c5/2b1fd/img/logo/bb-emblem.svg"
|
||||
|
||||
exports.JobQueues = {
|
||||
AUTOMATIONS: "automationQueue"
|
||||
}
|
||||
|
||||
exports.FieldTypes = {
|
||||
STRING: "string",
|
||||
LONGFORM: "longform",
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue