-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron-wrap
executable file
·143 lines (125 loc) · 2.85 KB
/
cron-wrap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
#
# Synopsis:
# Wrap blobs when flowd is "quiet", based upon mtime of run/flowd.gyr
# Usage:
# cron-wrap 60
#
START_EPOCH=$(date +%s)
START_TIME=$(RFC3339Nano)
PROG=$(basename $0)
TMPDIR=${TMPDIR:=/tmp}
TMP_JSON=$TMPDIR/$PROG-$$.json
now()
{
date +'%Y/%m/%d %H:%M:%S'
}
log()
{
echo "$(now): $@"
}
leave()
{
rm -f $TMP_JSON || log "WARN: rm tmp json failed: exit status=$?" >&2
log "good bye, cruel world: $(
duration-english $(
expr $(date +%s) - $START_EPOCH
)
) elapsed"
}
die()
{
NOW=$(now)
MSG="$NOW: $@"
test -d run/ && echo "$MSG" >>run/$PROG.fault
echo "$NOW: ERROR: $@" >&2
exit 1
}
test $# = 1 || die "wrong number of arguments: expected 1, got $#"
STALE_MTIME=$1
log 'hello, world'
trap leave EXIT
[[ "$STALE_MTIME" =~ ^[1-9][0-9]{0,9}$ ]] ||
die "unknown stale mtime: $STALE_MTIME"
log "stale mtime: $STALE_MTIME sec"
test -n "$BLOBIO_ROOT" || die "env not defined: $BLOBIO_ROOT"
log "BLOBIO_ROOT=$BLOBIO_ROOT"
cd $BLOBIO_ROOT || die "cd BLOBIO_ROOT failed: exit status=$?"
. etc/profile
test -n "$BLOBIO_SERVICE" || die 'env not defined: BLOBIO_SERVICE'
log "BLOBIO_SERVICE=$BLOBIO_SERVICE"
test -n "$BLOBIO_ALGORITHM" || die 'env not defined: BLOBIO_ALGORITHM'
log "BLOBIO_ALGORITHM=$BLOBIO_ALGORITHM"
STAT=run/flowd.gyr
file-stale-mtime $STALE_MTIME $STAT
STATUS=$?
case $STATUS in
0)
log 'run/flowd.gyr is stale'
;;
1)
log 'run/flowd.gyr is fresh, no wrap'
exit 0
;;
2)
die "run/flowd.gyr disappeared"
;;
*)
die "file-stale-mtime failed: exit status=$?"
;;
esac
test -e $STAT || die "run/stat does not exist: $STAT"
WRAP=$(blobio wrap --service $BLOBIO_SERVICE)
STATUS=$?
case $STATUS in
0)
log "wrap udig: $WRAP"
;;
1)
die 'wrap replied "no", not sure why'
;;
*)
die "blobio wrap failed: exit status=$?"
;;
esac
cat >>$TMP_JSON <<END || die "cat tmp json failed: exit status=$?"
{
"job.blobio.com": {
"command_line": [
"$PROG",
"$STALE_MTIME"
],
"wrap_blob": "$WRAP",
"environment": {
"BLOBIO_ROOT": "$BLOBIO_ROOT",
"BLOBIO_SERVICE": "$BLOBIO_SERVICE",
"BLOBIO_ALGORITHM": "$BLOBIO_ALGORITHM",
"JMSCOTT_ROOT": "$JMSCOTT_ROOT",
"USER": "$USER",
"PATH": "$PATH",
"LD_LIBRARY_PATH": "$LD_LIBRARY_PATH",
"PWD": "$PWD"
}
},
"start_time": "$START_TIME",
"stop_time": "$(RFC3339Nano)",
"elapsed_duration": "$(duration-english $(
expr $(date +%s) - $START_EPOCH
))",
"hostname": "$(hostname -f)"
}
END
JSON_UDIG=$BLOBIO_ALGORITHM:$(
blobio eat --algorithm $BLOBIO_ALGORITHM --input-path $TMP_JSON
)
STATUS=$?
test $STATUS = 0 || die "blobio eat failed: exit status=$STATUS"
log "json txn json: $JSON_UDIG"
blobio give \
--input-path $TMP_JSON \
--udig $JSON_UDIG \
--service $BLOBIO_SERVICE ||
die "blobio give json failed: exit status=$?"
echo "$(RFC3339Nano) $JSON_UDIG" >>spool/$PROG.txn ||
die "echo >spool/txn failed: exit status=$?"
exit 0