From df0d0731201ddd2b8f8cdfa970bb769ddcf0c256 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 17 Mar 2006 02:07:50 +0000 Subject: [PATCH] adding delays and more debug --- etc/spectre.conf.original | 15 ++++++++++++++- lib/Spectre/Workflow.pm | 17 ++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/etc/spectre.conf.original b/etc/spectre.conf.original index 760bcad65..3957f2d43 100644 --- a/etc/spectre.conf.original +++ b/etc/spectre.conf.original @@ -20,7 +20,20 @@ # but depending upon the amount of editing and workflows you have # on your site, you may not need that many. -"maxWorkers" : 3 +"maxWorkers" : 3, + +# How many seconds should Spectre wait between spawning jobs. This +# can help avoid creating a denial of service attack on overworked +# or underpowered servers. + +"timeBetweenJobs" : 5, + +# The number of seconds that Spectre should wait after an activity +# has been suspended before it should start it back up again. An +# activity may be suspended if it's waiting on user input, or +# if it returns an error. + +"delayAfterSuspension" : 500 } diff --git a/lib/Spectre/Workflow.pm b/lib/Spectre/Workflow.pm index 8142266d0..64660d7ff 100644 --- a/lib/Spectre/Workflow.pm +++ b/lib/Spectre/Workflow.pm @@ -106,6 +106,7 @@ sub checkJobs { $kernel->yield("runWorker",$job); } } + $kernel->delay_set("checkJobs",$self->config->get("timeBetweenJobs")); } #------------------------------------------------------------------- @@ -186,6 +187,10 @@ sub getNextJob { $self->debug("Looking for a workflow instance to execute."); foreach my $priority (1..3) { foreach my $job (@{$self->{"_priority".$priority}}) { + if (time() > $job->{statusDelay} & $job->{status}) { + delete $job->{statusDelay}; + $job->{status} eq "waiting"; + } if ($job->{status} eq "waiting") { $self->debug("Looks like ".$job->{instanceId}." would be a good workflow instance to run."); return $job; @@ -288,7 +293,8 @@ sub suspendJob { my $self = shift; my $instanceId = shift; $self->debug("Suspending workflow instance ".$instanceId."."); - $self->{_jobs}{$instanceId}{status} = "waiting"; + $self->{_jobs}{$instanceId}{status} = "delay"; + $self->{_jobs}{$instanceId}{statusDelay} = $self->config->get("delayAfterSuspension") + time(); for (my $i=0; $i < scalar(@{$self->{_runningJobs}}); $i++) { if ($self->{_runningJobs}[$i]{instanceId} eq $instanceId) { splice(@{$self->{_runningJobs}}, $i, 1); @@ -323,9 +329,10 @@ sub workerResponse { my $state = $payload->{state}; if ($state eq "waiting") { $self->debug("Was told to wait on $jobId because we're still waiting on some external event."); - $self->suspendJob($jobId, $self->config->get("waitTime")); + $self->suspendJob($jobId); } elsif ($state eq "complete") { $self->debug("Workflow instance $jobId ran one of it's activities successfully."); + $self->suspendJob($jobId); } elsif ($state eq "disabled") { $self->debug("Workflow instance $jobId is disabled."); $self->deleteJob($jobId); @@ -334,10 +341,10 @@ sub workerResponse { $self->deleteJob($jobId); } elsif ($state eq "error") { $self->debug("Got an error for $jobId."); - $self->suspendJob($jobId, $self->config->get("waitTime")); + $self->suspendJob($jobId); } else { - $self->debug("Something bad happened on the return of $jobId, so we're suspending it's run for a while."); - $self->suspendJob($jobId, $self->config->get("waitTime")); + $self->debug("Something bad happened on the return of $jobId."); + $self->suspendJob($jobId); # something bad happened } } elsif ($response->is_redirect) {