[pve-devel] [PATCH pve-guest-common 1/2] Add function delete_state

Dietmar Maurer dietmar at proxmox.com
Tue Jun 27 08:12:44 CEST 2017


> +sub delete_jobs_in_state {
> +    my ($vmid, $tid) = @_;
> +
> +    my $update = sub {
> +
> +	my $stateobj = read_state();
> +	# Note: tuple ($vmid, $tid) is unique
> +	delete $stateobj->{$vmid}->{$tid} if $tid;
> +	delete $stateobj->{$vmid} if !%{$stateobj->{$vmid}};
> +
> +	PVE::Tools::file_set_contents($state_path, encode_json($stateobj));
> +    };
> +
> +    my $code = sub {
> +	PVE::Tools::lock_file($state_lock, 10, $update);
> +	die $@ if $@;
> +    };
> +
> +    # make sure we have guest_migration_lock during update
> +    PVE::GuestHelpers::guest_migration_lock($vmid, undef, $code);
> +}

We already have most of this coed in write_job_state, so I suggest the
following patch instead:

diff --git a/PVE/ReplicationState.pm b/PVE/ReplicationState.pm
index 6a743a3..8e30e07 100644
--- a/PVE/ReplicationState.pm
+++ b/PVE/ReplicationState.pm
@@ -84,6 +84,7 @@ sub read_job_state {
 }

 # update state for a single job
+# pass $state = undef to delete the job state completely
 sub write_job_state {
     my ($jobcfg, $state) = @_;

@@ -96,8 +97,12 @@ sub write_job_state {

        my $stateobj = read_state();
        # Note: tuple ($vmid, $tid) is unique
-       $stateobj->{$vmid}->{$tid} = $state;
-
+       if (defined($state)) {
+           $stateobj->{$vmid}->{$tid} = $state;
+       } else {
+           delete $stateobj->{$vmid}->{$tid};
+           delete $stateobj->{$vmid} if !%{$stateobj->{$vmid}};
+       }
        PVE::Tools::file_set_contents($state_path, encode_json($stateobj));
     };




More information about the pve-devel mailing list