/
goalState.ts
82 lines (78 loc) · 3.12 KB
/
goalState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright © 2020 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.
*/
import { logger } from "@atomist/automation-client/lib/util/logger";
import * as cluster from "cluster";
import * as _ from "lodash";
import { metadata } from "../../api-helper/misc/extensionPack";
import { ExtensionPack } from "../../api/machine/ExtensionPack";
import { isInLocalMode } from "../../core/machine/modes";
import { SdmGoalState } from "../../typings/types";
import { cancelGoalSetsCommand, listPendingGoalSetsCommand } from "./cancelGoals";
import { manageGoalSetsTrigger } from "./manageGoalSets";
import { resetGoalsCommand } from "./resetGoals";
import { setGoalStateCommand } from "./setGoalState";
/**
* Configuration options for the goal state support
*/
export interface GoalStateOptions {
/** Configure the goal cancellation support */
cancellation?: {
/** Enable goal cancellation based on timeouts */
enabled?: boolean;
/**
* Optionally set the timeout after which goals should be cancelled.
* Defaults to 1 hour.
*/
timeout?: number;
/**
* Optional state the goal should be set to when it times out
* Defaults to SdmGoalState.canceled
*/
state?: SdmGoalState.canceled | SdmGoalState.failure;
};
}
/**
* Allow goal setting
*/
export function goalStateSupport(options?: GoalStateOptions): ExtensionPack {
return {
...metadata("goal-state"),
configure: sdm => {
if (isInLocalMode()) {
logger.warn("Setting goal state is not available in local mode.");
logger.warn("Resetting goals does not work in local mode. Use `atomist trigger post-commit` instead.");
} else {
sdm.addCommand(setGoalStateCommand(sdm));
sdm.addCommand(resetGoalsCommand(sdm));
sdm.addCommand(cancelGoalSetsCommand(sdm));
sdm.addCommand(listPendingGoalSetsCommand(sdm));
if (
(cluster.isMaster || !_.get(sdm.configuration, "cluster.enabled")) &&
!process.env.ATOMIST_ISOLATED_GOAL &&
!!options &&
!!options.cancellation &&
!!options.cancellation.enabled
) {
logger.debug(`Timeout based goal cancellation enabled for this SDM`);
sdm.addTriggeredListener({
trigger: { interval: 1000 * 30 },
listener: manageGoalSetsTrigger(options.cancellation),
});
}
}
},
};
}