瀏覽代碼

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
             "-c DIR      change to DIR before doing anything"                 \
16
             "-c DIR      change to DIR before doing anything"                 \
17
             "-l          list handlers and exit"                              \
17
             "-l          list handlers and exit"                              \
18
             "-d          turn on debugging mode"                              \
18
             "-d          turn on debugging mode"                              \
19
+            "-N          turn off locking"                                    \
19
        --                                                                     \
20
        --                                                                     \
20
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
21
        "imapdomo will try to read init.lua and handlers/ACTION.lua from its"  \
21
        "configuration directory."                                             \
22
        "configuration directory."                                             \
73
       | sed 's/.lua$//'
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
 handle() {
104
 handle() {
77
     #
105
     #
78
     # Handle action $Action
106
     # Handle action $Action
111
     local CdTo      # change dir to this before running imapfilter
139
     local CdTo      # change dir to this before running imapfilter
112
     local Certs     # certificate storage
140
     local Certs     # certificate storage
113
     local es=0      # exit status of this function
141
     local es=0      # exit status of this function
142
+    local NoLock    # disable locks
114
     CfgDir="$IMAPDOMO_CFGDIR"
143
     CfgDir="$IMAPDOMO_CFGDIR"
115
     LogDir="$IMAPDOMO_USER_CACHE/logs"
144
     LogDir="$IMAPDOMO_USER_CACHE/logs"
116
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
145
     HeaderDir="$IMAPDOMO_USER_CACHE/headers"
117
     Certs="$IMAPDOMO_CFGDIR/certificates"
146
     Certs="$IMAPDOMO_CFGDIR/certificates"
147
+    NoLock=false
118
     Debug=false
148
     Debug=false
119
     #shellcheck disable=SC2034
149
     #shellcheck disable=SC2034
120
     while true; do case $1 in
150
     while true; do case $1 in
122
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
152
         -d) Debug=true; PRETTY_DEBUG=true; shift ;;
123
         -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
153
         -t) Certs="$2"; shift 2 || usage -w "missing value to: $1" ;;
124
         -l) lshandlers; exit ;;
154
         -l) lshandlers; exit ;;
155
+        -N) NoLock=true; shift ;;
125
         -V|--version-semver) show_semversion ;;
156
         -V|--version-semver) show_semversion ;;
126
         --version) show_version ;;
157
         --version) show_version ;;
127
         -*) usage -w "unknown argument: '$1'" ;;
158
         -*) usage -w "unknown argument: '$1'" ;;
129
     esac done
160
     esac done
130
     Action="$1"; shift
161
     Action="$1"; shift
131
     test -n "$Action" || usage -w "no action specified"
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
     handle "$Action"; es=$?
166
     handle "$Action"; es=$?
167
+    unlock
134
     return $es
168
     return $es
135
 }
169
 }
136
 
170