@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
149 lines • 7.49 kB
JavaScript
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const WebSocketMessageClient_1 = require("@atomist/automation-client/lib/internal/transport/websocket/WebSocketMessageClient");
const namespace = require("@atomist/automation-client/lib/internal/util/cls");
const sdm_1 = require("@atomist/sdm");
const _ = require("lodash");
const time_1 = require("../../util/misc/time");
const cancelGoals_1 = require("./cancelGoals");
/**
* TriggeredListener that queries pending goal sets and updates their state according to state of
* goals
*/
function manageGoalSetsTrigger(options) {
return (li) => __awaiter(this, void 0, void 0, function* () {
const workspaceIds = li.sdm.configuration.workspaceIds;
if (!!workspaceIds && workspaceIds.length > 0) {
for (const workspaceId of workspaceIds) {
const ses = namespace.create();
ses.run(() => __awaiter(this, void 0, void 0, function* () {
const id = automation_client_1.guid();
namespace.set({
invocationId: id,
correlationId: id,
workspaceName: workspaceId,
workspaceId,
operation: "ManagePendingGoalSets",
ts: Date.now(),
name: li.sdm.configuration.name,
version: li.sdm.configuration.version,
});
try {
const graphClient = li.sdm.configuration.graphql.client.factory.create(workspaceId, li.sdm.configuration);
const messageClient = new TriggeredMessageClient(li.sdm.configuration.ws.lifecycle, workspaceId, li.sdm.configuration);
const ctx = {
graphClient,
messageClient,
workspaceId,
correlationId: id,
invocationId: id,
context: {
name: li.sdm.configuration.name,
version: li.sdm.configuration.version,
operation: "ManagePendingGoalSets",
ts: Date.now(),
workspaceId,
workspaceName: workspaceId,
correlationId: id,
invocationId: id,
},
};
yield manageGoalSets(li.sdm, ctx);
yield timeoutInProcessGoals(li.sdm, ctx, options);
}
catch (e) {
automation_client_1.logger.debug("Error managing pending goal sets: %s", e.stack);
}
}));
}
}
});
}
exports.manageGoalSetsTrigger = manageGoalSetsTrigger;
function manageGoalSets(sdm, ctx) {
return __awaiter(this, void 0, void 0, function* () {
const pgs = yield cancelGoals_1.pendingGoalSets(ctx, sdm.configuration.name, 0, 100);
for (const goalSet of pgs) {
const goals = yield sdm_1.fetchGoalsForCommit(ctx, {
owner: goalSet.repo.owner,
repo: goalSet.repo.name,
sha: goalSet.sha,
branch: goalSet.branch,
}, goalSet.repo.providerId, goalSet.goalSetId);
const state = sdm_1.goalSetState(goals || []);
if (state !== goalSet.state) {
const newGoalSet = Object.assign(Object.assign({}, goalSet), { state });
automation_client_1.logger.debug(`Goal set '${goalSet.goalSetId}' now in state '${state}'`);
yield sdm_1.storeGoalSet(ctx, newGoalSet);
}
}
});
}
exports.manageGoalSets = manageGoalSets;
function timeoutInProcessGoals(sdm, ctx, options) {
return __awaiter(this, void 0, void 0, function* () {
const timeout = !!options && !!options.timeout
? options.timeout
: _.get(sdm.configuration, "sdm.goal.inProcessTimeout", 1000 * 60 * 60);
const end = Date.now() - timeout;
const gs = (yield ctx.graphClient.query({
name: "InProcessSdmGoals",
variables: {
registration: [sdm.configuration.name],
},
options: Object.assign(Object.assign({}, automation_client_1.QueryNoCacheOptions), { log: automation_client_1.configurationValue("sdm.query.logging", false) }),
})).SdmGoal;
const state = !!options && !!options.state ? options.state : sdm_1.SdmGoalState.canceled;
for (const goal of gs) {
if (goal.ts < end) {
automation_client_1.logger.debug(`Canceling goal '${goal.uniqueName}' of goal set '${goal.goalSetId}' because it timed out after '${time_1.formatDuration(timeout)}'`);
let description = `${state === sdm_1.SdmGoalState.canceled ? "Canceled" : "Failed"}: ${goal.name}`;
if (!!goal.descriptions) {
if (state === sdm_1.SdmGoalState.canceled && !!goal.descriptions.canceled) {
description = goal.descriptions.canceled;
}
else if (state === sdm_1.SdmGoalState.failure && !!goal.descriptions.failed) {
description = goal.descriptions.failed;
}
}
yield sdm_1.updateGoal(ctx, goal, {
state,
description,
phase: `${time_1.formatDuration(timeout)} timeout`,
});
}
}
});
}
exports.timeoutInProcessGoals = timeoutInProcessGoals;
class TriggeredMessageClient extends WebSocketMessageClient_1.AbstractWebSocketMessageClient {
constructor(ws, workspaceId, configuration) {
super(ws, {}, automation_client_1.guid(), { id: workspaceId }, {}, configuration);
}
}
//# sourceMappingURL=manageGoalSets.js.map
;