Index: /trunk/oarutils/oar-dispatch
===================================================================
--- /trunk/oarutils/oar-dispatch	(revision 94)
+++ /trunk/oarutils/oar-dispatch	(revision 95)
@@ -15,6 +15,8 @@
 
 my $task = 0;
-my $overload = 1.1;
+my $overload = 1;
 my $file = '';
+my $dir;
+my $cmd;
 my $logtrace;
 my $verbose;
@@ -27,4 +29,6 @@
    'overload=f'   => \$overload,
    'file=s'       => \$file,
+   'dir=s'        => \$dir,
+   'cmd=s'        => \$cmd,
    'logtrace=s'   => \$logtrace,
    'verbose'      => \$verbose,
@@ -62,11 +66,53 @@
 # job to run
 my @job = ();
-open (JOB_LIST, '<', "$file") or die "can't open $file: $!";
-while (<JOB_LIST>) {
-   chomp;
-   next if m/^#/;
-   push @job, $_ if m/^\s*oarsub/;
-   }
-close JOB_LIST;
+#open (JOB_LIST, '<', "$file") or die "can't open $file: $!";
+#while (<JOB_LIST>) {
+#   chomp;
+#   next if m/^#/;
+#   push @job, $_ if m/^\s*oarsub/;
+#   }
+#close JOB_LIST;
+if (-e "$file") {
+   my $job_num = 0;
+   open(JOB_LIST, '<', "$file") or die "error: can't open job file $file: $!";
+   while (my $job_cmd = <JOB_LIST>) {
+      chomp $job_cmd;
+      next if $job_cmd =~ m/^#/;
+      next if $job_cmd =~ m/^\s*$/;
+      # Add oarsub -S if not
+      $job_cmd = "oarsub -S $job_cmd" if $job_cmd !~ m/^\s*oarsub/;
+      $job_num++;
+      my ($job_name) = $job_cmd =~ m/#.*?\bname=(\S+?)\b/i;
+      $job_name ||= $job_num;
+      push @job, {
+         name   => $job_name,
+         cmd    => "$job_cmd",
+         num    => $job_num,
+         };
+      }
+   close JOB_LIST;
+   }
+else {
+   # Add oarsub -S if not
+   $cmd = "oarsub -S $cmd" if $cmd !~ m/^\s*oarsub/;
+   my $job_num = 0;
+   opendir(DIR, $dir) or die "error: can't open folder $dir: $!";
+   while (my $item = readdir(DIR)) {
+      next if $item =~ m/^\./;
+      next if $item =~ m/:/;
+      next if $item =~ m/\.old$/;
+      next if $item =~ m/\.sav$/;
+      next if $item =~ m/\.bak$/;
+      next if $item =~ m/\.no$/;
+      next unless (-d "$dir/$item");
+      $job_num++;
+      push @job, {
+         name   => $item,
+         cmd    => "cd $dir/$item/; $cmd",
+         num    => $job_num,
+         };
+      }
+   closedir DIR;
+   }
 
 my $container_id=$ENV{OAR_JOB_ID};
@@ -79,9 +125,13 @@
    }
 
-
 my $finished = new Coro::Signal;
+my $job_todo = new Coro::Semaphore 0;
+my $job_name_maxlen;
+for (@job) {
+   $job_todo->up;
+   $job_name_maxlen = length($_->{name}) if length($_->{name}) > $job_name_maxlen;
+   }
+
 my $job_active = new Coro::Semaphore 0;
-my $job_todo = new Coro::Semaphore 0;
-$job_todo->up for (@job);
 
 my %scheduled = ();
@@ -115,4 +165,22 @@
    JOB:
    for my $job (@job) {
+      my $job_name   = $job->{name};
+      my $job_cmd    = $job->{cmd};
+
+      # job has been already run ?
+      if (exists $state{$job_name}) {
+         if ($state{$job_name} eq 'start') {
+            print "warning: job $job_name was not clearly finished, relaunching...\n"
+               if $verbose;
+            }
+         elsif ($state{$job_name} eq 'end') {
+            delete $state{$job_name}; # free memory
+            $job_todo->down;
+            print "warning: job $job_name already run\n" if $verbose;
+            cede;
+            next JOB;
+            }
+         }
+
       while ($job_active->count >= $task*$overload) {
          cede;
@@ -122,11 +190,18 @@
       last JOB if $oar_checkpoint->count() > 0;
 
-      $job =~ s/^\s*oarsub//;
-      print "oarsub $insert_oar_option $job" if $verbose;
-      my $job_id = `oarsub $insert_oar_option $job|grep ^OAR_JOB_ID|cut -f 2 -d '='`;
+      $job_cmd =~ s/^\b(oarsub)\b/$1 $insert_oar_option/;
+      print "$job_cmd" if $verbose;
+      my $job_id = `$job_cmd|grep ^OAR_JOB_ID|cut -f 2 -d '='`;
       chomp $job_id;
       if ($job_id > 1) {
-         $scheduled{$job_id}++;
+         $scheduled{$job_id} = {
+            name         => $job_name,
+            };
          $job_active->up;
+
+         my $msg = sprintf "start job %${job_name_maxlen}s / %i at %s\n",
+            $job_name, $job_id, time;
+         $log_h->print($msg) if $logtrace;
+         print($msg) if $verbose;
          }
       cede;
@@ -142,7 +217,18 @@
             last GUARD if $is_finish =~ m/Terminated/;
             }
-         delete $scheduled{$job_id};
+
+         my $msg = sprintf "end   job %${job_name_maxlen}s / %i at %s\n",
+            $scheduled{$job_id}->{name}, $job_id, time;
+
+            # Job non finish, just suspend if received checkpoint signal
+            $msg =~ s/^end\s+job/suspend job/
+               if $sig_transmit and $oar_checkpoint->count() > 0;
+
+            $log_h->print($msg) if $logtrace;
+            print($msg) if $verbose;
+
          $job_active->down;
          $job_todo->down;
+         delete $scheduled{$job_id};
          } $job_id;
       }
