From 42e4b0a5d3c5b512261967e3b351bee1bfe7b5a4 Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Mon, 23 Feb 2015 17:49:07 +0100 Subject: [PATCH] switched to Proc::Daemon for startig background processes, introduced a log file --- user_interface/coral_scanner.js | 19 ++++++ user_interface/coral_scanner.pm | 90 +++++++++++++++------------ user_interface/misc_subs.pm | 105 +++++++++++++++++++++++++++----- user_interface/shm_manager.pm | 2 +- user_interface/table_control.pm | 2 +- 5 files changed, 161 insertions(+), 57 deletions(-) diff --git a/user_interface/coral_scanner.js b/user_interface/coral_scanner.js index 9d37ed6..5c4e239 100644 --- a/user_interface/coral_scanner.js +++ b/user_interface/coral_scanner.js @@ -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", diff --git a/user_interface/coral_scanner.pm b/user_interface/coral_scanner.pm index a4478be..0cb88fb 100644 --- a/user_interface/coral_scanner.pm +++ b/user_interface/coral_scanner.pm @@ -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 ""; print ""; + print ""; + print ""; 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; diff --git a/user_interface/misc_subs.pm b/user_interface/misc_subs.pm index a9eb0fb..ac00ac6 100644 --- a/user_interface/misc_subs.pm +++ b/user_interface/misc_subs.pm @@ -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 diff --git a/user_interface/shm_manager.pm b/user_interface/shm_manager.pm index 289475c..91f709f 100644 --- a/user_interface/shm_manager.pm +++ b/user_interface/shm_manager.pm @@ -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: $!"; diff --git a/user_interface/table_control.pm b/user_interface/table_control.pm index a0bf1da..ddb264d 100644 --- a/user_interface/table_control.pm +++ b/user_interface/table_control.pm @@ -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} -- 2.43.0