File: smd-loop

package info (click to toggle)
syncmaildir 1.2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,188 kB
  • ctags: 479
  • sloc: ansic: 6,321; sh: 1,969; makefile: 258; xml: 14
file content (218 lines) | stat: -rwxr-xr-x 4,372 bytes parent folder | download | duplicates (7)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash

# Released under the terms of GPLv3 or at your option any later version.
# No warranties.
# Copyright Enrico Tassi <gares@fettunta.org>

# The config file name
CONFFILE=~/.smd/loop

# The lock file name
LOCKFILE=~/.smd/loop.lock

if [ -e $LOCKFILE ]; then
	if ps -p `cat $LOCKFILE` > /dev/null | grep smd-loop; then
		echo Another smd-loop instance is running. If it is not the case
		echo remove $LOCKFILE and retry
		echo "any: smd-loop@localhost: TAGS: error::context(locking) probable-cause(another-instance-is-running) human-intervention(necessary) suggested-actions(run(kill `cat $LOCKFILE`) run(rm $LOCKFILE))"
		exit 1
	else
		echo "Found lock file of a dead instance. Ignored."
	fi
fi

[ -d ~/.smd ] || mkdir -p ~/.smd
echo $$ > $LOCKFILE

# The log file
LOGFILE=~/.smd/log/loop.log
mkdir -p `dirname $LOGFILE`
> $LOGFILE
log() {
	echo `date '+%x %X'`: $@ >> $LOGFILE 
}
log_cat() {
	cat $1 | sed 's/^/output: /' >> $LOGFILE 
}

# The length of a minute, decrease to debug
MINUTE=60

# The clock, incremented every $MINUTE
TIME=1

# Verbose
VERBOSE=0

# Just create a template
TEMPLATE_ONLY=0

# Temp file, used to store subprocesses' output
OUTPUT=`mktemp -q /tmp/smd-loop.XXXXXXXXXX`

# List of commands that failed
STOP_TAG="__STOP__"
declare -a FAILURES=("$STOP_TAG")

# Prefix
PREFIX="@PREFIX@"

if [ `echo $PREFIX | cut -c -1` = "@" ]; then
	echo "smd-loop not installed, assuming smd-pull is ./smd-pull"
	echo "smd-loop not installed, assuming smd-push is ./smd-push"
	log "smd-loop not installed, assuming smd-pull is ./smd-pull"
	log "smd-loop not installed, assuming smd-push is ./smd-push"
	PULL="./smd-pull"
	PUSH="./smd-push"
else
	PULL="$PREFIX/bin/smd-pull"
	PUSH="$PREFIX/bin/smd-push"
fi

remove_from_failures() {
	local item="$1"
	for ((i=0; ; i++)); do
		if [ "${FAILURES[$i]}" = "$item" ]; then
			unset FAILURES[$i]
		fi

		if [ "${FAILURES[$i]}" = "$STOP_TAG" ]; then
			break
		fi
	done
}

add_to_failures() {
	local item="$1"
	for ((i=0; ; i++)); do
		if [ "${FAILURES[$i]}" = "" ]; then
			FAILURES[$i]="$item"
			break
		fi

		if [ "${FAILURES[$i]}" = "$STOP_TAG" ]; then
			FAILURES[$i]="$item"
			FAILURES[`expr $i + 1`]="$STOP_TAG"
			break
		fi
	done
}

has_not_failed() {
	local item="$1"
	local found=0

	for ((i=0; ; i++)); do
		if [ "${FAILURES[$i]}" = "$item" ]; then
			found=1
		fi

		if [ "${FAILURES[$i]}" = "$STOP_TAG" ]; then
			break
		fi
	done

	return $found
}

perform() {
	local cmd=$1
	local endpoint=$2

	local cmd_line="$cmd -s $endpoint"
	if [ "$VERBOSE" = 1 ]; then
		echo smd-loop: $cmd_line 1>&2
	fi
	log "$cmd_line"
	$cmd_line > $OUTPUT 2>&1
	if [ $? = 0 ]; then
		cat $OUTPUT
		remove_from_failures "$cmd_line"
		log "completed successfully"
	else
		if grep -q 'human-intervention(avoidable)' $OUTPUT &&
		   grep -q 'suggested-actions(retry)' $OUTPUT &&
		   has_not_failed "$cmd_line"; then
			if [ "$VERBOSE" = 1 ]; then
				echo smd-loop: warning: failed: $cmd_line 1>&2
				echo smd-loop: warning: will retry later 1>&2
			fi
			add_to_failures "$cmd_line"
			log "avoidable failure, retry later"
			log_cat $OUTPUT
		else
			cat $OUTPUT
			log "persistent or non avoidable failure"
			log_cat $OUTPUT
			exit 1
		fi
	fi
}

cleanup() {
	rm -f $OUTPUT
	rm -f $LOCKFILE
	log "exiting"
}

cleanup_killed() {
	cleanup
	log "killed"
	exit 1
}

trap cleanup "EXIT" 
trap cleanup_killed "SIGTERM" 

if [ "$1" = "-v" ]; then
	VERBOSE=1
	shift
fi

if [ "$1" = "-t" ]; then
	TEMPLATE_ONLY=1
	shift
fi

if [ ! -f $CONFFILE ]; then
	mkdir -p `dirname $CONFFILE`
	cat > $CONFFILE <<-EOT
	# smd-loop configuration file
	#
	# Line starting with '#' are comments.
	# Frequences are in minutes.
	#
	# pull-frequency push-frequency endpoint-name
	  3              10             default
	EOT

	echo No config file found: created a default one
	echo Please edit it: $CONFFILE
	exit 1
fi

if [ "$TEMPLATE_ONLY" = 1 ]; then
	exit 0
fi

log "starting"
first_run=1
while true; do
	while read pull push endpoint; do
		do_pull=1
		do_push=1
		if [ $pull -gt 0 ]; then do_pull=$((TIME % pull)); fi
		if [ $push -gt 0 ]; then do_push=$((TIME % push)); fi
	
		if [ $do_pull -eq 0 ]; then
			perform $PULL $endpoint
			first_run=0
		fi
		if [ $do_push -eq 0 ]; then
			perform $PUSH $endpoint
			first_run=0
		fi
	done < <(grep -v '^#' $CONFFILE)
	TIME=$((TIME+1))
	if [ $first_run -eq 0 ]; then sleep $MINUTE; fi
done