Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #125 from mesg-foundation/feature/workflow-resolve…
Browse files Browse the repository at this point in the history
…-sid

Service resolution for the workflow
  • Loading branch information
antho1404 authored Aug 21, 2019
2 parents 9335976 + 0f51676 commit 7572a1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
30 changes: 29 additions & 1 deletion src/commands/workflow/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {readFileSync} from 'fs'

import Command from '../../root-command'
import * as compile from '../../utils/compiler'
import ServiceStart from '../service/start';

export default class WorkflowCompile extends Command {
static description = 'Compile a workflow'
Expand All @@ -17,9 +18,36 @@ export default class WorkflowCompile extends Command {

async run(): Promise<any> {
const {args} = this.parse(WorkflowCompile)
const definition = await compile.workflow(readFileSync(args.WORKFLOW_FILE))
const definition = await compile.workflow(readFileSync(args.WORKFLOW_FILE), async (instanceObject: any) => {
if (instanceObject.instanceHash) {
return instanceObject.instanceHash
}
if (instanceObject.service) {
return this.serviceToInstance(instanceObject.service)
}
throw new Error('at least one of the following parameter should be set: "instanceHash" or "service"')
})
this.styledJSON(definition)
this.spinner.stop()
return definition
}

async serviceToInstance(key: string): Promise<string> {
const {services} = await this.api.service.list({})
if (!services) throw new Error('no services deployed, please deploy your service first')
const match = services.filter(x => x.hash === key || x.sid === key)
if (!match || match.length === 0) throw new Error(`cannot find any service with the following: ${key}`)
if (match.length > 1) throw new Error(`multiple services match the following sid: ${key}, provide a service's hash instead`)
const service = match[0]
if (!service.hash) throw new Error('invalid service')
const {instances} = await this.api.instance.list({serviceHash: service.hash})
if (!instances || instances.length === 0) {
const instance = await ServiceStart.run([service.hash, '--silent'])
return instance.hash
}
if (instances.length > 1) throw new Error('multiple instances match the service, use parameter "instanceHash" instead of "service"')
const instance = instances[0]
if (!instance.hash) throw new Error('invalid instance')
return instance.hash
}
}
10 changes: 3 additions & 7 deletions src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const parseParams = (params: any): any => mapToArray(params).map(x => ({
object: parseParams(x.object),
}))

const prepareInstanceHash = async (object: any): Promise<string> => {
return object.instanceHash
}

export const service = async (content: Buffer): Promise<Service> => {
const definition = decode(content)
return {
Expand All @@ -36,17 +32,17 @@ export const service = async (content: Buffer): Promise<Service> => {
}
}

export const workflow = async (content: Buffer): Promise<Workflow> => {
export const workflow = async (content: Buffer, instanceResolver: (object: any) => Promise<string>): Promise<Workflow> => {
const definition = decode(content)
const createNode = async (def: any, index: number): Promise<WorkflowType.types.Workflow.INode> => ({
key: def.key || `node-${index}`,
taskKey: def.taskKey,
instanceHash: await prepareInstanceHash(def)
instanceHash: await instanceResolver(def)
})
const orderedNodes = await Promise.all(definition.tasks.map(createNode)) as WorkflowType.types.Workflow.INode[]

const trigger = {
instanceHash: await prepareInstanceHash(definition.trigger),
instanceHash: await instanceResolver(definition.trigger),
taskKey: definition.trigger.taskKey,
eventKey: definition.trigger.eventKey,
nodeKey: orderedNodes[0].key
Expand Down

0 comments on commit 7572a1f

Please sign in to comment.