mtr_im.pl

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PL 代码 · 共 776 行 · 第 1/2 页

PL
776
字号
###########################################################################sub mtr_im_rm_file($){  my $file_path= shift;  if ( -f $file_path )  {    mtr_debug("Removing '$file_path'...");    unless ( unlink($file_path) )    {      mtr_warning("Can not remove '$file_path'.")    }  }  else  {    mtr_debug("File '$file_path' does not exist already.");  }}###########################################################################sub mtr_im_errlog($) {  my $msg= shift;  # Complain in error log so that a warning will be shown.  #   # TODO: unless BUG#20761 is fixed, we will print the warning to stdout, so  # that it can be seen on console and does not produce pushbuild error.  # my $errlog= "$opt_vardir/log/mysql-test-run.pl.err";  #   # open (ERRLOG, ">>$errlog") ||  #   mtr_error("Can not open error log ($errlog)");  #   # my $ts= localtime();  # print ERRLOG  #   "Warning: [$ts] $msg\n";  #   # close ERRLOG;  my $ts= localtime();  print "Warning: [$ts] $msg\n";}###########################################################################sub mtr_im_kill($) {  my $im= shift;  # Re-load PIDs. That can be useful because some processes could have been  # restarted.  mtr_im_load_pids($im);  # Ignoring SIGCHLD so that all children could rest in peace.  start_reap_all();  # Kill IM-angel first of all.  if ( defined $im->{'angel_pid'} )  {    mtr_debug("Killing IM-angel (PID: $im->{angel_pid})...");    mtr_im_kill_process([ $im->{'angel_pid'} ], 'KILL', 10, 1)  }  else  {    mtr_debug("IM-angel is dead.");  }  # Re-load PIDs again.  mtr_im_load_pids($im);  # Kill IM-main.    if ( defined $im->{'pid'} )  {    mtr_debug("Killing IM-main (PID: $im->pid})...");    mtr_im_kill_process([ $im->{'pid'} ], 'KILL', 10, 1);  }  else  {    mtr_debug("IM-main is dead.");  }  # Re-load PIDs again.  mtr_im_load_pids($im);  # Kill guarded mysqld instances.  my @mysqld_pids;  mtr_debug("Collecting PIDs of mysqld instances to kill...");  for ( my $idx= 0; $idx < 2; ++$idx )  {    my $pid= $im->{'instances'}->[$idx]->{'pid'};    unless ( defined $pid )    {      next;    }    mtr_debug("  - IM-guarded mysqld[$idx] PID: $pid.");    push (@mysqld_pids, $pid);  }  if ( scalar @mysqld_pids > 0 )  {    mtr_debug("Killing IM-guarded mysqld instances...");    mtr_im_kill_process(\@mysqld_pids, 'KILL', 10, 1);  }  # That's all.  stop_reap_all();}##############################################################################sub mtr_im_wait_for_connection($$$) {  my $im= shift;  my $total_attempts= shift;  my $connect_timeout= shift;  mtr_debug("Waiting for IM on port $im->{port} " .            "to start accepting connections...");  for ( my $cur_attempt= 1; $cur_attempt <= $total_attempts; ++$cur_attempt )  {    mtr_debug("Trying to connect to IM ($cur_attempt of $total_attempts)...");    if ( mtr_ping_port($im->{'port'}) )    {      mtr_debug("IM is accepting connections " .                "on port $im->{port}.");      return 1;    }    mtr_debug("Sleeping $connect_timeout...");    sleep($connect_timeout);  }  mtr_debug("IM does not accept connections " .            "on port $im->{port} after " .            ($total_attempts * $connect_timeout) . " seconds.");  return 0;}##############################################################################sub mtr_im_wait_for_mysqld($$$) {  my $mysqld= shift;  my $total_attempts= shift;  my $connect_timeout= shift;  mtr_debug("Waiting for IM-guarded mysqld on port $mysqld->{port} " .            "to start accepting connections...");  for ( my $cur_attempt= 1; $cur_attempt <= $total_attempts; ++$cur_attempt )  {    mtr_debug("Trying to connect to mysqld " .              "($cur_attempt of $total_attempts)...");    if ( mtr_ping_port($mysqld->{'port'}) )    {      mtr_debug("Mysqld is accepting connections " .                "on port $mysqld->{port}.");      return 1;    }    mtr_debug("Sleeping $connect_timeout...");    sleep($connect_timeout);  }  mtr_debug("Mysqld does not accept connections " .            "on port $mysqld->{port} after " .            ($total_attempts * $connect_timeout) . " seconds.");  return 0;}################################################################################  Public operations.###############################################################################sub mtr_im_start($$) {  my $im = shift;  my $opts = shift;  mtr_debug("Starting Instance Manager...");  my $args;  mtr_init_args(\$args);  mtr_add_arg($args, "--defaults-file=%s", $im->{'defaults_file'});  foreach my $opt ( @{$opts} )  {    mtr_add_arg($args, $opt);  }  $im->{'spawner_pid'} =    mtr_spawn(      $::exe_im,                        # path to the executable      $args,                            # cmd-line args      '',                               # stdin      $im->{'path_log'},                # stdout      $im->{'path_err'},                # stderr      '',                               # pid file path (not used)      { append_log_file => 1 }          # append log files      );  unless ( $im->{'spawner_pid'} )  {    mtr_error('Could not start Instance Manager.')  }  # Instance Manager can be run in daemon mode. In this case, it creates  # several processes and the parent process, created by mtr_spawn(), exits just  # after start. So, we have to obtain Instance Manager PID from the PID file.  mtr_debug("Waiting for IM to create PID file (" .            "path: '$im->{path_pid}'; " .            "timeout: $im->{start_timeout})...");  unless ( sleep_until_file_created($im->{'path_pid'},                                    $im->{'start_timeout'},                                    -1) ) # real PID is still unknown  {    mtr_debug("IM has not created PID file in $im->{start_timeout} secs.");    mtr_debug("Aborting test suite...");    mtr_kill_leftovers();    mtr_report("IM has not created PID file in $im->{start_timeout} secs.");    return 0;  }  $im->{'pid'}= mtr_get_pid_from_file($im->{'path_pid'});  mtr_debug("Instance Manager started. PID: $im->{pid}.");  # Wait until we can connect to IM.  my $IM_CONNECT_TIMEOUT= 30;  unless ( mtr_im_wait_for_connection($im,                                      $IM_CONNECT_TIMEOUT, 1) )  {    mtr_debug("Can not connect to Instance Manager " .              "in $IM_CONNECT_TIMEOUT seconds after start.");    mtr_debug("Aborting test suite...");    mtr_kill_leftovers();    mtr_report("Can not connect to Instance Manager " .               "in $IM_CONNECT_TIMEOUT seconds after start.");    return 0;  }  # Wait for IM to start guarded instances:  #   - wait for PID files;  mtr_debug("Waiting for guarded mysqlds instances to create PID files...");  for ( my $idx= 0; $idx < 2; ++$idx )  {    my $mysqld= $im->{'instances'}->[$idx];    if ( exists $mysqld->{'nonguarded'} )    {      next;    }    mtr_debug("Waiting for mysqld[$idx] to create PID file (" .              "path: '$mysqld->{path_pid}'; " .              "timeout: $mysqld->{start_timeout})...");    unless ( sleep_until_file_created($mysqld->{'path_pid'},                                      $mysqld->{'start_timeout'},                                      -1) ) # real PID is still unknown    {      mtr_debug("mysqld[$idx] has not created PID file in " .                 "$mysqld->{start_timeout} secs.");      mtr_debug("Aborting test suite...");      mtr_kill_leftovers();      mtr_report("mysqld[$idx] has not created PID file in " .                 "$mysqld->{start_timeout} secs.");      return 0;    }    mtr_debug("PID file for mysqld[$idx] ($mysqld->{path_pid} created.");  }  # Wait until we can connect to guarded mysqld-instances  # (in other words -- wait for IM to start guarded instances).  mtr_debug("Waiting for guarded mysqlds to start accepting connections...");  for ( my $idx= 0; $idx < 2; ++$idx )  {    my $mysqld= $im->{'instances'}->[$idx];    if ( exists $mysqld->{'nonguarded'} )    {      next;    }    mtr_debug("Waiting for mysqld[$idx] to accept connection...");    unless ( mtr_im_wait_for_mysqld($mysqld, 30, 1) )    {      mtr_debug("Can not connect to mysqld[$idx] " .                "in $IM_CONNECT_TIMEOUT seconds after start.");      mtr_debug("Aborting test suite...");      mtr_kill_leftovers();      mtr_report("Can not connect to mysqld[$idx] " .                 "in $IM_CONNECT_TIMEOUT seconds after start.");      return 0;    }    mtr_debug("mysqld[$idx] started.");  }  mtr_debug("Instance Manager and its components are up and running.");  return 1;}##############################################################################sub mtr_im_stop($) {  my $im= shift;  mtr_debug("Stopping Instance Manager...");  # Try graceful shutdown.  mtr_im_terminate($im);  # Check that all processes died.  unless ( mtr_im_check_alive($im) )  {    mtr_debug("Instance Manager has been stopped successfully.");    mtr_im_cleanup($im);    return 1;  }  # Instance Manager don't want to die. We should kill it.  mtr_im_errlog("Instance Manager did not shutdown gracefully.");  mtr_im_kill($im);  # Check again that all IM-related processes have been killed.  my $im_is_alive= mtr_im_check_alive($im);  mtr_im_cleanup($im);  if ( $im_is_alive )  {    mtr_debug("Can not kill Instance Manager or its children.");    return 0;  }  mtr_debug("Instance Manager has been killed successfully.");  return 1;}###########################################################################1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?