-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.zflai_check_start
30 lines (24 loc) · 1.01 KB
/
.zflai_check_start
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
# Copyright (c) 2018 Sebastian Gniazdowski
#
# zflai_check_start
#
# Ensures that the in-memory log-keeper runs and isn't close
# to exit because of being idle for a long time (i.e. close
# to ZFLAI_KEEP_ALIVE). If any of these is false, ensures that
# the log-keeper will quit, and starts a fresh instance. This
# protects from race conditions.
#
# No input, no return value (always 0)
(( $ZFLAI_FD )) && { builtin print -r -u$ZFLAI_FD 2>&$ZFLAI_NULL_FD || { builtin exec {ZFLAI_FD}>&-; ZFLAI_FD=0; }; }
# Running, but dangerously close to idle-exit moment?
if (( ZFLAI_FD != 0 && ZFLAI_LAST_ACTION + ZFLAI_KEEP_ALIVE - 10 < EPOCHSECONDS )); then
builtin print -u$ZFLAI_FD '!ZFLAI-SPECIAL-CMD: exit' 2>/dev/null
builtin exec {ZFLAI_FD}>&-
ZFLAI_FD=0
fi
# Running (not much idle, that was checked above) ?
(( ZFLAI_FD != 0 )) && return 0
# Not running, or closed to prevent race condition - start a new agent
builtin exec {ZFLAI_FD}> >(.zflai-disk-jokey "$ZUID_ID" "$ZUID_CODENAME") || return 1
return 0
# vim:ft=zsh:et