From 745d942d91d71a8fddb7b942f09ed1b877d638ad Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 2 Aug 2013 10:03:18 +0200 Subject: [PATCH] apt: try to send updated packe list only once --- PVE/API2/APT.pm | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/PVE/API2/APT.pm b/PVE/API2/APT.pm index 072392fd..948f8c1f 100644 --- a/PVE/API2/APT.pm +++ b/PVE/API2/APT.pm @@ -144,10 +144,28 @@ my $assemble_pkginfo = sub { # we try to cache results my $pve_pkgstatus_fn = "/var/lib/pve-manager/pkgupdates"; +my $read_cached_pkgstatus = sub { + my $data = []; + eval { + my $jsonstr = PVE::Tools::file_get_contents($pve_pkgstatus_fn, 5*1024*1024); + $data = decode_json($jsonstr); + }; + if (my $err = $@) { + warn "error reading cached package status in $pve_pkgstatus_fn\n"; + } + return $data; +}; + my $update_pve_pkgstatus = sub { syslog('info', "update new package list: $pve_pkgstatus_fn"); + my $notify_status = {}; + my $oldpkglist = &$read_cached_pkgstatus(); + foreach my $pi (@$oldpkglist) { + $notify_status->{$pi->{Package}} = $pi->{Version}; + } + my $pkglist = []; my $cache = &$get_apt_cache(); @@ -194,6 +212,14 @@ my $update_pve_pkgstatus = sub { } } } + + # keep notification status (avoid sending mails abou new packages more than once) + foreach my $pi (@$pkglist) { + if (my $ns = $notify_status->{$pi->{Package}}) { + $pi->{NotifyStatus} = $ns if $ns eq $pi->{Version}; + } + } + PVE::Tools::file_set_contents($pve_pkgstatus_fn, encode_json($pkglist)); return $pkglist; @@ -230,15 +256,7 @@ __PACKAGE__->register_method({ my $st3 = File::stat::stat("/var/lib/dpkg/status"); if ($st2 && $st3 && $st2->mtime <= $st1->mtime && $st3->mtime <= $st1->mtime) { - my $data; - eval { - my $jsonstr = PVE::Tools::file_get_contents($pve_pkgstatus_fn, 5*1024*1024); - $data = decode_json($jsonstr); - }; - if (my $err = $@) { - warn "error readin cached package status in $pve_pkgstatus_fn\n"; - # continue and overwrite cache with new content - } else { + if (my $data = &$read_cached_pkgstatus()) { return $data; } } @@ -344,7 +362,12 @@ __PACKAGE__->register_method({ print $fh $data; - $fh->close(); + $fh->close() || die "unable to close 'sendmail' - $!"; + + foreach my $pi (@$pkglist) { + $pi->{NotifyStatus} = $pi->{Version}; + } + PVE::Tools::file_set_contents($pve_pkgstatus_fn, encode_json($pkglist)); } }