From bd862b2e5ed6c9b9ebf7cf01288a79f7b0ffb2a5 Mon Sep 17 00:00:00 2001 From: Digimer Date: Mon, 27 Aug 2018 02:51:53 -0400 Subject: [PATCH] * Made more progress on anvil-daemon's invokation of jobs. * Got anvil-update-system clearing job data when (re)starting Signed-off-by: Digimer --- Anvil/Tools/System.pm | 5 ++++ share/words.xml | 2 ++ tools/anvil-daemon | 42 +++++++++++++++------------- tools/anvil-update-system | 59 ++++++++++++++++++++++++++++----------- 4 files changed, 71 insertions(+), 37 deletions(-) diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index 2cfb9703..a4339107 100755 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -244,9 +244,14 @@ sub call $process->redirect_output(undef, $stderr_file); } + # Start the process + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0204", variables => { call => $shell_call }}); my $status = $process->start($shell_call); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { status => $status }}); + # Report that it started with PID. + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0205", variables => { call => $shell_call, pid => $process->pid }}); + # We'll return the handle instead of output. $output = $process; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { output => $output }}); diff --git a/share/words.xml b/share/words.xml index 6c6d18c8..a59ca789 100644 --- a/share/words.xml +++ b/share/words.xml @@ -327,6 +327,8 @@ The database connection error was: Unable to connect to any database. Will try to initialize the local system and then try again. Failed to connect to any databases. Skipping the loop of the daemon. Disconnected from all databases. Will reconnect when entering the main loop. + Starting the background process: [#!variable!call!#] now. + Background process: [#!variable!call!#] running with PID: [#!variable!pid!#]. Test diff --git a/tools/anvil-daemon b/tools/anvil-daemon index db85009d..5f5fc0c9 100755 --- a/tools/anvil-daemon +++ b/tools/anvil-daemon @@ -315,30 +315,31 @@ WHERE next if exists $anvil->data->{pids}{$job_picked_up_by}; # The previous job is gone, but the job isn't finished. Start it again. - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "striker_warning_0007", variables => { + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "striker_warning_0007", variables => { command => $job_command, pid => $job_picked_up_by, percent => $job_progress, }}); clear_job($anvil, $job_uuid); } - - # Start the job, appending '--job-uuid' to the command. - $anvil->data->{jobs}{handles}{$job_uuid} = $anvil->System->call({ - debug => 2, - background => 1, - stdout_file => "/tmp/anvil.job.".$job_uuid.".stdout", - stderr_file => "/tmp/anvil.job.".$job_uuid.".stderr", - shell_call => $job_command." --job-uuid ".$job_uuid, - source => $THIS_FILE, - line => __LINE__, - }); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "jobs::handles::${job_uuid}" => $anvil->data->{jobs}{handles}{$job_uuid} }}); - - # Record the PID - my $pid = $anvil->data->{jobs}{handles}{$job_uuid}->pid(); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { pid => $pid }}); - my $query = " + else + { + # Start the job, appending '--job-uuid' to the command. + $anvil->data->{jobs}{handles}{$job_uuid} = $anvil->System->call({ + debug => 2, + background => 1, + stdout_file => "/tmp/anvil.job.".$job_uuid.".stdout", + stderr_file => "/tmp/anvil.job.".$job_uuid.".stderr", + shell_call => $job_command." --job-uuid ".$job_uuid, + source => $THIS_FILE, + line => __LINE__, + }); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "jobs::handles::${job_uuid}" => $anvil->data->{jobs}{handles}{$job_uuid} }}); + + # Record the PID + my $pid = $anvil->data->{jobs}{handles}{$job_uuid}->pid(); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { pid => $pid }}); + my $query = " UPDATE jobs SET @@ -347,8 +348,9 @@ SET WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid)." "; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); + $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + } } # Close the jobs file. diff --git a/tools/anvil-update-system b/tools/anvil-update-system index 7e21fd00..01752ee5 100755 --- a/tools/anvil-update-system +++ b/tools/anvil-update-system @@ -15,6 +15,7 @@ # 1 = No database connections available. # 2 = The job UUID was passed, but it wasn't valid. # 3 = It looks like the update failed, reset progress to '0'. +# use strict; use warnings; @@ -38,6 +39,9 @@ $anvil->Storage->read_config({file => "/etc/anvil/anvil.conf"}); $anvil->data->{switches}{'job-uuid'} = ""; $anvil->Get->switches; +# Log that we've started. +$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }}); + # Connect to DBs. $anvil->Database->connect; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); @@ -50,6 +54,7 @@ if (not $anvil->data->{sys}{database}{connections}) } # Did we get called with a job UUID? +$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'} }}); if ($anvil->data->{switches}{'job-uuid'}) { # Is it set and valid? @@ -106,11 +111,14 @@ WHERE }}); } +# Clea any old runs. +update_progress($anvil, 0, "clear"); + # Mark that we're starting +print $THIS_FILE."; time: [".time."], running with PID: [".$$."]\n"; update_progress($anvil, 1, "message_0033"); - -print $THIS_FILE." running with PID: [".$$."]\n" sleep 60; +print $THIS_FILE."; time: [".time."], exiting\n"; exit; run_os_update($anvil); @@ -140,12 +148,24 @@ sub update_progress my ($anvil, $progress, $message) = @_; # Log the progress percentage. - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { progress => $progress }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + progress => $progress, + message => $message, + "jobs::job_uuid" => $anvil->data->{jobs}{job_uuid}, + }}); if ($anvil->data->{jobs}{job_uuid}) { # Get the current job_status and append this new one. - my $query = " + my $job_picked_up_by = $$; + my $job_status = ""; + if ($message eq "clear") + { + $job_picked_up_by = 0; + } + else + { + my $query = " SELECT job_status FROM @@ -153,26 +173,31 @@ FROM WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{jobs}{job_uuid})." ;"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - - my $job_status = $anvil->Database->query({uuid => $uuid, debug => $debug, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; - $job_status = "" if not defined $old_job_status; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { job_status => $job_status }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { message => $message }}); - if ($message) - { - $job_status .= $message."\n"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { job_status => $job_status }}); + $job_status = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; + $job_status = "" if not defined $job_status; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_status => $job_status }}); + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { message => $message }}); + if ($job_status) + { + $job_status .= "\n"; + } + if ($message) + { + $job_status .= $message; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_status => $job_status }}); + } } my $query = " UPDATE jobs SET - job_picked_up_by = ".$anvil->data->{sys}{database}{use_handle}->quote($$).", - job_updated = now(), - job_progress = ".$progress.", + job_picked_up_by = ".$anvil->data->{sys}{database}{use_handle}->quote($job_picked_up_by).", + job_updated = ".time.", + job_progress = ".$anvil->data->{sys}{database}{use_handle}->quote($progress).", job_status = ".$anvil->data->{sys}{database}{use_handle}->quote($job_status).", modified_date = ".$anvil->data->{sys}{database}{use_handle}->quote($anvil->data->{sys}{database}{timestamp})." WHERE