test(unitproc):bkFreqWrite:Rewrote script to use while read
[BK-2020-03.git] / unitproc / bkFreqWrite
CommitLineData
045dc2fb
SBS
1#!/bin/bash
2# Desc: Writes stdin to disk every 5 minutes
3
d642682d
SBS
4yell() { echo "[$(date --iso-8601=ns)]:$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
5die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
6try() { "$@" || die "cannot $*"; } #o
045dc2fb 7
9988125c
SBS
8declare -a buffer # Initialize buffer array
9scriptTTL="15";
10bufferTTL="5";
d642682d 11
9988125c
SBS
12magicWriteBuffer() {
13 printf "%s\n" "${buffer[@]}" >> /dev/shm/$(date +%s)..bkFreqWrite-output.txt;
14}
d642682d 15
9988125c
SBS
16bufferRound=0
17# Run until script TTL seconds pass
18while [[ $SECONDS -lt "scriptTTL" ]]; do
19 bufferTOD="$((SECONDS + $bufferTTL))";
20 lineCount=0
21 # Start and fill buffer until buffer time-of-death (TOD) arrives
22 while read -r line && [[ $SECONDS -lt "$bufferTOD" ]]; do
23 # Append line to buffer
24 buffer+=("$line");
25 echo "Processing line:$lineCount";
26 echo "Current line :$line";
27 echo "buf elem count :${#buffer[@]}";
28 ((lineCount++));
29 done;
30 # Export buffer to asynchronous processing.
31 magicWriteBuffer &
32 unset buffer;
33 # Increment buffer round
34 ((bufferRound++));
d642682d 35done;