UNPKG

3.14 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { logger } from "@atomist/automation-client";
18import {
19 ExtensionPack,
20 metadata,
21 SdmGoalState,
22} from "@atomist/sdm";
23import * as cluster from "cluster";
24import * as _ from "lodash";
25import { isInLocalMode } from "../../internal/machine/modes";
26import {
27 cancelGoalSetsCommand,
28 listPendingGoalSetsCommand,
29} from "./cancelGoals";
30import { manageGoalSetsTrigger } from "./manageGoalSets";
31import { resetGoalsCommand } from "./resetGoals";
32import { setGoalStateCommand } from "./setGoalState";
33
34/**
35 * Configuration options for the goal state support
36 */
37export interface GoalStateOptions {
38 /** Configure the goal cancellation support */
39 cancellation?: {
40 /** Enable goal cancellation based on timeouts */
41 enabled?: boolean;
42 /**
43 * Optionally set the timeout after which goals should be cancelled.
44 * Defaults to 1 hour.
45 */
46 timeout?: number;
47 /**
48 * Optional state the goal should be set to when it times out
49 * Defaults to SdmGoalState.canceled
50 */
51 state?: SdmGoalState.canceled | SdmGoalState.failure;
52 };
53}
54
55/**
56 * Allow goal setting
57 */
58export function goalStateSupport(options?: GoalStateOptions): ExtensionPack {
59 return {
60 ...metadata("goal-state"),
61 configure: sdm => {
62 if (isInLocalMode()) {
63 logger.warn("Setting goal state is not available in local mode.");
64 logger.warn("Resetting goals does not work in local mode. Use `atomist trigger post-commit` instead.");
65 } else {
66 sdm.addCommand(setGoalStateCommand(sdm));
67 sdm.addCommand(resetGoalsCommand(sdm));
68 sdm.addCommand(cancelGoalSetsCommand(sdm));
69 sdm.addCommand(listPendingGoalSetsCommand(sdm));
70
71 if ((cluster.isMaster || !_.get(sdm.configuration, "cluster.enabled")) &&
72 !process.env.ATOMIST_ISOLATED_GOAL &&
73 !!options && !!options.cancellation && !!options.cancellation.enabled) {
74 logger.debug(`Timeout based goal cancellation enabled for this SDM`);
75 sdm.addTriggeredListener({
76 trigger: { interval: 1000 * 30 },
77 listener: manageGoalSetsTrigger(options.cancellation),
78 });
79 }
80 }
81 },
82 };
83}
84
85/**
86 * @deprecated use goalStateSupport
87 */
88export function goalState(): ExtensionPack {
89 return goalStateSupport();
90}