Skip to content

workflow

axios edited this page Sep 21, 2021 · 5 revisions

Get details from the code.

Usage

  • operator.js
'use strict';

async function a(context) {
  console.log('a');
  // return 'c'; // will skip step 'b'
}

async function b(context) {
  console.log('b');
}

async function c(context) {
  console.log('c');
}

module.exports = {
  a, b, c
};
  • work.js
'use strict';

const operator = require('./operator');
const { Workflow } = require('@axiosleo/cli-tool');

const workflow = new Workflow(operator,['a', 'b', 'c']);
const context = {};
workflow.start(context).then((context) => {
  console.log(context);
}).catch((context) => {
  console.log(context);
});
node work.js

Context demo

  • success
context = {
  success: true,
  curr: {},
  step_data: {
    a: {
      workflow: 'a',
      start: 1611663346927,
      end: 1611663346936,
      success: true
    },
    b: {
      workflow: 'b',
      start: 1611663346936,
      end: 1611663346936,
      success: true
    },
    c: {
      workflow: 'c',
      start: 1611663346936,
      end: 1611663346936,
      success: true
    }
  }
}
  • failed
context = {
  success: false,
  curr: {
    workflow: 'error',
    error_step: 'c',
    start: 1611664311892,
    end: 1611664311892,
    error: Error instance
  },
  step_data: {
    a: {
      workflow: 'a',
      start: 1611664311880,
      end: 1611664311891,
      success: true
    },
    b: {
      workflow: 'b',
      start: 1611664311891,
      end: 1611664311891,
      success: true
    },
    c: {
      workflow: 'c',
      start: 1611664311891,
      end: 1611664311892,
      success: false,
      error: Error instance
    }
  }
}

Example

const { Workflow } = require('@axiosleo/cli-tool');

const workflow = new Workflow({
  a: async function () { },
  b: async function () { }
});

const context = {};

workflow.start(context).then((context) => {
  console.log(context);
}).catch((context) => {
  console.log(context);
});

Example For Typescript

import {
  Workflow,
  Context
} from '@axiosleo/cli-tool';

export interface CustomContext extends Context {
  some_field: string;
}
const workflow = new Workflow<CustomContext>({
  a: async (context: CustomContext): Promise<void> => {
    console.log('this is step a', context);
  },
  b: async (context: CustomContext) => {
    console.log('this is step b', context);
  }
});

const context = {
  some_field: '',
  curr: {}
};

workflow.start(context).then((context: CustomContext) => {
  console.log(context);
}).catch((context: CustomContext) => {
  console.log(context);
});
Clone this wiki locally