@@ -3,10 +3,10 @@ use crate::matrix::MatrixClient;
3
3
use crate :: webhook:: Alert ;
4
4
use crate :: { unix_time, AlertId , Result } ;
5
5
use actix:: prelude:: * ;
6
- use std:: sync:: Arc ;
6
+ use std:: sync:: { Arc , Mutex } ;
7
7
use std:: time:: Duration ;
8
8
9
- const CRON_JON_INTERVAL : u64 = 5 ;
9
+ const CRON_JOB_INTERVAL : u64 = 5 ;
10
10
11
11
#[ derive( Debug , Clone , Eq , PartialEq , Serialize , Deserialize ) ]
12
12
pub struct AlertContext {
@@ -103,6 +103,8 @@ pub struct Processor {
103
103
db : Option < Arc < Database > > ,
104
104
escalation_window : u64 ,
105
105
should_escalate : bool ,
106
+ // Ensures that only one escalation task is running at the time.
107
+ escalation_lock : Arc < Mutex < ( ) > > ,
106
108
}
107
109
108
110
impl Processor {
@@ -111,6 +113,7 @@ impl Processor {
111
113
db : db. map ( |db| Arc :: new ( db) ) ,
112
114
escalation_window : escalation_window,
113
115
should_escalate : should_escalate,
116
+ escalation_lock : Default :: default ( ) ,
114
117
}
115
118
}
116
119
fn db ( & self ) -> Arc < Database > {
@@ -159,15 +162,25 @@ impl Actor for Processor {
159
162
Result :: < ( ) > :: Ok ( ( ) )
160
163
} ;
161
164
165
+ let lock = Arc :: clone ( & self . escalation_lock ) ;
162
166
ctx. run_interval (
163
- Duration :: from_secs ( CRON_JON_INTERVAL ) ,
167
+ Duration :: from_secs ( CRON_JOB_INTERVAL ) ,
164
168
move |_proc, _ctx| {
169
+ // Acquire new handles for async task.
165
170
let db = Arc :: clone ( & db) ;
171
+ let lock = Arc :: clone ( & lock) ;
166
172
167
173
actix:: spawn ( async move {
168
- match local ( db, escalation_window) . await {
169
- Ok ( _) => { }
170
- Err ( err) => error ! ( "{:?}" , err) ,
174
+ // Immediately exits if the lock cannot be acquired.
175
+ if let Ok ( locked) = lock. try_lock ( ) {
176
+ // Lock acquired and will remain locked until
177
+ // `_l` goes out of scope.
178
+ let _l = locked;
179
+
180
+ match local ( db, escalation_window) . await {
181
+ Ok ( _) => { }
182
+ Err ( err) => error ! ( "{:?}" , err) ,
183
+ }
171
184
}
172
185
} ) ;
173
186
} ,
0 commit comments