chore(scaffolder): work on a better api for holding stages and metadata

This commit is contained in:
blam
2020-06-26 21:27:48 +02:00
parent ce43dce8ff
commit ad2354909e
18 changed files with 36 additions and 26 deletions
@@ -79,6 +79,7 @@ describe('JobProcessor', () => {
const createJob = (): { job: Job; processor: JobProcessor } => {
preparers.register('github', mockPreparer);
new JobProcessor(1);
const processor = new JobProcessor({
preparers,
dockerClient: mockDocker,
@@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Processor, Job } from './types';
import { Processor, Job, Stage, StageContext } from './types';
import { JsonValue } from '@backstage/config';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { PassThrough } from 'stream';
import * as uuid from 'uuid';
import Docker from 'dockerode';
import winston from 'winston';
import { RequiredTemplateValues, TemplaterBase } from '../templater';
import { RequiredTemplateValues, TemplaterBase } from '../stages/templater';
import { createNewRootLogger } from '@backstage/backend-common';
import { PreparerBuilder } from '../prepare';
import { PreparerBuilder } from '../stages/prepare';
export type JobProcessorArguments = {
preparers: PreparerBuilder;
@@ -36,15 +36,10 @@ export type JobAndDirectoryTuple = {
};
export class JobProcessor implements Processor {
private preparers: PreparerBuilder;
private templater: TemplaterBase;
private dockerClient: Docker;
private jobs = new Map<string, Job>();
constructor({ preparers, templater, dockerClient }: JobProcessorArguments) {
this.preparers = preparers;
this.templater = templater;
this.dockerClient = dockerClient;
private stages: Stage[];
constructor({ stages }: { stages: Stage[] }) {
this.stages = stages;
}
create(
@@ -66,11 +61,17 @@ export class JobProcessor implements Processor {
const logger = createNewRootLogger();
logger.add(new winston.transports.Stream({ stream: logStream }));
const context: StageContext = {
entity,
values,
logger,
};
const job: Job = {
id,
logStream,
logger,
log,
context,
stages: this.stages.map((stage) => ({}))
status: 'PENDING',
metadata: {
entity,
@@ -16,13 +16,13 @@
import type { Writable } from 'stream';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { JsonValue } from '@backstage/config';
import { RequiredTemplateValues } from '../templater';
import { RequiredTemplateValues } from '../stages/templater';
import { Logger } from 'winston';
// Context will be a mutable object which is passed between stages
// To share data, but also thinking that we can pass in functions here too
// To maybe create sub steps or fail the entire thing, or skip stages down the line.
export type StageContext<T> = T & {
export type StageContext<T = {}> = T & {
values: RequiredTemplateValues & Record<string, JsonValue>;
entity: TemplateEntityV1alpha1;
logger: Logger;
@@ -35,12 +35,9 @@ export type Stage<T = {}> = {
handler: (ctx: StageContext<T>) => Promise<void>;
};
export type Job = {
export type Job<T = {}> = {
id: string;
metadata: {
entity: TemplateEntityV1alpha1;
values: RequiredTemplateValues & Record<string, JsonValue>;
};
context: StageContext<T>;
status: 'PENDING' | 'STARTED' | 'COMPLETE' | 'FAILED';
stages: Stage[];
logStream: Writable;
@@ -48,11 +45,22 @@ export type Job = {
error?: Error;
};
export type Processor = {
create(
entity: TemplateEntityV1alpha1,
values: RequiredTemplateValues & Record<string, JsonValue>,
): Job;
export interface ProcessorConstructor {
new (t: string): Processor;
}
export interface Processor {
create<T = {}>({
entity,
values,
stages,
}: {
entity: TemplateEntityV1alpha1;
values: RequiredTemplateValues & Record<string, JsonValue>;
stages: Stage[];
}): Job<T>;
get(id: string): Job | undefined;
};
}
declare let Processor: ProcessorConstructor;