many updates to change BINDIR to FEISTY_MEOW_BINARIES. expands on previous changes...
[feisty_meow.git] / scripts / processes / process_manager.sh
1 #!/bin/bash
2
3 ##############
4 #
5 #  Name   : process manager helper methods
6 #  Author : Chris Koeritz
7 #  Rights : Copyright (C) 2015-$now by Author
8 #
9 # Purpose:
10 #
11 #   Relies on the built-in process management in bash to run a bunch of
12 # processes in the background, but will limit total number running to a
13 # maximum count.  There is a demonstration method at the end of the file
14 # that shows how to use the process management functions.
15 #
16 ##############
17 # This script is free software; you can modify/redistribute it under the terms
18 # of the GNU General Public License. [ http://www.gnu.org/licenses/gpl.html ]
19 # Feel free to send updates to: [ fred@gruntose.com ]
20 ##############
21
22 #hmmm: revisions desired someday:
23 #  + allow number of max processes to be passed in.
24 #  + 
25
26 # number of background processes.
27 bg_count=0
28
29 # maximum number of simultaneous background processes.
30 max_bg_procs=20
31
32 # number of processes to wait for if we hit the maximum.
33 procs_to_await=3
34
35 function start_background_action()
36 {
37   # launch the commands provided as parms in a subshell.
38   (for i in "${@}"; do eval "$i" ; done)&
39   #echo bg_count pre inc is $bg_count
40   ((bg_count++))
41   #echo bg_count post inc is $bg_count
42 }
43
44 function take_inventory()
45 {
46   start_background_action \
47       'echo "taking inventory..."' \
48       'bash $FEISTY_MEOW_SCRIPTS/core/inventory.sh'
49 }
50
51 function nechung()
52 {
53   start_background_action \
54       'echo "your nechung oracle pronouncement of the moment..."' \
55       '$FEISTY_MEOW_BINARIES/nechung'
56 }
57
58 function sum_dir()
59 {
60   start_background_action \
61       'echo "summing directory output coming up..."' \
62       'perl $FEISTY_MEOW_SCRIPTS/files/summing_dir.pl'
63 }
64
65 # takes the number of processes to wait for, or just waits for one of them.
66 function wait_on_backgrounders()
67 {
68   local wait_count="$1"; shift
69
70   target_count=$(($bg_count - $wait_count))
71   if (($target_count < 1)); then target_count=1; fi
72   echo before waiting, count is $bg_count
73   while (($bg_count > $target_count - 1)); do
74     # wait for one job, let bash pick which.
75     wait -n
76     echo bg_count pre dec is $bg_count
77     ((bg_count--))
78     echo bg_count post dec is $bg_count
79   done
80   echo "done waiting, background process count is down to $bg_count."
81 }
82
83 # happily launches off different actions as background processes.
84 launcher_demonstrator()
85 {
86   while true; do
87     # pick a thing to do.
88     which=$(($RANDOM % 3))
89 #hmmm: not asynch yet!  make it so!
90     case $which in
91       0) take_inventory;;
92       1) nechung;;
93       2) sum_dir;;
94     esac
95
96     # we have reached the limit on processes and need to wait for a few, defined by
97     # procs_to_await variable at top.
98     if (($bg_count > $max_bg_procs - 1)); then
99       echo "have reached $max_bg_procs background processes threshold; waiting for $procs_to_await of them to complete."
100       wait_on_backgrounders $procs_to_await
101     fi
102
103   done
104 }
105
106 launcher_demonstrator;
107