BzTasks

BzTasks

Beelzebub Task Class, should be extended

Constructor

new BzTasks()

Source:

Methods

$addSubTasks(Task, configopt) → {object}

Source:

Add Sub Tasks

Example
  let Beelzebub = require('../../');
  let bz = Beelzebub(options || { verbose: true });

  class MyBaseTasks extends Beelzebub.Tasks {
    constructor (config) {
      super(config);
      this.$setName(config.name || 'MyBaseTasks');

      this.value = config.value;
      this._delayTime = 300;
    }

    $init () {
      return this._delay('MyBaseTasks init');
    }

    _delay (message) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.logger.log(message);
          resolve();
        }, this._delayTime);
      });
    }

    task1 () {
      return this._delay('MyBaseTasks task1 - ' + this.value);
    }
  }

  class MyTasks extends Beelzebub.Tasks {
    constructor (config) {
      super(config);
      this.$setName('MyTasks');
    }

    $init () {
      this.logger.log('MyTasks init');
      // simlate tasks dynamiclly added after some async event
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.$addSubTasks(MyBaseTasks, { name: 'MyBaseTasks1', value: 123 });
          this.$addSubTasks(MyBaseTasks, { name: 'MyBaseTasks2', value: 456 });
          // done
          resolve(1234);
        }, 200);
      });
    }

    task1 () {
      this.logger.log('MyTasks task1');
      return this.$sequence('MyTasks.MyBaseTasks1.task1', 'MyTasks.MyBaseTasks2.task1');
    }
  }

  bz.add(MyTasks);
  let p = bz.run('MyTasks.task1');
/* Output:
MyTasks init
MyBaseTasks init
MyBaseTasks init
MyTasks task1
MyBaseTasks task1 - 123
MyBaseTasks task1 - 456
*/
Parameters:
Name Type Attributes Default Description
Task object

Task Class

config object <optional>
{}

Config for Task

Returns:

Promise

Type
object

$defineTaskVars(taskName, taskDef)

Source:

Define Task Vars

Example
  const Beelzebub = require('../../');
  const bz = Beelzebub(options || { verbose: true });
  const task = Beelzebub.TmplStrFunc.task;

  class MyTasks extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$defineTaskVars('task1', {
        name: { type: 'String', default: 'hello' },
        flag: { type: 'Boolean', default: true }
      });
      this.$defineTaskVars('task2', {
        count:   { type: 'Number', required: true },
        verbose: { type: 'Boolean', alias: 'v', default: false }
      });
      this.$defineTaskVars('task3', {
        fullname: {
          type:       'Object',
          properties: {
            first: { type: 'String' },
            last:  { type: 'String' }
          }
        },
        list: {
          type:  'Array',
          items: { type: 'String' }
        }
      });
    }

    task1 (customVars) {
      this.logger.log(`MyTasks task1 - ${JSON.stringify(this.$getGlobalVars())} ${customVars.name} ${customVars.flag}`);
    }

    task2 (customVars) {
      this.logger.log(`MyTasks task2 - ${customVars.count} ${customVars.verbose}`);
    }

    task3 (customVars) {
      this.logger.log(`MyTasks task3 - "${customVars.fullname.first} ${customVars.fullname.last}" ${customVars.list}`);
    }
  }

  bz.add(MyTasks);

  let p = bz.run(
    'MyTasks.task1',
    task`MyTasks.task2:${{count: 100, verbose: true}}`,
    {
      task: 'MyTasks.task3',
      vars: {
        fullname: { first: 'hello', last: 'world' },
        list:     [ 'te', 'st' ]
      }
    }
  );
/* Output:
MyTasks task1 - {} hello true
MyTasks task2 - 100 true
MyTasks task3 - "hello world" te,st
*/
Parameters:
Name Type Description
taskName string

Name of Task

taskDef object

Var Defintion for Task

$getGlobalVars() → {object}

Source:

Get Global Vars

Returns:
Type
object

$getName() → {string}

Source:

Get name of Task Group/Class

Returns:
Type
string

$getRunning() → {boolean}

Source:

Is Task Running?

Returns:
Type
boolean

$getStatsSummary() → {object}

Source:

Get task status and all it's sub tasks stats

Returns:
Type
object

$getSubTask(name) → {object}

Source:

Get SubTask by Name

Parameters:
Name Type Description
name string

Name of Sub Task

Returns:
Type
object

$getSubTasks() → {object}

Source:

Get All Sub Task(s)

Returns:
Type
object

$getTask(name) → {function}

Source:

Get Task by Name

Parameters:
Name Type Description
name string

Name of Task to get

Returns:
Type
function

$getTaskFlatList() → {object}

Source:

Get flatten task tree so it's one level

Returns:
Type
object

$getTaskTree() → {object}

Source:

Get Task Tree starting with this task

Returns:
Type
object

$getVarDefsForTaskName(taskStr) → {object}

Source:

Get Define Task Vars by Name

Parameters:
Name Type Description
taskStr string

Name of Task

Returns:

Varaible Definition for Task

Type
object

$hasRunBefore() → {boolean}

Source:

Has any of the tasks ran before?

Returns:
Type
boolean

$hasSubTask(name) → {boolean}

Source:

Does this have a Sub Task with the name?

Parameters:
Name Type Description
name string

Name of Sub Task

Returns:
Type
boolean

$hasTask(name) → {boolean}

Source:

Does this Task Group have a Task with the name?

Parameters:
Name Type Description
name string

Name of Task

Returns:
Type
boolean

$isRoot() → {boolean}

Source:

Is this task root level?

Returns:
Type
boolean

$parallel(…args) → {object}

Source:

Runs task(s) in parallel

Example
  let Beelzebub = require('../../');
  let bz = Beelzebub(options || { verbose: true });

  class MyTasks extends Beelzebub.Tasks {
    _delay (message, delay = 300) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.logger.log(message);
          resolve();
        }, delay);
      });
    }

    task1 () {
      this.logger.log('MyTasks task1');
      return this.$parallel('.task3', '.task4');
    }

    task2 () {
      return this._delay('MyTasks task2', 200);
    }

    task3 () {
      this.logger.log('MyTasks task3');
      return this.$run(['.task5', '.task6']);
    }

    task4 () {
      return this._delay('MyTasks task4', 400);
    }

    task5 () {
      this.logger.log('MyTasks task5');
    }

    task6 () {
      return this._delay('MyTasks task6', 600);
    }
}

  bz.add(MyTasks);
  // arrays are run in parallel
  let p = bz.run(['MyTasks.task1', 'MyTasks.task2']);
/* Output:
MyTasks task1
MyTasks task3
MyTasks task5
MyTasks task2
MyTasks task4
MyTasks task6
*/
Parameters:
Name Type Attributes Description
args function | string <repeatable>

task(s)

Returns:

Promise

Type
object

$printHelp()

Source:

Prints Task help and all sub tasks help

Example
  const Beelzebub = require('../../');
  const bz = Beelzebub(options || { verbose: true });

  class MyTasks extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('task1');

      this.$setTaskHelpDocs('task1', 'ES7 Decorator Example MyTasks - Task 1');
      this.$setTaskHelpDocs('task2', 'ES7 Decorator Example MyTasks - Task 2');
    }

    task1 () {
      this.logger.log('MyTasks task1');
    }

    task2 () {
      this.logger.log('MyTasks task2');
    }
  }
  bz.add(MyTasks);

  class MyTasks2 extends bz.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('task1');

      this.$setTaskHelpDocs('task1', 'ES7 Decorator Example MyTasks2 - Task 1');
      this.$setTaskHelpDocs('task2', 'ES7 Decorator Example MyTasks2 - Task 2');
    }

    task1 () {
      this.logger.log('MyTasks2 task1');
    }

    task2 () {
      this.logger.log('MyTasks2 task2');
    }
  }
  bz.add(MyTasks2);

  let p = bz.run('MyTasks', 'MyTasks.task2', 'MyTasks2', 'MyTasks2.task2');
  // prints help results
  // bz.printHelp();
/* Output:
MyTasks task1
MyTasks task2
MyTasks2 task1
MyTasks2 task2
*/

$register() → {object}

Source:

Register the Task with BZ

Returns:

Promise

Type
object

$run(…args) → {object}

Source:

Runs task(s) - multi args run in sequence, arrays are run in parallel

Parameters:
Name Type Attributes Description
args function | string <repeatable>

task(s)

Returns:

Promise

Type
object

$sequence(…args) → {Object}

Source:

Runs task(s) in sequence

Example
  let Beelzebub = require('../../');
  let bz = Beelzebub(options || { verbose: true });

  class MyTasks extends Beelzebub.Tasks {
    _delay (message, delay = 300) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.logger.log(message);
          resolve();
        }, delay);
      });
    }

    task1 () {
      this.logger.log('MyTasks task1');
      return this.$sequence('.task3', '.task4');
    }

    task2 () {
      return this._delay('MyTasks task2', 200);
    }

    task3 () {
      this.logger.log('MyTasks task3');
      return this.$run('.task5', '.task6');
    }

    task4 () {
      return this._delay('MyTasks task4', 400);
    }

    task5 () {
      this.logger.log('MyTasks task5');
    }

    task6 () {
      return this._delay('MyTasks task6', 600);
    }
}

  bz.add(MyTasks);
  // params are run in sequence
  let p = bz.run('MyTasks.task1', 'MyTasks.task2');
/* Output:
MyTasks task1
MyTasks task3
MyTasks task5
MyTasks task6
MyTasks task4
MyTasks task2
*/
Parameters:
Name Type Attributes Description
args object | string <repeatable>

task(s)

Returns:

Promise

Type
Object

$setDefault(taskFuncName)

Source:

Set a Task as default

Example
  let Beelzebub = require('../../');
  let bz = Beelzebub(options || { verbose: true });

  class MyRootLevel extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$useAsRoot();
    }

    task1 () {
      this.logger.log('MyRootLevel task1');
    }

    task2 () {
      this.logger.log('MyRootLevel task2');
    }
  }

  class MyTasks1 extends Beelzebub.Tasks {
    default () {
      this.logger.log('MyTasks1 default');
    }
  }

  class MyTasks2 extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('myDefault');
    }

    myDefault () {
      this.logger.log('MyTasks2 myDefault');
    }
  }

  bz.add(MyRootLevel);
  bz.add(MyTasks1);
  bz.add(MyTasks2);

  let p = bz.run(
    'task1',
    'task2',
    'MyTasks1',
    'MyTasks1.default',
    'MyTasks2',
    'MyTasks2.myDefault'
  );
/* Output:
MyRootLevel task1
MyRootLevel task2
MyTasks1 default
MyTasks1 default
MyTasks2 myDefault
MyTasks2 myDefault
*/
Parameters:
Name Type Description
taskFuncName string

This Class (Task) function name

$setGlobalVars(vars)

Source:

Set Global Vars

Parameters:
Name Type Description
vars object

$setName(name)

Source:

Set name of Task Group/Class, when refering to Task Group in CLI or other Tasks

Parameters:
Name Type Description
name string

New name of Task Group/Class

$setSubTask(name, task)

Source:

Set SubTask

Parameters:
Name Type Description
name string

Name of Sub Task

task string

Task Class

$setSubTasks(tasks)

Source:

Set All Sub Task(s)

Parameters:
Name Type Description
tasks object

$setTaskHelpDocs(taskName, helpDocs)

Source:

Set Help Docs for Task

Example
  const Beelzebub = require('../../');
  const bz = Beelzebub(options || { verbose: true });

  class MyTasks extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('task1');

      this.$setTaskHelpDocs('task1', 'ES7 Decorator Example MyTasks - Task 1');
      this.$setTaskHelpDocs('task2', 'ES7 Decorator Example MyTasks - Task 2');
    }

    task1 () {
      this.logger.log('MyTasks task1');
    }

    task2 () {
      this.logger.log('MyTasks task2');
    }
  }
  bz.add(MyTasks);

  class MyTasks2 extends bz.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('task1');

      this.$setTaskHelpDocs('task1', 'ES7 Decorator Example MyTasks2 - Task 1');
      this.$setTaskHelpDocs('task2', 'ES7 Decorator Example MyTasks2 - Task 2');
    }

    task1 () {
      this.logger.log('MyTasks2 task1');
    }

    task2 () {
      this.logger.log('MyTasks2 task2');
    }
  }
  bz.add(MyTasks2);

  let p = bz.run('MyTasks', 'MyTasks.task2', 'MyTasks2', 'MyTasks2.task2');
  // prints help results
  // bz.printHelp();
/* Output:
MyTasks task1
MyTasks task2
MyTasks2 task1
MyTasks2 task2
*/
Parameters:
Name Type Description
taskName string

Name of Task

helpDocs string

Help Docs for Task

$useAsRoot()

Source:

Use this Task as root task

Example
  let Beelzebub = require('../../');
  let bz = Beelzebub(options || { verbose: true });

  class MyRootLevel extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$useAsRoot();
    }

    task1 () {
      this.logger.log('MyRootLevel task1');
    }

    task2 () {
      this.logger.log('MyRootLevel task2');
    }
  }

  class MyTasks1 extends Beelzebub.Tasks {
    default () {
      this.logger.log('MyTasks1 default');
    }
  }

  class MyTasks2 extends Beelzebub.Tasks {
    constructor (config) {
      super(config);

      this.$setDefault('myDefault');
    }

    myDefault () {
      this.logger.log('MyTasks2 myDefault');
    }
  }

  bz.add(MyRootLevel);
  bz.add(MyTasks1);
  bz.add(MyTasks2);

  let p = bz.run(
    'task1',
    'task2',
    'MyTasks1',
    'MyTasks1.default',
    'MyTasks2',
    'MyTasks2.myDefault'
  );
/* Output:
MyRootLevel task1
MyRootLevel task2
MyTasks1 default
MyTasks1 default
MyTasks2 myDefault
MyTasks2 myDefault
*/