From bcaa335663af643459e537477c03bb47b4135327 Mon Sep 17 00:00:00 2001 From: janw Date: Mon, 7 Apr 2008 12:58:44 +0000 Subject: [PATCH] Added DEBUG output for sql statements. Added jobdb merge and cleanup. Closes #399 git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@10240 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/gosa-si-server | 153 +++++++++++++++++++++--------------- gosa-si/modules/DBsqlite.pm | 1 + 2 files changed, 92 insertions(+), 62 deletions(-) diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index e18617b87..657f40e37 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -437,7 +437,7 @@ sub sig_int_handler { daemon_log("shutting down gosa-si-server", 1); - system("killall gosa-si-server"); + system("kill `ps -C gosa-si-server -o pid=`"); } $SIG{INT} = \&sig_int_handler; @@ -1233,7 +1233,7 @@ sub process_task { sub trigger_db_loop { my ($kernel) = @_ ; $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay); - $kernel->delay_set('watch_for_done_jobs', $job_queue_loop_delay); + $kernel->delay_set('watch_for_done_jobs', $job_queue_loop_delay); } sub watch_for_done_jobs { @@ -1256,35 +1256,65 @@ sub watch_for_new_jobs { my ($kernel,$heap) = @_[KERNEL, HEAP]; # check gosa job queue for jobs with executable timestamp - my $timestamp = &get_time(); - my $sql_statement = "SELECT * FROM ".$job_queue_tn. - " WHERE status='waiting' AND timestamp<'$timestamp'"; - my $res = $job_db->select_dbentry( $sql_statement ); + my $timestamp = &get_time(); + my $sql_statement = "SELECT * FROM $job_queue_tn WHERE status='waiting' AND (CAST (timestamp AS INTEGER) + 120) < $timestamp ORDER BY timestamp"; + my $res = $job_db->exec_statement( $sql_statement ); + + # Merge all new jobs that would do the same actions + my @drops; + my $hits; + foreach my $hit (reverse @{$res} ) { + my $macaddress= lc @{$hit}[8]; + my $headertag= @{$hit}[5]; + if(defined($hits->{$macaddress}->{$headertag})) { + push @drops, "DELETE FROM $job_queue_tn WHERE id = '$hits->{$macaddress}->{$headertag}[0]'"; + } + $hits->{$macaddress}->{$headertag}= $hit; + } - while( my ($id, $hit) = each %{$res} ) { - my $jobdb_id = $hit->{id}; - my $macaddress = $hit->{'macaddress'}; - my $job_msg = $hit->{'xmlmessage'}; - daemon_log("J DEBUG: its time to execute $job_msg", 7); - my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$macaddress'"; - my $res_hash = $known_clients_db->select_dbentry( $sql_statement ); - # expect macaddress is unique!!!!!! - my $target = $res_hash->{1}->{hostname}; - - # change header - $job_msg =~ s/
job_/
gosa_/; - - # add sqlite_id - $job_msg =~ s/<\/xml>$/$jobdb_id<\/jobdb_id><\/xml>/; - - $job_msg =~ /
(\S+)<\/header>/; - my $header = $1 ; - my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header, "J"); - - # update status in job queue to 'processing' - $sql_statement = "UPDATE $job_queue_tn SET status='processing' WHERE id='$jobdb_id'"; - my $res = $job_db->update_dbentry($sql_statement); - } + # Delete new jobs with a matching job in state 'processing' + foreach my $macaddress (keys %{$hits}) { + foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) { + my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0]; + my $sql_statement = "SELECT * FROM $job_queue_tn WHERE macaddress LIKE '$macaddress' AND headertag='$jobdb_headertag' AND status='processing'"; + my $res = $job_db->exec_statement( $sql_statement ); + foreach my $hit (@{$res}) { + push @drops, "DELETE FROM $job_queue_tn WHERE id = '$jobdb_id'"; + } + } + } + + # Commit deletion + $job_db->exec_statementlist(\@drops); + + # Look for new jobs that could be executed + foreach my $macaddress (keys %{$hits}) { + foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) { + my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0]; + my $job_msg = @{$hits->{$macaddress}->{$jobdb_headertag}}[7]; + + daemon_log("J DEBUG: its time to execute $job_msg", 7); + my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$macaddress'"; + my $res_hash = $known_clients_db->select_dbentry( $sql_statement ); + + # expect macaddress is unique!!!!!! + my $target = $res_hash->{1}->{hostname}; + + # change header + $job_msg =~ s/
job_/
gosa_/; + + # add sqlite_id + $job_msg =~ s/<\/xml>$/$jobdb_id<\/jobdb_id><\/xml>/; + + $job_msg =~ /
(\S+)<\/header>/; + my $header = $1 ; + my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header, "J"); + + # update status in job queue to 'processing' + $sql_statement = "UPDATE $job_queue_tn SET status='processing' WHERE id='$jobdb_id'"; + my $res = $job_db->update_dbentry($sql_statement); + } + } $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay); } @@ -1373,38 +1403,37 @@ sub change_fai_state { filter => "$search" ); - if ($mesg->count) { - my @entries = $mesg->entries; - foreach my $entry (@entries) { - # Only modify entry if it is not set to '$state' - if ($entry->get_value("FAIstate") ne "$state"){ - daemon_log("$session_id INFO: Setting FAIstate to '$state' for ".$entry->dn, 5); - my $result; - my %tmp = map { $_ => 1 } $entry->get_value("objectClass"); - if (exists $tmp{'FAIobject'}){ - if ($state eq ''){ - $result= $ldap_handle->modify($entry->dn, changes => [ - delete => [ FAIstate => [] ] ]); - } else { - $result= $ldap_handle->modify($entry->dn, changes => [ - replace => [ FAIstate => $state ] ]); - } - } elsif ($state ne ''){ - $result= $ldap_handle->modify($entry->dn, changes => [ - add => [ objectClass => 'FAIobject' ], - add => [ FAIstate => $state ] ]); - } - - # Errors? - if ($result->code){ - daemon_log("$session_id Error: Setting FAIstate to '$state' for ".$entry->dn. "failed: ".$result->error, 1); - } - - } else { - daemon_log("$session_id DEBUG FAIstate at host '$_' already at state '$st'", 7); - } - } - } + if ($mesg->count) { + my @entries = $mesg->entries; + foreach my $entry (@entries) { + # Only modify entry if it is not set to '$state' + if ($entry->get_value("FAIstate") ne "$state"){ + daemon_log("$session_id INFO: Setting FAIstate to '$state' for ".$entry->dn, 5); + my $result; + my %tmp = map { $_ => 1 } $entry->get_value("objectClass"); + if (exists $tmp{'FAIobject'}){ + if ($state eq ''){ + $result= $ldap_handle->modify($entry->dn, changes => [ + delete => [ FAIstate => [] ] ]); + } else { + $result= $ldap_handle->modify($entry->dn, changes => [ + replace => [ FAIstate => $state ] ]); + } + } elsif ($state ne ''){ + $result= $ldap_handle->modify($entry->dn, changes => [ + add => [ objectClass => 'FAIobject' ], + add => [ FAIstate => $state ] ]); + } + + # Errors? + if ($result->code){ + daemon_log("$session_id Error: Setting FAIstate to '$state' for ".$entry->dn. "failed: ".$result->error, 1); + } + } else { + daemon_log("$session_id DEBUG FAIstate at host '".$entry->dn."' already at state '$st'", 7); + } + } + } # if no ldap handle defined } else { daemon_log("$session_id ERROR: no LDAP handle defined for update FAIstate", 1); diff --git a/gosa-si/modules/DBsqlite.pm b/gosa-si/modules/DBsqlite.pm index 0264a9011..cf53043a0 100644 --- a/gosa-si/modules/DBsqlite.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -116,6 +116,7 @@ sub add_dbentry { } my $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES ('".join("', '", @val_list)."')"; + &main::daemon_log("DEBUG: $sql_statement", 7); my $db_res = $self->{dbh}->do($sql_statement); if( $db_res != 1 ) { return (4, $sql_statement); -- 2.30.2