Code

Added DEBUG output for sql statements.
authorjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 7 Apr 2008 12:58:44 +0000 (12:58 +0000)
committerjanw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 7 Apr 2008 12:58:44 +0000 (12:58 +0000)
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
gosa-si/modules/DBsqlite.pm

index e18617b874502a3dda7fa90c27410788da7bab24..657f40e37e5b477aa50c6f57906038eef61049d1 100755 (executable)
@@ -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/<header>job_/<header>gosa_/;
-
-               # add sqlite_id 
-        $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
-
-        $job_msg =~ /<header>(\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/<header>job_/<header>gosa_/;
+
+                       # add sqlite_id 
+                       $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
+
+                       $job_msg =~ /<header>(\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); 
index 0264a9011584f24405e8bce0f644973b95ebaeb8..cf53043a0d5bdb279bc0fd5fc3ee5728a74e59a2 100644 (file)
@@ -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);