@@ -16,28 +16,40 @@ impl NginxDequeue {
1616 NGINX_QUEUE . is_empty ( )
1717 }
1818
19- /// Only clear pending messages for the *same* container_id and domain.
19+ /// Only clear pending messages for the *same* container_id, domain, and action type.
20+ /// This prevents clearing "start" events when processing "die" events during restarts.
2021 pub fn clear_pending_messages ( domain : & str , container_id : & str ) {
22+ Self :: clear_pending_messages_by_action ( domain, container_id, "die" ) ;
23+ }
24+
25+ /// Clear pending messages for specific action type to avoid clearing restart events
26+ pub fn clear_pending_messages_by_action ( domain : & str , container_id : & str , action : & str ) {
2127 let mut removed = false ;
22- loop {
23- if let Some ( nginx_msg) = Self :: peek ( ) {
24- let is_host_match = nginx_msg. host . as_ref ( ) . map ( |h| h. domain == domain) . unwrap_or ( false ) ;
25- let is_redirect_match = nginx_msg. redirect . as_ref ( ) . map ( |r| r. from == domain) . unwrap_or ( false ) ;
28+ let mut messages_to_requeue = Vec :: new ( ) ;
29+
30+ // First, collect all messages and separate the ones we want to keep
31+ while let Some ( nginx_msg) = Self :: message ( ) {
32+ let is_host_match = nginx_msg. host . as_ref ( ) . map ( |h| h. domain == domain) . unwrap_or ( false ) ;
33+ let is_redirect_match = nginx_msg. redirect . as_ref ( ) . map ( |r| r. from == domain) . unwrap_or ( false ) ;
2634
27- // Only remove if both domain and container_id match
28- if ( is_host_match || is_redirect_match) && nginx_msg. container_id == container_id {
29- Self :: message ( ) ;
30- println ! ( "🧹 Removed pending Nginx queue message for '{}' (container_id: {})" , domain, container_id) ;
31- removed = true ;
32- } else {
33- break ;
34- }
35+ // Only remove if domain, container_id, AND action match
36+ if ( is_host_match || is_redirect_match) && nginx_msg. container_id == container_id && nginx_msg. action == action {
37+ println ! ( "🧹 Removed pending Nginx queue message for '{}' (container_id: {}, action: {})" , domain, container_id, action) ;
38+ removed = true ;
3539 } else {
36- break ;
40+ // Keep this message - we'll requeue it
41+ messages_to_requeue. push ( nginx_msg) ;
3742 }
3843 }
44+
45+ // Requeue the messages we want to keep
46+ for msg in messages_to_requeue {
47+ use super :: shared:: NGINX_QUEUE ;
48+ NGINX_QUEUE . enqueue ( msg) ;
49+ }
50+
3951 if !removed {
40- println ! ( "🧹 No pending Nginx queue messages found for '{}' with container_id '{}'" , domain, container_id) ;
52+ println ! ( "🧹 No pending Nginx queue messages found for '{}' with container_id '{}' and action '{}' " , domain, container_id, action ) ;
4153 }
4254 }
4355}
0 commit comments