Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await reportStart(sdmGoal, progressLog);
const start = Date.now();
try {
const result = await executeGoal(
{
projectLoader: this.configuration.sdm.projectLoader,
goalExecutionListeners: this.goalExecutionListeners,
},
{
...implementation,
projectListeners: [...toArray(implementation.projectListeners || []), ...listeners],
},
goalInvocation);
const terminatingStates = [
SdmGoalState.canceled,
SdmGoalState.failure,
SdmGoalState.skipped,
SdmGoalState.stopped,
SdmGoalState.success,
SdmGoalState.waiting_for_approval,
];
if (!result || !result.state || terminatingStates.includes(result.state)) {
await reportEndAndClose(result, start, progressLog);
}
return {
...result,
// successfully handled event even if goal failed
code: 0,
};
} catch (e) {
e.message = `Goal executor threw exception: ${e.message}`;
const goalsToSkip = goals.filter(g => isDependentOn(failedGoal, g, mapKeyToGoal(goals)))
.filter(g => g.state === "planned");
let failedGoalState;
let failedGoalDescription;
switch (failedGoal.state) {
case SdmGoalState.failure:
failedGoalDescription = "failed";
failedGoalState = SdmGoalState.skipped;
break;
case SdmGoalState.stopped:
failedGoalDescription = "stopped goals";
failedGoalState = SdmGoalState.skipped;
break;
case SdmGoalState.canceled:
failedGoalDescription = "was canceled";
failedGoalState = SdmGoalState.canceled;
break;
}
await Promise.all(goalsToSkip.map(g => updateGoal(context, g, {
state: failedGoalState,
description: `${failedGoalState === SdmGoalState.skipped ? "Skipped" : "Canceled"
} ${g.name} because ${failedGoal.name} ${failedGoalDescription}`,
})));
return Success;
}
}
},
options: {
...QueryNoCacheOptions,
log: configurationValue("sdm.query.logging", false),
},
})).SdmGoal;
const state = !!options && !!options.state ? options.state : SdmGoalState.canceled;
for (const goal of gs) {
if (goal.ts < end) {
logger.debug(
`Canceling goal '${goal.uniqueName}' of goal set '${goal.goalSetId}' because it timed out after '${formatDuration(timeout)}'`);
let description = `${state === SdmGoalState.canceled ? "Canceled" : "Failed"}: ${goal.name}`;
if (!!goal.descriptions) {
if (state === SdmGoalState.canceled && !!goal.descriptions.canceled) {
description = goal.descriptions.canceled;
} else if (state === SdmGoalState.failure && !!goal.descriptions.failed) {
description = goal.descriptions.failed;
}
}
await updateGoal(
ctx,
goal as any,
{
state,
description,
phase: `${formatDuration(timeout)} timeout`,
});
}
}
}
? options.timeout
: _.get(sdm.configuration, "sdm.goal.inProcessTimeout", 1000 * 60 * 60);
const end = Date.now() - timeout;
const gs = (await ctx.graphClient.query({
name: "InProcessSdmGoals",
variables: {
registration: [sdm.configuration.name],
},
options: {
...QueryNoCacheOptions,
log: configurationValue("sdm.query.logging", false),
},
})).SdmGoal;
const state = !!options && !!options.state ? options.state : SdmGoalState.canceled;
for (const goal of gs) {
if (goal.ts < end) {
logger.debug(
`Canceling goal '${goal.uniqueName}' of goal set '${goal.goalSetId}' because it timed out after '${formatDuration(timeout)}'`);
let description = `${state === SdmGoalState.canceled ? "Canceled" : "Failed"}: ${goal.name}`;
if (!!goal.descriptions) {
if (state === SdmGoalState.canceled && !!goal.descriptions.canceled) {
description = goal.descriptions.canceled;
} else if (state === SdmGoalState.failure && !!goal.descriptions.failed) {
description = goal.descriptions.failed;
}
}
await updateGoal(
ctx,
goal as any,
.filter(g => g.state === "planned");
let failedGoalState;
let failedGoalDescription;
switch (failedGoal.state) {
case SdmGoalState.failure:
failedGoalDescription = "failed";
failedGoalState = SdmGoalState.skipped;
break;
case SdmGoalState.stopped:
failedGoalDescription = "stopped goals";
failedGoalState = SdmGoalState.skipped;
break;
case SdmGoalState.canceled:
failedGoalDescription = "was canceled";
failedGoalState = SdmGoalState.canceled;
break;
}
await Promise.all(goalsToSkip.map(g => updateGoal(context, g, {
state: failedGoalState,
description: `${failedGoalState === SdmGoalState.skipped ? "Skipped" : "Canceled"
} ${g.name} because ${failedGoal.name} ${failedGoalDescription}`,
})));
return Success;
}
}
variables: {
registration: [sdm.configuration.name],
},
options: {
...QueryNoCacheOptions,
log: configurationValue("sdm.query.logging", false),
},
})).SdmGoal;
const state = !!options && !!options.state ? options.state : SdmGoalState.canceled;
for (const goal of gs) {
if (goal.ts < end) {
logger.debug(
`Canceling goal '${goal.uniqueName}' of goal set '${goal.goalSetId}' because it timed out after '${formatDuration(timeout)}'`);
let description = `${state === SdmGoalState.canceled ? "Canceled" : "Failed"}: ${goal.name}`;
if (!!goal.descriptions) {
if (state === SdmGoalState.canceled && !!goal.descriptions.canceled) {
description = goal.descriptions.canceled;
} else if (state === SdmGoalState.failure && !!goal.descriptions.failed) {
description = goal.descriptions.failed;
}
}
await updateGoal(
ctx,
goal as any,
{
state,
description,
phase: `${formatDuration(timeout)} timeout`,
});
}
return async gi => {
const goalEvent = gi.completedGoal;
if (goalEvent.state === SdmGoalState.in_process) {
return;
}
if (goalEvent.state === SdmGoalState.canceled && !(await cancelableGoal(goalEvent, gi.configuration))) {
return;
}
const selector = `atomist.com/goal-set-id=${goalEvent.goalSetId},atomist.com/creator=${sanitizeName(this.sdm.configuration.name)}`;
let jobs;
try {
jobs = await listJobs(selector);
} catch (e) {
logger.warn(`Failed to read k8s jobs: ${prettyPrintError(e)}`);
return;
}
logger.debug(
`Found k8s jobs for goal set '${goalEvent.goalSetId}': '${
jobs.map(j => `${j.metadata.namespace}:${j.metadata.name}`).join(", ")}'`);