Преглед на файлове

Add locking mechanism

Action won't start if one is already running.  This should prevent
overlapping actions.

It's possible to turn off locking to allow running imapdomo as service,
ie. locking will be taken care of by init system.
Alois Mahdal преди 6 години
родител
ревизия
730ffc903a
променени са 1 файла, в които са добавени 35 реда и са изтрити 1 реда
  1. 35
    1
      src/imapdomo.skel

+ 35
- 1
src/imapdomo.skel Целия файл

@@ -16,6 +16,7 @@ usage() {
16 16
             "-c DIR      change to DIR before doing anything"                 \
17 17
             "-l          list handlers and exit"                              \
18 18
             "-d          turn on debugging mode"                              \
19
+            "-N          turn off locking"                                    \
19 20
        --                                                                     \
20 21
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
21 22
        "configuration directory."                                             \
@@ -73,6 +74,33 @@ lshandlers() {
73 74
       | sed 's/.lua$//'
74 75
 }
75 76
 
77
+lock() {
78
+    #
79
+    # Put the lockfile
80
+    #
81
+    $NoLock && return 0
82
+    date +"$$@%s" > "$IMAPDOMO_USER_CACHE/lock"
83
+}
84
+
85
+is_locked() {
86
+    #
87
+    # Does lockfile exist?
88
+    #
89
+    # True if lockfile exists. False if lockfile does not exist or locking
90
+    # is turned off.
91
+    #
92
+    $NoLock && return 1
93
+    test -e "$IMAPDOMO_USER_CACHE/lock"
94
+}
95
+
96
+unlock() {
97
+    #
98
+    # Remove the lockfile
99
+    #
100
+    $NoLock && return 0
101
+    rm "$IMAPDOMO_USER_CACHE/lock"
102
+}
103
+
76 104
 handle() {
77 105
     #
78 106
     # Handle action $Action
@@ -111,10 +139,12 @@ main() {
111 139
     local CdTo      # change dir to this before running imapfilter
112 140
     local Certs     # certificate storage
113 141
     local es=0      # exit status of this function
142
+    local NoLock    # disable locks
114 143
     CfgDir="$IMAPDOMO_CFGDIR"
115 144
     LogDir="$IMAPDOMO_USER_CACHE/logs"
116 145
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
117 146
     Certs="$IMAPDOMO_CFGDIR/certificates"
147
+    NoLock=false
118 148
     Debug=false
119 149
     #shellcheck disable=SC2034
120 150
     while true; do case $1 in
@@ -122,6 +152,7 @@ main() {
122 152
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
123 153
         -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
124 154
         -l) lshandlers; exit ;;
155
+        -N) NoLock=true; shift ;;
125 156
         -V|--version-semver) show_semversion ;;
126 157
         --version) show_version ;;
127 158
         -*) usage -w "unknown argument: '$1'" ;;
@@ -129,8 +160,11 @@ main() {
129 160
     esac done
130 161
     Action="$1"; shift
131 162
     test -n "$Action" || usage -w "no action specified"
132
-    debug -v Action CfgDir LogDir HeaderDir Debug CdTo
163
+    debug -v Action CfgDir LogDir HeaderDir Debug CdTo NoLock
164
+    is_locked && return 1
165
+    lock
133 166
     handle "$Action"; es=$?
167
+    unlock
134 168
     return $es
135 169
 }
136 170