A 'waiting' state is introduced and other 'waiting' and 'syncing'
instances of the same job are now detected by moving the check out
from the sync lock. There are two new checks that allow disabling a job
while it is 'syncing' or 'waiting'. Previously when sync finished it would
re-enable such a job involuntarily.
Disabling a 'waiting' job causes it to not sync anymore.

Signed-off-by: Fabian Ebner <f.eb...@proxmox.com>
---

Changes from v2:
    * Split into 2 patches

Changes from v1:
    * Added 2 additional checks to not involuntarily re-enable a disabled job

 pve-zsync | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/pve-zsync b/pve-zsync
index f7bf5bd..677e937 100755
--- a/pve-zsync
+++ b/pve-zsync
@@ -581,18 +581,32 @@ sub destroy_job {
 sub sync {
     my ($param) = @_;
 
-    my $date = get_date();
+    my $date;
     my $job;
     my $dest;
     my $source;
     my $vm_type;
 
+    locked("$CONFIG_PATH/cron_and_state.lock", sub {
+       $job = eval { get_job($param); };
+
+       if ($job) {
+           if (defined($job->{state}) && ($job->{state} eq "syncing" || 
$job->{state} eq "waiting")) {
+               die "Job --source $param->{source} --name $param->{name} is 
already scheduled to sync\n";
+           }
+           $job->{state} = "waiting";
+           update_state($job);
+       }
+    });
+
     locked("$CONFIG_PATH/sync.lock", sub {
        locked("$CONFIG_PATH/cron_and_state.lock", sub {
+           #job might've changed since we waited for the sync lock, but we can 
be sure it's not syncing
+           $date = get_date();
            eval { $job = get_job($param); };
 
-           if ($job && defined($job->{state}) && $job->{state} eq "syncing") {
-               die "Job --source $param->{source} --name $param->{name} is 
syncing at the moment";
+           if ($job && defined($job->{state}) && $job->{state} eq "stopped") {
+               die "Job --source $param->{source} --name $param->{name} has 
been disabled\n";
            }
 
            $dest = parse_target($param->{dest});
@@ -647,11 +661,18 @@ sub sync {
            die "$err\n";
        }
 
-       if ($job) {
-           $job->{state} = "ok";
-           $job->{lsync} = $date;
-           locked("$CONFIG_PATH/cron_and_state.lock", sub { 
update_state($job); });
-       }
+       locked("$CONFIG_PATH/cron_and_state.lock", sub {
+           eval { $job = get_job($param); };
+           if ($job) {
+               if (defined($job->{state}) && $job->{state} eq "stopped") {
+                   $job->{state} = "stopped";
+               } else {
+                   $job->{state} = "ok";
+               }
+               $job->{lsync} = $date;
+               update_state($job);
+           }
+       });
     }); #sync lock
 }
 
-- 
2.20.1


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to