summaryrefslogtreecommitdiff
path: root/bin/ii-watch
blob: ae008c4ae3f159490cab892eed3b7773476c87d2 (plain)
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
#!/bin/sh

# shellcheck disable=SC2119,SC2120

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

checksum=$(
  calculate_script_checksum
)

next_connection_check=0
next_intentions_check=0
last_seen=0

while pgrep -x 'ii' >/dev/null && \
  [ "$(calculate_script_checksum)" = "${checksum}" ]; do
  # this avoids missing modifications during our last execution
  if [ "$(date +%s)" -ge ${next_connection_check} ]; then
    # request this more often on startup
    if [ ${last_seen} -ne 0 ]; then
      next_connection_check=$((
        $(date +%s) + 60*5
      ))
    fi
    echo '/NAMES #archlinux32-devops' \
    | sponge "${irc_dir}/in"
  fi
  if [ -z "${said}" ]; then
    # shellcheck disable=SC2046
    inotifywait -t 30 -e 'CLOSE_WRITE,CLOSE' -e 'CREATE,ISDIR' $(
      find "${irc_dir}" \
        -type f \
        -name 'out' -o \
        -type d
      ) || true
  fi
  said=$(
    "${base_dir}/bin/ii-answer"
  )
  if [ ${next_intentions_check} -lt "$(date +%s)" ]; then
    # Are there old intentions waiting for execution?
    oldest_intention=$(
      find "${intentions_directory}" \
        -maxdepth 1 \
        -type f \
        -name 'intention.*' \
        -mmin +1200 \
        -printf '%Ts\n' \
      | sort -n \
      | tail -n1
    )
    if [ -n "${oldest_intention}" ]; then
      printf 'There is something intented for %s minutes.\n' $((
        $(date +%s) - oldest_intention
      )) \
      | irc_say
      next_intentions_check=$((
        $(date +%s) + 3600
      ))
    fi
  fi
  # When was the buildmaster seen the last time?
  last_seen=$(
    {
      echo 0
      sed '
        s/^\([0-9]\+\) = #archlinux32-devops\( \S\+\)* buildmaster\( \S\+\)*$/\1/
        t
        d
      ' "${irc_dir}/out"
    } \
    | sort -n \
    | tail -n1
  )
  # more than 10 minutes ago
  if [ $((last_seen + 60*10)) -lt "$(date +%s)" ]; then
    # reconnect!
    "${base_dir}/bin/ii-connect"
  fi
done