fix: zmtrigger keeps each monitor's last state across a monitor reload so on/off notices aren't lost refs #7281

This commit is contained in:
Jash committed 2026-11-22 03:03:35 +05:30
1 parent de4e032e3c
commit 19097c17a3
3 files changed
+55 -3

No files matched your search

+9 -2
View File
@@ -302,6 +302,7 @@ exit;
sub loadMonitors {
$monitor_reload_time = time();
my %old_monitors = %monitors; # Keep prior state/event so alarms that started or ended since the last poll are not swallowed
%monitors = ();
foreach my $monitor ( ZoneMinder::Monitor->find(
Deleted=>0,
@@ -309,8 +310,14 @@ sub loadMonitors {
($Config{ZM_SERVER_ID} ? (ServerId=>$Config{ZM_SERVER_ID}) : ()),
)) {
if ($monitor->connect()) { # This will re-init shared memory
$monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor);
my $old = $old_monitors{$monitor->{Id}};
if ($old and defined($old->{LastState})) {
$monitor->{LastState} = $old->{LastState};
$monitor->{LastEvent} = $old->{LastEvent};
} else {
$monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor);
}
}
$monitors{$monitor->{Id}} = $monitor;
} # end while fetchrow
@@ -0,0 +1,45 @@
#!/usr/bin/perl
#
# A full monitor reload (a message for an unknown monitor id, enable/disable,
# the periodic reload) must not forget which state each monitor was last seen
# in. If it re-reads the state from shared memory, an alarm that ended since
# the last poll never sends its "off" and one that started never sends its
# "on" to the clients.
#
# Run as: perl tests/perl/test_zmtrigger_reload_keeps_state.pl
# ZMT_REPO=<dir> points it at another copy of the scripts (e.g. the original).
#
use strict;
use warnings;
use FindBin;
require "$FindBin::Bin/zmtrigger_harness.pm";
my $r = run_zmtrigger(<<'EOM');
package ZmtSim;
our $max_ticks = 14;
our %rows = map { $_ => { Id => $_, Name => "m$_", Deleted => 0, Capturing => 'Always', Recording => 'None', ServerId => 0 } } 1 .. 2;
# A stray message for a monitor that does not exist makes zmtrigger reload all
# monitors, once while monitor 1's alarm has just ended, once while monitor 2's
# has just started.
our @inbox = ([6, '99|on|1|x'], [10, '99|on|1|x']);
sub t { return $ZoneMinder::tick; }
sub mem {
my ($id, $f) = @_;
my $t = t();
my ($state, $event) = (1, 0);
if ($id == 1) { $event = $t >= 3 ? 10 : 0; $state = ($t >= 3 && $t <= 5) ? 3 : 1; }
if ($id == 2) { $event = $t >= 10 ? 20 : 0; $state = $t >= 10 ? 3 : 1; }
return $f eq 'state' ? $state : $event;
}
1;
EOM
my %by;
foreach (grep { /^OUT / } @{$r->{lines}}) {
my $l = s/\|\d{7}\|/|T|/r;
$l =~ /^OUT (\d+)\|/ and push @{$by{$1}}, $l =~ s/^OUT //r;
}
check('daemon ran to the end', $r->{exit} . '/' . $r->{signal}, '0/0');
check('alarm that ended just before a reload still sends off', join(',', @{$by{1} || []}), '1|on|T|10,1|off|T|10');
check('alarm that started just before a reload still sends on', join(',', @{$by{2} || []}), '2|on|T|20');
finish();
+1 -1
View File
@@ -24,7 +24,7 @@ use File::Temp qw(tempdir);
use File::Path qw(make_path);
use FindBin;
my $repo = "$FindBin::Bin/../..";
my $repo = $ENV{ZMT_REPO} // "$FindBin::Bin/../..";
sub write_file {
my ($path, $content) = @_;