diff options
author | Preston Cody <codeman@gentoo.org> | 2008-01-05 05:15:51 +0000 |
---|---|---|
committer | Preston Cody <codeman@gentoo.org> | 2008-01-05 05:15:51 +0000 |
commit | f0d5d991f23122e323c720e8c7bcea5ec15471ba (patch) | |
tree | c3ca4eb64bc170d009b95670b381a9669948d6cb | |
parent | move SERVER_STDIN and SERVER_STDOUT inside the class hash (diff) | |
download | scire-f0d5d991f23122e323c720e8c7bcea5ec15471ba.tar.gz scire-f0d5d991f23122e323c720e8c7bcea5ec15471ba.tar.bz2 scire-f0d5d991f23122e323c720e8c7bcea5ec15471ba.zip |
whole bunch of updates here.
first, created run_query and put the process of debug/prepare/execute into it.
much simpler and cleaner!
added RETURN_JOBFILE cmd and stub for JOBFILE_SENT.
add a check to set_job_status to see if we're marking a job as finished.
if so, it needs to do some more work. that part isn't coded yet.
svn path=/branches/new-fu/; revision=327
-rwxr-xr-x | server/scireserver.pl | 150 |
1 files changed, 87 insertions, 63 deletions
diff --git a/server/scireserver.pl b/server/scireserver.pl index e3d201b..68d6fe9 100755 --- a/server/scireserver.pl +++ b/server/scireserver.pl @@ -54,6 +54,7 @@ while(<>) { my ($command, @args) = parse_command($_); # chomp( my $line = $_); # debug("DEBUG: line is: $line"); +# SEE http://agaffney.org/mediawiki/index.php/SSH-based_protocol for documentation on the protocol. if($command eq "QUIT") { print "OK\n"; @@ -89,7 +90,14 @@ while(<>) { } elsif ($command eq "SET_JOB_STATUS") { my ($jobid,$status) = @args; set_job_status($jobid,$client_id,$status) and print "OK\n"; - + } elsif ($command eq "RETURN_JOBFILE") { + my $jobid = $args[0]; + my $filename = "$conf{job_dir}/$client_id/result/$jobid.result"; + print "OK ${filename}\n"; + } elsif ($command eq "JOBFILE_SENT") { + my $filename = $args[0]; + print "OK\n" and process_jobfile($filename); + } else { print "ERROR The command $command is unknown. Please try again.\n"; } @@ -127,50 +135,36 @@ sub register_client { eval { $query = 'SELECT statusid FROM client_status WHERE statusname = "Pending"'; - debug("Query is $query"); -# $status_id = "4"; #db.conn.GetRow($query) - $sth = $dbh->prepare($query); - $sth->execute(); + $sth = run_query($query); $status_id = $sth->fetchrow_hashref->{'statusid'}; }; ($@) and print "ERROR Could not get status id: $DBI::errstr\n"; eval { - $query = 'LOCK TABLES `gacl_axo_seq` WRITE'; - debug("Query is $query"); + run_query('LOCK TABLES `gacl_axo_seq` WRITE'); +# debug("Query is $query"); #execute it - $dbh->do($query); +# $dbh->do($query); $query = 'SELECT id FROM `gacl_axo_seq`'; - debug("Query is $query"); - #$id = "56"; #execute $query - $sth = $dbh->prepare($query); - $sth->execute(); + $sth = run_query($query); $id = $sth->fetchrow_hashref->{'id'}; $id += 1; $query = 'UPDATE `gacl_axo_seq` SET id=?'; - debug("Query is $query"); - #execute with $id - $sth = $dbh->prepare($query); - $sth->execute($id); - $query = 'UNLOCK TABLES'; - debug("Query is $query"); - $dbh->do($query); + run_query($query,$id); + run_query('UNLOCK TABLES'); +# debug("Query is $query"); +# $dbh->do($query); }; ($@) and print "ERROR during fetching of id sequence: $DBI::errstr\n"; eval { $query = 'INSERT INTO `gacl_axo` (id,section_value,value,order_value,name,hidden) VALUES (?,"clients",?,"1",?,"0")'; - debug("Query is $query"); - $sth = $dbh->prepare($query); - $sth->execute($id, $hostname, $hostname); - #execute with $id, $hostname, $hostname + run_query($query,$id,$hostname,$hostname); #NOTE: not sure if this query is still valid. may be using id instead of hostname for one of those two now. $query = 'INSERT INTO clients (clientid,digest,hostname,mac,ip,status) VALUES (?,?,?,?,?,?)'; - debug("Query is $query"); #execute with $id, client_cert.digest("sha1"),crypto.dump_certificate(crypto.FILETYPE_PEM,client_cert),$hostname,$mac,$ip,$status_id)) - $sth = $dbh->prepare($query); - $sth->execute($id,$digest,$hostname,$mac,$ip,$status_id); + run_query($query,$id,$digest,$hostname,$mac,$ip,$status_id); }; ($@) and print "ERROR Could not insert client with $query: $DBI::errstr\n"; #FIXME look for "duplicate key" and if found fail and notify admin. @@ -186,14 +180,15 @@ sub identify_client { $digest =~ /^[A-Za-z0-9]+$/ or print "ERROR invalid digest!\n"; my $query = 'SELECT client_status.statusname, clients.clientid FROM clients JOIN client_status on (clients.status = client_status.statusid) WHERE clients.digest=?'; - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($digest); + my $sth = run_query($query,$digest); + #debug("Query is $query"); + #my $sth = $dbh->prepare($query); + #$sth->execute($digest); my $hashref = $sth->fetchrow_hashref(); debug(Dumper($hashref)); my $status_name = $hashref->{'statusname'}; $client_id = $hashref->{'clientid'}; - if ($client_id > 0) { #and ($status_name eq 'Active') { + if (defined($client_id) and $client_id > 0) { #and ($status_name eq 'Active') { $identified = 1; print "OK\n"; } else { @@ -218,9 +213,10 @@ sub get_jobs { EndOfQuery #FIXME ADD JOB DEPENDENCIES TO THIS QUERY. - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($client_id); + my $sth = run_query($query,$client_id); +# debug("Query is $query"); +# my $sth = $dbh->prepare($query); +# $sth->execute($client_id); my $jobs_ref = $sth->fetchall_arrayref(); # Don't ask me...ask the guys in #perl :P my @jobs = map { @$_ } @$jobs_ref; @@ -231,16 +227,12 @@ sub get_job { my $jobid = shift; #Validate your inputs! my $query = 'SELECT * FROM jobs LEFT JOIN job_conditions on (jobs.jobid) WHERE jobs.jobid = ?'; - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($jobid); + my $sth = run_query($query, $jobid); my $job = $sth->fetchrow_hashref(); my $scriptid = $job->{'script'}; $query = 'SELECT * FROM scripts WHERE scriptid=?'; - debug("Query is $query"); - $sth = $dbh->prepare($query); - $sth->execute($scriptid); + $sth = run_query($query,$scriptid); $job->{'script'} = $sth->fetchrow_hashref(); debug(Dumper($job)); @@ -266,9 +258,7 @@ sub job_fetched { eval { my $query = 'DELETE FROM jobs_clients WHERE jobid=? AND clientid=?'; - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($jobid,$client_id); + run_query($query,$jobid,$client_id); }; ($@) and print "ERROR Could not get status id: $DBI::errstr\n"; @@ -288,10 +278,7 @@ sub set_job_status { my $status_id; eval { my $query = 'SELECT statusid FROM jobs_status WHERE statusname = ?'; - debug("Query is $query"); -# $status_id = "4"; #db.conn.GetRow($query) - my $sth = $dbh->prepare($query); - $sth->execute($status); + my $sth = run_query($query,$status); $status_id = $sth->fetchrow_hashref->{'statusid'}; }; ($@) and print "ERROR Could not get status id: $DBI::errstr\n"; @@ -299,11 +286,18 @@ sub set_job_status { eval { my $query = 'INSERT INTO job_history (jobid,clientid,statusid,eventmsg) VALUES (?,?,?,?)'; - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($jobid,$id_of_client,$status_id,$eventmsg); + run_query($query,$jobid,$id_of_client,$status_id,$eventmsg); }; ($@) and print "ERROR Could not insert into job_history: $DBI::errstr\n"; + + + #If we're marking the completetion or failure of a job, we have more work to do here. + if ($status eq 'Failed') { + mark_job_as_failed($jobid,$id_of_client); + } elsif ($status eq 'Completed') { + mark_job_as_completed($jobid,$id_of_client); + } + return 1; } @@ -318,6 +312,15 @@ sub parse_command { return @parts; } +sub run_query { + my ($query, @params) = @_; + debug("Query is $query"); + my $sth = $dbh->prepare($query); + $sth->execute(@params); + return $sth; +} + + sub expand_jobs { #Searches for the group jobs that the client must be into and does the expansion. my @groups = get_client_groups(); @@ -333,39 +336,60 @@ AND (job_conditions.deploy_time < now()) AND ((job_conditions.expiration_time > now()) OR (job_conditions.expiration_time IS NULL)) AND ((job_conditions.last_run_date < job_conditions.deploy_time) OR (job_conditions.last_run_date IS NULL)) EndOfQuery2 - debug("Query is $query"); - my $sth = $dbh->prepare($query); - $sth->execute($groupid); - $dbh->do('LOCK TABLES `jobs_clients` WRITE, `job_conditions` WRITE, `job_history` WRITE, `jobs_status` WRITE'); + my $sth = run_query($query,$groupid); + run_query('LOCK TABLES `jobs_clients` WRITE, `job_conditions` WRITE, `job_history` WRITE, `jobs_status` WRITE, `jobs` WRITE'); + #FIXME need to lock jobs_clients for READ as well!!! while( my $jobref = $sth->fetchrow_hashref() ) { my $jobid = $jobref->{'jobid'}; foreach my $member (@members) { $query = 'INSERT INTO jobs_clients (jobid, clientid) VALUES (?,?)'; - debug("Query is $query"); - my $sth2 = $dbh->prepare($query); - $sth2->execute($jobid,$member); + my $sth2 = run_query($query,$jobid,$member); set_job_status($jobid,$member,'Pending', 'Job expanded.') or print "ERROR could not add expanded jobs to job_history.\n"; } $query = 'UPDATE `job_conditions` SET last_run_date = now() WHERE jobid = ?'; - debug("Query is $query"); - my $sth3 = $dbh->prepare($query); - $sth3->execute($jobid); + run_query($query,$jobid); + + $query = 'UPDATE `jobs` SET pending=pending+? WHERE jobid = ?'; + run_query($query,$#members,$jobid); #This works because you want one less b/c of removing the group. # One last query to remove the row from jobs_clients so someone else doesn't expand it. $query = 'DELETE FROM `jobs_clients` WHERE groupid=? AND jobid=?'; - debug("Query is $query"); - my $sth4 = $dbh->prepare($query); - $sth4->execute($groupid,$jobid); + run_query($query,$groupid,$jobid); + } - $dbh->do('UNLOCK TABLES'); + run_query('UNLOCK TABLES'); }; ($@) and print "ERROR Could not expand jobs: $@ $DBI::errstr\n"; return undef; } } +sub mark_job_as_failed { + my ($jobid,$id_of_client) = @_; +} + +sub mark_job_as_completed { + my ($jobid,$id_of_client) = @_; + my ($query,$sth); + debug("Marking $jobid as completed for client $id_of_client"); + #If we succeeded, we need to check this jobid to see if it is a recurring job, and then set the next_run if necessary. + #This requries looking at the pending count for the job as well as the run_schedule. + + #First off, update the pending count now that we've finished. + eval { + $query = 'UPDATE jobs SET pending=pending-1 WHERE jobid=?'; + debug("Query is $query"); + }; + ($@) and print "ERROR Could not update pending count: $@ $DBI::errstr\n"; +} + +sub process_jobfile { + my $filename = shift; + +} + ######################################################### # PHPGACL FUNCTIONS ######################################################### |