Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
eventSubscriber.on('eventCreated', async ({ event, platformId, env } = {}) => {
// APM transaction needs to be created for customRequester
// Note: APM transactions cannot be nested
const prepareWorkflowsTransaction = apm.startTransaction('Prepare Workflows', 'workflow')
apm.setUserContext({ id: platformId })
apm.addLabels({ platformId, env, eventType: event.type })
/*
Objects to be shared across ALL workflows for this single event
Remember that these can be mutated/overwritten by each of them
*/
let runId
let workflowsCtx = {} // shared and same for all current event’s workflows
let vm
try {
const {
Event,
Workflow,
WorkflowLog
async function emitTaskEvents () {
let fetchEventsTransaction = apm.startTransaction('Fetch task events to emit via cron')
try {
// use ref date because cron job cannot trigger at the specified time (with 0 millisecond)
const refDate = getRoundedDate(new Date(), nbMinutes)
const taskConfigs = await getAllStelaceTasks()
const filteredTaskConfigs = filterTasks(taskConfigs, refDate, nbMinutes)
fetchEventsTransaction.end()
fetchEventsTransaction = null // set null to prevent stopping a second time in the finally block
for (let i = 0; i < filteredTaskConfigs.length; i++) {
const taskConfig = filteredTaskConfigs[i]
const { platformId, env, task } = taskConfig
async function authenticationCallback (data) {
const name = 'Socket: Signal authentication'
apm.startTransaction(name)
const rawApiKey = _.get(data, 'publishableKey')
// channels can be array of strings or single string
const channels = _.flatten([_.get(data, 'channels', '')])
const authToken = _.get(data, 'authToken')
const organizationIds = [] // to be extracted from channels
const userIds = [] // there should be a single one at most
let otherChannels = []
let platformId
let env
let userId
let hasInternalUserId
const disconnect = reason => {
disconnectReason = reason
await bluebird.mapSeries(workflows, async workflow => {
if (_.isEmpty(workflow.run)) return
const singleWorkflowTransaction = apm.startTransaction('Execute Workflow', 'workflow')
singleWorkflowTransaction.action = workflow.name
apm.setUserContext({ id: platformId })
apm.addLabels({ env, platformId, eventType: event.type })
apm.setCustomContext({ workflowId: workflow.id })
apmSpans.fetchStats = apm.startSpan('Update nbTimesRun')
runId = Uuid.v4()
currentWorkflowId = workflow.id
const initialComputedScript = _getComputedValuesScript(workflow.computed, { reset: true })
const lastResponses = [] // array of responses
const responses = {} // step name -> response
const knex = Workflow.knex()
await Workflow.query().where('id', currentWorkflowId).patch({
try {
// use ref date because cron job cannot trigger at the specified time (with 0 millisecond)
const refDate = getRoundedDate(new Date(), nbMinutes)
const taskConfigs = await getAllStelaceTasks()
const filteredTaskConfigs = filterTasks(taskConfigs, refDate, nbMinutes)
fetchEventsTransaction.end()
fetchEventsTransaction = null // set null to prevent stopping a second time in the finally block
for (let i = 0; i < filteredTaskConfigs.length; i++) {
const taskConfig = filteredTaskConfigs[i]
const { platformId, env, task } = taskConfig
const emitEventTransaction = apm.startTransaction('Emit task event via cron')
apm.setUserContext({ id: platformId })
apm.addLabels({ env, platformId, eventType: task.eventType })
apm.setCustomContext({ taskId: task.id })
try {
// use redlock to ensure the cron process is handled only by one server at a time
// even within a distributed system
const lockResource = `locks:stelace_tasks:${task.id}_${refDate}`
const lock = await redlock.lock(lockResource, lockTtl)
const alreadyExecuted = await didStelaceTaskExecute({ taskId: task.id, executionDate: refDate })
if (!alreadyExecuted) {
await addStelaceTaskExecutionDate({ taskId: task.id, executionDate: refDate })
await emitTaskEvent({ platformId, env, task })
}