5
0
mirror of git://git.proxmox.com/git/pve-guest-common.git synced 2024-12-24 21:34:13 +03:00

PVE::ReplicationState - new helpers record_job_start/record_job_end

This commit is contained in:
Dietmar Maurer 2017-06-20 06:53:36 +02:00
parent e4f6301672
commit c17dcb3eb3
2 changed files with 37 additions and 20 deletions

View File

@ -309,17 +309,10 @@ my $run_replication_nolock = sub {
eval {
my $state = PVE::ReplicationState::read_job_state($jobcfg);
PVE::ReplicationState::record_job_start($jobcfg, $state, $start_time, $iteration);
my $t0 = [gettimeofday];
$state->{pid} = $$;
$state->{ptime} = PVE::ProcFSTools::read_proc_starttime($state->{pid});
$state->{last_node} = PVE::INotify::nodename();
$state->{last_try} = $start_time;
$state->{last_iteration} = $iteration;
$state->{storeid_list} //= [];
PVE::ReplicationState::write_job_state($jobcfg, $state);
mkdir $PVE::ReplicationState::replicate_logdir;
my $logfile = PVE::ReplicationState::job_logfile_name($jobid);
open(my $logfd, '>', $logfile) ||
@ -340,24 +333,15 @@ my $run_replication_nolock = sub {
};
my $err = $@;
$state->{duration} = tv_interval($t0);
delete $state->{pid};
delete $state->{ptime};
if ($err) {
chomp $err;
$state->{fail_count}++;
$state->{error} = "$err";
PVE::ReplicationState::write_job_state($jobcfg, $state);
$logfunc_wrapper->("end replication job with error: $err");
} else {
$logfunc_wrapper->("end replication job");
$state->{last_sync} = $start_time;
$state->{fail_count} = 0;
delete $state->{error};
PVE::ReplicationState::write_job_state($jobcfg, $state);
}
PVE::ReplicationState::record_job_end($jobcfg, $state, $start_time, tv_interval($t0), $err);
close($logfd);
};
if (my $err = $@) {

View File

@ -5,6 +5,7 @@ use strict;
use JSON;
use PVE::INotify;
use PVE::ProcFSTools;
use PVE::Tools;
use PVE::CalendarEvent;
use PVE::Cluster;
@ -114,6 +115,38 @@ sub write_vmid_job_states {
PVE::GuestHelpers::guest_migration_lock($vmid, undef, $code);
}
sub record_job_start {
my ($jobcfg, $state, $start_time, $iteration) = @_;
$state->{pid} = $$;
$state->{ptime} = PVE::ProcFSTools::read_proc_starttime($state->{pid});
$state->{last_node} = PVE::INotify::nodename();
$state->{last_try} = $start_time;
$state->{last_iteration} = $iteration;
$state->{storeid_list} //= [];
write_job_state($jobcfg, $state);
}
sub record_job_end {
my ($jobcfg, $state, $start_time, $duration, $err) = @_;
$state->{duration} = $duration;
delete $state->{pid};
delete $state->{ptime};
if ($err) {
chomp $err;
$state->{fail_count}++;
$state->{error} = "$err";
} else {
$state->{last_sync} = $start_time;
$state->{fail_count} = 0;
delete $state->{error};
}
write_job_state($jobcfg, $state);
}
sub replication_snapshot_name {
my ($jobid, $last_sync) = @_;