首页 > 代码库 > BASH 脚本制作简单 Workflow - 更新 2014/6/17 12:00

BASH 脚本制作简单 Workflow - 更新 2014/6/17 12:00

因为需要,我创建了一个脚本叫 wf.sh (随意起什么名字),它可以读取当前目录下的 config.ini 配置文件来解析并执行工作流任务(Workflow)。


直接上代码:

config.ini

Workflow.Name = example
Workflow.Description = my work flow example \n and I know what's going on here.
#Workflow.StartJob = job1
Workflow.NextJobCounts = 1
Workflow.NextJob1.Name = job1

Job.job1.Command = echo "I am job1"; sleep 3;
Job.job1.NextJobCounts = 3
Job.job1.NextJob1.Name = job2
Job.job1.NextJob2.Name = job3
Job.job1.NextJob3.Name = job4

Job.job2.Command = echo "I am job2"; sleep 3;
Job.job2.NextJobCounts = 0

Job.job3.Command = echo "I am job3"; sleep 3;
Job.job3.NextJobCounts = 0

Job.job4.Command = echo "I am job4"; sleep 3;
Job.job4.NextJobCounts = 1
Job.job4.NextJob1.Name = job5

Job.job5.Command = echo "I am job5"; sleep 3;
Job.job5.NextJobCounts = 1
Job.job5.NextJob1.Name = job6

Job.job6.Command = echo "I am job6"; sleep 3;
Job.job6.NextJobCounts = 0



wf.sh

#!/bin/bash

CFGGet() {
  grep "^$1 = " config.ini | tail -1 | sed 's~^[^=]* = \(.*\)$~\1~g'
}

# Notify: make sure launch() is below library functions and dependencies.
launch() {
  local job="$1"
  local ret=0

  eval $(CFGGet Job.${job}.Command) || {
    ret=$?
    echo FAIL,$ret, command [$(CFGGet Job.${job}.Command)] is fail.
    exit $ret
  }

  local nextJobCounts=$(CFGGet Job.${job}.NextJobCounts)
  for ((i=1;i<=nextJobCounts;i++)); do
    eval launch $(CFGGet Job.${job}.NextJob${i}.Name) &
  done

  return 0
}

run() {
  local jobCounts=$(CFGGet Workflow.NextJobCounts)

  for ((i=1;i<=jobCounts;i++)); do
    launch $(CFGGet Workflow.NextJob${i}.Name) &
  done

  return 0
}

run



运行方式:

$ ./wf.sh