]> jspc29.x-matter.uni-frankfurt.de Git - coral.git/commitdiff
switched to Proc::Daemon for startig background processes, introduced a log file
authorMichael Wiebusch <m.wiebusch@gsi.de>
Mon, 23 Feb 2015 16:49:07 +0000 (17:49 +0100)
committerMichael Wiebusch <m.wiebusch@gsi.de>
Mon, 23 Feb 2015 16:49:07 +0000 (17:49 +0100)
user_interface/coral_scanner.js
user_interface/coral_scanner.pm
user_interface/misc_subs.pm
user_interface/shm_manager.pm
user_interface/table_control.pm

index 9d37ed66a5a9fcb24dd94e5724f956228ee0ab5e..5c4e239560a8fcdec7f49bb3d77a8b6f4e3ba5a1 100644 (file)
@@ -117,6 +117,10 @@ $(document).ready(function(){
     signal_thresh();
   });
   
+  $("#button_clearlog").click(function(){
+    clear_log();
+  });
+  
   $('#checkbox_log_spectrum').change(function(){
 //     alert($(this).prop('checked'));
     plot_spectrum();
@@ -531,6 +535,21 @@ function clear_spectrum(){
      });
 }
 
+function clear_log(){
+  $.ajax({
+        url:       "coral_scanner.pl",
+        cache:     false,
+        async:     false,
+        dataType:  "text",
+        data:      {
+            sub      : "clear_log",
+        },
+        success:   function(answer) {
+          alert(answer);
+        }
+     });
+}
+
 function record_spectrum(){
   $.ajax({
         url:       "coral_scanner.pl",
index a4478be9392c15ef30b8a88515092787652fe45e..0cb88fb753ddc4ec931e6abaa5c127bb8c050688 100644 (file)
@@ -8,7 +8,6 @@ use POSIX qw/strftime/;
 use POSIX;
 use Device::SerialPort;
 use Data::Dumper;
-use Proc::Daemon;
 
 use SVG;
 
@@ -45,16 +44,19 @@ sub new {
     approx_upper_rate => 4000,
     plot_lower_limit  => 0,
     plot_upper_limit  => 4000,
-    pidfile           => "./coral_scanner.pid",
+    pid_file           => "./".__PACKAGE__.".pid",
+    log_file           => "./".__PACKAGE__.".log",
   };
   
   $self->{settings_desc} = {
     time_per_pixel => "time in seconds to integrate the counts of the PMT at a given coordinate",
     approx_upper_rate => "upper boundary of the counting rate in counts/sec,
     is used for setting the value range of the plot",
-    plot_lower_limit  => "lower contrast setting for the plot",
-    plot_upper_limit  => "upper contrast setting for the plot",
-    pidfile           => "/path/to/file of the lockfile for the scanning background process",
+    plot_lower_limit => "lower contrast setting for the plot",
+    plot_upper_limit => "upper contrast setting for the plot",
+    pid_file         => "/path/to/file of the lockfile for the scanning background process",
+    stdout           => "/path/to/file of the stdout logfile",
+    stderr           => "/path/to/file of the stderr logfile",
   };
 
   $self->{has_run} = {}; # remember which subs already have run
@@ -131,6 +133,9 @@ sub main_html {
   print "<input type='button' value='(re)plot' id='button_replot'>";
   print "<a href='coral_scanner.pl?sub=scan_to_ascii' target='_blank' id='button_ascii'
   ><input type='button' value='scan to ascii'></a>";
+  print "<a href='coral_scanner.log' target='_blank' id='button_log'
+  ><input type='button' value='view log'></a>";
+  print "<input type='button' value='clear log' id='button_clearlog'>";
   print br;
   print br;
   print "estimated scan duration: ".hms_string($self->scan_ETA());
@@ -226,6 +231,8 @@ sub scan_sample {
   my $scan_pattern = $tc->scan_pattern();
   my $ETA = $self->scan_ETA();
   
+  print ">>> starting scan\n\n";
+  
   $self->{status_shm}->updateShm({
     action => 'scanning',
     abort  => 0,
@@ -254,18 +261,25 @@ sub scan_sample {
   my $points_scanned = 0;
   
   for my $point (@{$scan_pattern->{points}}) {
-    $tc->go_xy( x => $point->{x}, y => $point->{y});
+    # attempt to drive to position
+    printf("Drive to point x,y = %3.3f,%3.3f i,j = %d,%d\n" ,$point->{x_rel},$point->{y_rel}, $point->{row},$point->{col});
+
+    
+    eval {
+      $tc->go_xy( x => $point->{x}, y => $point->{y});
+    };
+    warn "error from table\n$@" if $@;
     
-    printf("Acquire PMT counts at point x,y = %3.3f,%3.3f i,j = %d,%d\n" ,$point->{x_rel},$point->{y_rel}, $point->{row},$point->{col});
     
     my $delay = $self->{settings}->{time_per_pixel};
+    printf("Acquiring PMT counts for %3.2f seconds\n",$delay);
     my $counts = $ro->count(delay => $delay, channel => "signal");
     my $col = $point->{col};
     my $row = $point->{row};
     $points_scanned += 1;
     
     $self->{current_scan}->{data}->[$row]->[$col] = $counts;
-    print "counts: $counts\n";
+    print "recorded counts: $counts\n";
     print "\n\n";
     
     my $status = $self->{status_shm}->lockAndReadShm();
@@ -282,7 +296,7 @@ sub scan_sample {
       };
       $self->{status_shm}->writeShm($status);
       $self->{scan_shm}->writeShm($self->{current_scan});
-      print "scan was aborted!\n";
+      print ">>> scan was aborted!\n\n";
 #       last; # stop the acquisition loop!
       exit;
     } else {
@@ -305,32 +319,9 @@ sub scan_sample {
     seconds_left => 0
   });
   
-  $self->save_scan_ascii(filename => "./scan.dat");
-  
+  print ">>> scan completed!\n\n";
   
-
-}
-
-
-sub save_scan_ascii {
-  my $self = shift;
-  my %options = @_;
-  
-  my $filename = $options{filename};
-  
-  my @darray = @{$self->{current_scan}->{data}};
-#   @darray = sort {$a->{col} <=> $b->{col}} @darray;
-#   @darray = sort {$a->{row} <=> $b->{row}} @darray;
-  
-  open(FILE,">$filename");
-  for my $item (@darray){
-  
-    my $string = join("\t",@$item)."\n";
-#     my $string = sprintf("%d\t%d\t%d\n",$item->{row},$item->{col},$item->{counts});
-    print $string;
-    print FILE $string;
-  }
-  close(FILE);
+  return "";
 
 }
 
@@ -394,12 +385,26 @@ sub scan_status {
 sub last_scan {
   my $self = shift;
   return $self->{scan_shm}->readShm();
+}
 
+sub clear_log {
+  my $self = shift;
+  eval {
+    unlink $self->{settings}->{log_file} or die "could not delete log file";
+  };
+  if ($@) {
+    print $@;
+    return "";
+  }
+  print "log file deleted\n";
+  return "";
 }
 
 sub start_scan {
   my $self= shift;
-  daemonize();
+  
+  #start daemon
+  $self->daemon_start() or die "service could not be started (already running?)\n";
   $self->scan_sample();
 }
 
@@ -408,17 +413,22 @@ sub record_spectrum {
   my %options = @_;
   my $name = $options{name} || "signal";
   
-  daemonize();
+  #start daemon
+  die "service could not be started (already running?)\n" unless ($self->daemon_start());
   $self->{pmt_ro}->spectral_scan_onesided(
     name => $name
   );
   
-  return " ";
+  return "";
 }
 
 sub home {
   my $self= shift;
-  daemonize();
+  #start daemon
+  die "service could not be started (already running?)\n" unless ($self->daemon_start());
+  
+  print "homing the table\n";
+  
   $self->{status_shm}->updateShm({
     action => "homing"
   });
@@ -428,6 +438,9 @@ sub home {
   $self->{status_shm}->updateShm({
     action => "idle"
   });
+  print "homing completed\n";
+  
+  return "";
 }
 
 sub stop_scan {
@@ -561,5 +574,4 @@ sub scan_to_svg {
 
 
 
-
 1;
index a9eb0fb29c1e0ceefd6e649ffa9793d0062a0b68..ac00ac6047c37cc1c6afe2234541c9d0cc950c69 100644 (file)
@@ -1,14 +1,34 @@
 package misc_subs;
 use POSIX;
+use Proc::Daemon;
+
+use IO::Handle;
 
 BEGIN {
   require Exporter;
   # set the version for version checking
-  our $VERSION = 1.00;
+  our $VERSION = 1.01;
+  
+  # revision history
+  # v1.01
+  #     removed daemonize
+  #     added daemon_start/stop/status based on Proc::Daemon
+  
   # Inherit from Exporter to export functions and variables
   our @ISA = qw(Exporter);
   # Functions and variables which are exported by default
-  our @EXPORT = qw(printHeader min max echo require_run test hms_string daemonize false_color);
+  our @EXPORT = qw(
+    printHeader
+    min
+    max
+    echo
+    require_run
+    test hms_string
+    false_color
+    daemon_start
+    daemon_stop
+    daemon_status
+  );
   # Functions and variables which can be optionally exported
   #our @EXPORT_OK = qw($Var1 %Hashit func3);
 }
@@ -76,21 +96,7 @@ sub hms_string {
   return $string;
 }
 
-sub daemonize {
-  # chdir '/' or die "Can't chdir to /: $!";
 
-  defined(my $pid = fork) or die "Can't fork: $!";
-  if($pid){
-#     printHeader('text/plain') if $isHttpReq;
-    print "this instance has terminated, the other one is a demon now\n";
-    exit;
-  }
-  open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
-  open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
-  open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
-  POSIX::setsid or die "Can't start a new session: $!";
-  umask 0;
-}
 
 sub false_color {
   my $val = shift;
@@ -103,4 +109,71 @@ sub false_color {
 }
 
 
+
+sub daemon_start{
+
+  my $self = shift;
+  my $pf = $self->{settings}->{pid_file};
+  my $log = $self->{settings}->{log_file};
+  my $daemon = Proc::Daemon->new(
+      pid_file => $pf,
+      work_dir => "./",
+  );
+  
+  my $pid = $daemon->Status($pf);
+  
+  if ($pid) {
+      print "Background service already running with pid $pid.\n";
+      return;
+  } else {
+      print "Not running. Starting background service\n";
+      $daemon->Init;
+      open(LOG,"+>>$log");
+      *STDERR = *LOG;
+      *STDOUT = *LOG;
+      LOG->autoflush;
+      return 1;
+  }
+}
+
+sub daemon_stop {
+  my $self = shift;
+  my $pf = $self->{settings}->{pid_file};
+  my $daemon = Proc::Daemon->new(
+      pid_file => $pf,
+      work_dir => "./",
+  );
+  my $pid = $daemon->Status($pf);
+
+  if ($pid) {
+      print "Stopping pid $pid...\n";
+      if ($daemon->Kill_Daemon($pf)) {
+          print "Successfully stopped.\n";
+      } else {
+          print "Could not find $pid.  Was it running?\n";
+      }
+    } else {
+          print "Not running, nothing to stop.\n";
+    }
+}
+
+sub daemon_status {
+  my $self = shift;
+  my $pf = $self->{settings}->{pid_file};
+  my $daemon = Proc::Daemon->new(
+      pid_file => $pf,
+      work_dir => "./",
+  );
+  my $pid = $daemon->Status($pf);
+
+    if ($pid) {
+        print "Running with pid $pid.\n";
+    } else {
+        print "Not running.\n";
+    }
+  return $pid;
+}
+
+
+
 1;
\ No newline at end of file
index 289475c434954679f15836126b0b7a6500d2f922..91f709fa3f9169f0d35240a17c01dd76151195e9 100644 (file)
@@ -108,7 +108,7 @@ sub writeShm { # closes and unlocks shm file if already open
     my $fh=$self->{shmFh};
     #check if file handle still open and locked
     unless($self->{shmFhLocked}){
-      print "found locked shm from previous lock-and-read\n";
+      print "found locked shm from previous lock-and-read\n";
       sysopen($fh, $self->{shmFile}, O_RDWR|O_CREAT, 0666) 
           or die "can't open shm file: $!";
       flock($fh, LOCK_EX)           or die "can't lock shm file: $!";
index a0bf1dadb912adfd98250faac78a204fa57e1201..ddb264de7ed5b7dd2e7577a4864dfbc6fa2fc7aa 100644 (file)
@@ -305,7 +305,7 @@ sub go_xy {
 
 sub go_startpoint {
   my $self = shift;
-  
+  print "attempting to go to the scan startpoint\n";
   $self->go_xy(
     x => $self->{settings}->{sample_rect_x1},
     y => $self->{settings}->{sample_rect_y1}