all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager] fix #3731: pvescheduler: Improve fork behaviour
Date: Tue, 22 Aug 2023 11:20:15 +0200	[thread overview]
Message-ID: <20230822092015.23709-1-f.schauer@proxmox.com> (raw)

Instead of forking every minute, check whether a job has to be run and
only fork if there is a pending job.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
Backup jobs are registered in the jobs.cfg file, while replication jobs
are registered in the replication.cfg file. Integrating replication into
PVE::Jobs would move replication jobs into jobs.cfg, breaking backwards
compatibility.

 PVE/Jobs.pm                 | 21 ++++++++++++++++++++-
 PVE/Service/pvescheduler.pm | 11 ++++-------
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/PVE/Jobs.pm b/PVE/Jobs.pm
index bd323332..5643bd15 100644
--- a/PVE/Jobs.pm
+++ b/PVE/Jobs.pm
@@ -258,13 +258,14 @@ sub get_last_runtime {
     return $state->{time} // 0;
 }
 
-sub run_jobs {
+sub get_pending {
     my ($first_run) = @_;
 
     synchronize_job_states_with_config();
 
     my $jobs_cfg = cfs_read_file('jobs.cfg');
     my $nodename = PVE::INotify::nodename();
+    my @pending_jobs = ();
 
     foreach my $id (sort keys %{$jobs_cfg->{ids}}) {
 	my $cfg = $jobs_cfg->{ids}->{$id};
@@ -292,6 +293,24 @@ sub run_jobs {
 
 	next if !defined($next_sync) || time() < $next_sync; # not yet its (next) turn
 
+	push @pending_jobs, $id;
+    }
+
+    return @pending_jobs;
+}
+
+sub run_jobs {
+    my (@scheduled_jobs) = @_;
+
+    synchronize_job_states_with_config();
+
+    my $jobs_cfg = cfs_read_file('jobs.cfg');
+
+    foreach my $id (@scheduled_jobs) {
+	my $cfg = $jobs_cfg->{ids}->{$id} or next;
+	my $type = $cfg->{type};
+	my $schedule = delete $cfg->{schedule};
+
 	my $plugin = PVE::Job::Registry->lookup($type);
 	if (starting_job($id, $type)) {
 	    PVE::Cluster::cfs_update();
diff --git a/PVE/Service/pvescheduler.pm b/PVE/Service/pvescheduler.pm
index 40be5977..7b6b3e99 100755
--- a/PVE/Service/pvescheduler.pm
+++ b/PVE/Service/pvescheduler.pm
@@ -100,19 +100,16 @@ sub run {
     my $first_run = 1;
 
     my $run_jobs = sub {
-	# TODO: actually integrate replication in PVE::Jobs and do not always fork here, we could
-	# do the state lookup and check if there's new work scheduled before doing so, e.g., by
-	# extending the PVE::Jobs interfacae e.g.;
-	# my $scheduled_jobs = PVE::Jobs::get_pending() or return;
-	# forked { PVE::Jobs::run_jobs($scheduled_jobs) }
+	# TODO: actually integrate replication in PVE::Jobs and do not always fork here
 
 	$fork->('replication', sub {
 	    PVE::API2::Replication::run_jobs(undef, sub {}, 0, 1);
 	});
 
+	my @pending_jobs = PVE::Jobs::get_pending($first_run);
 	$fork->('jobs', sub {
-	    PVE::Jobs::run_jobs($first_run);
-	});
+	    PVE::Jobs::run_jobs(@pending_jobs);
+	}) if (@pending_jobs);
 
 	$first_run = 0;
     };
-- 
2.39.2





                 reply	other threads:[~2023-08-22  9:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230822092015.23709-1-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal