Skip to content
Permalink
Browse files

Add services to a FulfillableGoal

[changelog:added]
  • Loading branch information...
cdupuis committed Jan 19, 2019
1 parent e2dc578 commit e5e09df8ed5ed1290620ec47466e53767de47b1f
Showing with 27 additions and 0 deletions.
  1. +27 −0 lib/api/goal/GoalWithFulfillment.ts
@@ -19,6 +19,7 @@ import * as _ from "lodash";
import { LogSuppressor } from "../../api-helper/log/logInterpreters";
import { AbstractSoftwareDeliveryMachine } from "../../api-helper/machine/AbstractSoftwareDeliveryMachine";
import { InterpretLog } from "../../spi/log/InterpretedLog";
import { RepoContext } from "../context/SdmContext";
import { GoalExecutionListener } from "../listener/GoalStatusListener";
import {
Registerable,
@@ -43,6 +44,7 @@ import {
} from "./GoalInvocation";
import { DefaultGoalNameGenerator } from "./GoalNameGenerator";
import { ReportProgress } from "./progress/ReportProgress";
import { SdmGoalEvent } from "./SdmGoalEvent";
import {
GoalEnvironment,
IndependentOfEnvironment,
@@ -139,6 +141,15 @@ export interface PredicatedGoalDefinition extends GoalDefinition {
preCondition?: WaitRules;
}

/**
* Register additional services for a goal.
* This can be used to add additional containers into k8s jobs to use during goal execution.
*/
export interface ServiceRegistration<T> {
name: string;
service: (goalEvent: SdmGoalEvent, repo: RepoContext) => Promise<{ type: string, spec: T }>;
}

/**
* Goal that registers goal implementations, side effects and callbacks on the
* current SDM. No additional registration with the SDM is needed.
@@ -182,6 +193,22 @@ export abstract class FulfillableGoal extends GoalWithPrecondition implements Re
return this;
}

public withService(registration: ServiceRegistration<any>): this {
this.addFulfillmentCallback({
goal: this,
callback: async (goalEvent, repoContext) => {
const service = await registration.service(goalEvent, repoContext);
const data = JSON.parse(goalEvent.data || "{}");
const servicesData = {
services: {},
};
servicesData.services[registration.name] = JSON.stringify(service);
goalEvent.data = _.merge(data, servicesData);
return goalEvent;
},
});
}

protected addFulfillmentCallback(cb: GoalFulfillmentCallback): this {
if (this.sdm) {
this.registerCallback(cb);

0 comments on commit e5e09df

Please sign in to comment.
You can’t perform that action at this time.