Skip to content

Commit

Permalink
Merge pull request #631 from atomist/dynamic-goals
Browse files Browse the repository at this point in the history
Dynamic goals
  • Loading branch information
cdupuis authored Dec 6, 2019
2 parents 43641c1 + dc0c688 commit 3d386bf
Show file tree
Hide file tree
Showing 12 changed files with 33,951 additions and 18,143 deletions.
3 changes: 2 additions & 1 deletion bin/start-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ async function main(): Promise<void> {
const registration = prepareRegistration(cfg);

const chokidar = require("chokidar");
const watcher = chokidar.watch(["index.js", "lib/*.js", "lib/**/*.js"], { ignored: "\.ts" });
const additionalFilesToWatch = (process.env.ATOMIST_WATCH_PATHS || "").split(",");
const watcher = chokidar.watch(["index.js", "lib/*.js", "lib/**/*.js", ...additionalFilesToWatch], { ignored: "\.ts" });

const indexPath = path.join(appRoot.path, "index.js");
const libPath = path.join(appRoot.path, "lib");
Expand Down
26 changes: 20 additions & 6 deletions lib/automationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class AutomationClient implements RequestProcessor {
public httpServer: ExpressServer;
public webSocketHandler: RequestProcessor;
public httpHandler: RequestProcessor;
public requestProcessor: RequestProcessor;

private readonly defaultListeners: AutomationEventListener[] = [
new MetricEnabledAutomationEventListener(),
Expand All @@ -59,7 +60,8 @@ export class AutomationClient implements RequestProcessor {
new StartupTimeMessageUatomationEventListener(),
];

constructor(public configuration: Configuration) {
constructor(public configuration: Configuration,
public requestProcessorMaker?: (automations: AutomationServer, configuration: Configuration, listeners: AutomationEventListener[]) => RequestProcessor) {
this.automations = new BuildableAutomationServer(configuration);
(global as any).__runningAutomationClient = this as AutomationClient;
}
Expand All @@ -84,7 +86,9 @@ export class AutomationClient implements RequestProcessor {
}

public processCommand(command: CommandIncoming, callback?: (result: Promise<HandlerResult>) => void): void {
if (this.webSocketHandler) {
if (this.requestProcessor) {
return this.requestProcessor.processCommand(command, callback);
} else if (this.webSocketHandler) {
return this.webSocketHandler.processCommand(command, callback);
} else if (this.httpHandler) {
return this.httpHandler.processCommand(command, callback);
Expand All @@ -94,7 +98,9 @@ export class AutomationClient implements RequestProcessor {
}

public processEvent(event: EventIncoming, callback?: (results: Promise<HandlerResult[]>) => void): void {
if (this.webSocketHandler) {
if (this.requestProcessor) {
return this.requestProcessor.processEvent(event, callback);
} else if (this.webSocketHandler) {
return this.webSocketHandler.processEvent(event, callback);
} else if (this.httpHandler) {
return this.httpHandler.processEvent(event, callback);
Expand All @@ -112,6 +118,13 @@ export class AutomationClient implements RequestProcessor {
const clientSig = `${this.configuration.name}:${this.configuration.version}`;
const clientConf = stringify(this.configuration, obfuscateJson);

if (!!this.requestProcessorMaker) {
this.requestProcessor = this.requestProcessorMaker(
this.automations,
this.configuration,
[...this.defaultListeners, ...this.configuration.listeners]);
}

if (!this.configuration.cluster.enabled) {
logger.info(`Starting Atomist automation client ${clientSig}`);
logger.debug(`Using automation client configuration: ${clientConf}`);
Expand Down Expand Up @@ -235,7 +248,7 @@ export class AutomationClient implements RequestProcessor {

private runHttp(handlerMaker: () => ExpressRequestProcessor): Promise<any> {
if (!this.configuration.http.enabled) {
return;
return Promise.resolve();
}

this.httpHandler = handlerMaker();
Expand All @@ -248,8 +261,9 @@ export class AutomationClient implements RequestProcessor {
}
}

export function automationClient(configuration: Configuration): AutomationClient {
const client = new AutomationClient(configuration);
export function automationClient(configuration: Configuration,
requestProcessorMaker?: (automations: AutomationServer, configuration: Configuration, listeners: AutomationEventListener[]) => RequestProcessor): AutomationClient {
const client = new AutomationClient(configuration, requestProcessorMaker);
configuration.commands.forEach(c => {
client.withCommandHandler(c);
});
Expand Down
9 changes: 6 additions & 3 deletions lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,18 +961,21 @@ export function validateConfiguration(cfg: Configuration): void {
* is modified to contain the final configuration values and returned
* from this function.
*
* @param cfgPath path to file exporting the configuration object, if
* @param base path to file exporting the configuration object, if
* not provided the package is searched for one
* @return merged configuration object
*/
export async function loadConfiguration(cfgPath?: string): Promise<Configuration> {
export async function loadConfiguration(base?: string | Promise<Configuration>): Promise<Configuration> {
// Register the logger globally so that downstream modules can see it
(global as any).__logger = logger;

const cfgPath = typeof base === "string" ? base : undefined;
const cfgBase = typeof base !== "string" ? base : undefined;

let cfg: Configuration;
try {
const defCfg = defaultConfiguration();
const autoCfg = (await loadAutomationConfig(cfgPath)) || (await loadIndexConfig());
const autoCfg = (await cfgBase) || (await loadAutomationConfig(cfgPath)) || (await loadIndexConfig());
const userCfg = loadUserConfiguration(defCfg.name, defCfg.version);
const atmPathCfg = loadAtomistConfigPath();
const atmCfg = loadAtomistConfig();
Expand Down
4 changes: 2 additions & 2 deletions lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AutomationClient } from "./automationClient";
import { InMemoryEventStore } from "./internal/event/InMemoryEventStore";
import { NoOpEventStore } from "./internal/event/NoOpEventStore";
import { EventStore } from "./spi/event/EventStore";

////////////////////////////////////////////////////////
let es: EventStore;

function initEventStore(): void {
if (!es) {
es = new InMemoryEventStore();
es = new NoOpEventStore();
}
}

Expand Down
Loading

0 comments on commit 3d386bf

Please sign in to comment.