Perl Multi-threading, real-time monitoring of threads, support for Max thread

Source: Internet
Author: User
Tags sprintf

#!/usr/bin/perl -w#description:rerun eod job group by system#auther:suzm#date   :2015-06-23use dbi;use thread;use strict;push (  @INC,  $ENV {TPMS_EOD_PERL_ lib} );require public_pg;my %dbc_info;my  $maxnum  = 3;my %threads;my $ tx_date;my  $path _log =  $ENV {tpms_eod_logpath};my  $system  = get_config (' TPMS_ system_id ');unless  ( defined (  $system  )  )  {$system  =  "error";} #my   $system  = undef;my  $log _name =  uc ( citic::getscript_name)  .  "_" &NBSP;. $system);my  $log _file     = citic::create_logfile (   $log _name,  $path _log );my  $TPMS _eod_sid =  $ENV {tpms_eod_sid};my  $my _sysenv     = uc (  $ENV {my_sysenv} );select  $log _file;$| = 1; #get  eod job info ,return hashsub geteodjob {my  (  $sysid,  $dbh  )  = @_;my %jobinfo; #my   $STR  =  "Select * from tpms_eod. t_tpms_eodtable where system= ' ${sysid} ' ";my  $str  = <<EOF;select a.*  From tpms_eod.t_tpms_eodtable ainner join tpms_eod. tpms_rollback_bizdate bon a.tx_date != b.curr_biz_dateor  (a.TX_DATE = b. curr_biz_date and a.flag=1) where a.system= ' ${sysid} ' eofmy  $sth; eval {$sth  =   $DBH->prepare ($STR); $sth->execute ();}; if  ([email protected])  {citic::showtime ();p rint  "an error occurred  ([email  protected]) \ n "; Citic::showtime ();p rint  $dbh->errstr .  "\ n"; $dbh->disconnect (); return %jobinfo;} while  ( my  @row  =  $sth->fetchrow_array ()  )  {$jobinfo {  $row [1]  } {"SYSTEM"}     =  $row [0]; $jobinfo {  $row [1] }{"Job_script"} =  $row [2]; $jobinfo {  $row [1]  }{"ARGV"}       =  $row [3]; $jobinfo {  $row [1] }{] START_ Time "} =  $row [4]; $jobinfo {  $row [1] }{" End_time "}   =  $row [5]; $jobinfo {   $row [1] }{"Tx_date"}    =  $row [6]; $jobinfo {  $row [1] }{"FLAG"}        =  $row [7];} Return %jobinfo;} #获取配置参数sub  get_config{my ($name)  = @_;my  $str; open (config, "$ENV {tpms_eod_etc}/etc.profile") while (<CONFIG>) {My ($key, $value) =split (' = ', $_), if (UC ($key)  eq uc ($name)) {$str  =  Citic::strimstr ($value);chomp  $str;} Else{next;}} Close (CONFIG); Return uc ($STR);} #cmdsub  cmd {my  $ret;my  (  $script,  $argv,  $tablename,  $dbh  )  = @_;my  $cmd  =  " perl ${script}  $argv";my  $RC  = open ( INFO,  "$cmd  2>&1|"  );unless  ($RC)  {citic::showtime ();p rint  "Can ' t invoke the command!\n"; return 1;} while  (<INFO>)  {print $_; #print  STDOUT $_;if  (EOF)  {unless  (/\b ( complete| succeeds)/)  {citic::showtime ();p rint  "*"  x 6 .  "$tablename Job execution Failed"  .  "* " x 6 . " \ n "; $ret  = 1;} Else {citic::showtime ();p rint  "*"  x 6 .  "$tablename Job execution Success"  .  "*"   x 6 .  "\ n"; $ret  = 0;}}} Close (INFO);return  $ret;} #update  job infosub upJobInfo {my  (  $dbh,  $tablename,  $flag,  $ final )  = @_;my  $upstr;my  $timestamp  = showtime ();if  (  $flag  == 1 )  {$upstr  = "Start_time=to_timestamp (' $timestamp ', ' yyyy-mm-dd hh24:mi:ss:ff '), Tx_date=to_daTe (' $tx _date ', ' yyyy-mm-dd ') ";} else {$upstr  = "End_time=to_timestamp (' $timestamp ', ' yyyy-mm-dd hh24:mi:ss:ff '), flag= $final";} my  $str  = "Update tpms_eod. t_tpms_eodtable set  $upstr  where table_name= ' $tablename '  and system= ' $system ' "; my  $sth, eval {$sth  =  $dbh->prepare ($str); $sth->execute ();}; if  ([email protected])  {citic::showtime ();p rint  "an error occurred  ([email  protected]) \ n "; Citic::showtime ();p rint  $dbh->errstr .  "\ n"; $dbh->disconnect (); return 1;} return 0;} #get  tx_datesub getTxdate {my  ($DBH)  = @_;my  $txdate;my  $str  =    "Select to_char (curr_biz_date, ' yyyy-mm-dd ')  from tpms_eod. Tpms_rollback_bizdate ";my  $sth, eval {$sth  =  $dbh->prepare ($str); $sth->execute ();}; if  ([email protected])  {citic::showtime ();p rint  "AN&NBSP;ERROR&Nbsp;occurred  ([email protected]) \ n "; Citic::showtime ();p rint  $dbh->errstr .  "\ n"; $dbh->disconnect (); return 1;} my  $table  =  $sth->fetchall_arrayref () $txdate  =  $table->[0][0];# $dbh Disconnect ();return  $txdate;} #get  timestampsub showtime {my  (  $sec,  $min,  $hour,  $day,  $mon,   $year  )  = localtime ( time ()  );my  $current  =  ""; $sec   =  sprintf (  "%02d",  $sec  ), $min   = sprintf (  "%02d",  $min  ); hour = sprintf (  "%02d",  $hour  ), $day   = sprintf (  "%02d",  $ day ); $mon   = sprintf (  "%02d",  $mon  + 1 ); $year  += 1900 , $current  =    $year-$mon-$day  .    $hour  . : $min " . ": $sec "; return ${current};} #execute  eod jobsub executejob {my  (  $DBH, %hash )  = @_;my  $i;my  $num  =  Scalar ( keys %hash );for my  $jobname   ( keys %hash )  {citic:: Showtime ();p rint  "*"  x 6 .  "executing $jobname job"  .  "*" &NBSP;X&NBSP;6&NBSP;.   "\ n", Upjobinfo (  $DBH,  $jobname, 1 ), $i + +, $num--;eval {$threads {$jobname} =  thread->new (\&cmd, $hash {$jobname} {' Job_script '}, $hash {$jobname} {' ARGV '},  $jobname);#-> Join ();}; if  ([email protected])  {citic::showtime ();p rint  "an error occurred  ([email  protected] \ n "; return 1;} if  (  $i  >  $maxnum  - 1 )  {print  "process reached maximum"  . thread- >list ()  .  "!! \ n ";while  (1)  {for my  $th   ( thread->list ()  )  {my  $tid;if  (  $th->done )  {eval {  $tid &NBSP;=&NBSP; $th->join ()  };foreach my  $TB   ( keys %threads )  {if  ( $ threads{$TB}->tid ()  ==  $th->tid ()  )  {if  ($tid)  {upjobinfo (  $DBH,   $TB, 2, 1 );} Else {upjobinfo (  $DBH,  $tb, 2, 0 );}} $i--;} Else {next;}} if  ( thread->list ()  <  $maxnum  )  {print  "process less than 3, start a new process \ n"; last;} else {print  "sleep 10s,waiting for the job finish\n"; sleep 10;}} if  (  $num  == 0 )  {print  "#"  x 20  .  "All job schedules complete, Wait for the process to end!!! "  . " # " x 20 . " \ n ";for my  $th 1  ( thread->list ()  )  {my  $tid 1;eval {  $tid 1 =  $th 1->join ()  };foreach my $ tb1  ( keys %threads )  {if  (  $threads {$TB 1}->tid ()  ==  $th 1-> Tid ()  )  {if  ($tid 1)  {upjobinfo (  $DBH,  $tb 1, 2, 1 );} Else {upjobinfo (  $DBH,  $tb 1, 2, 0 );}}}}} #程序入口sub  main {#my   ($system) [email protected]_;my  $ret  = 0;my  $dbh; Dbc_info = citic::get_dbc_info ($TPMS _eod_sid);unless  (%dbc_info)  {citic::showtime ();p rint   "failed to get database information!\n";} else {$dbh  = citic::connect_db ($dbc _info{"IP"},    $dbc _info{"Port"},  $dbc _ info{"Sid"}, $DBC _info{"user"},  $dbc _info{"pwd"}); unless  ($DBH)  {$ret  = 1;} Else {my %jobinfo = geteodjob (  $system,  $dbh  );if  (%jobinfo)  {if   ( gettxdate ($DBH)  eq 1 )  {$ret  = 1;} else {$tx _date = gettxdate ($DBH); Executejob (  $DBH, %jobinfo );}} Else {citic::showtime ();p rint  "No job to execute, please confirm and then execute!" \ n "; $ret  = 1;} # $ret  = excute_sql (  $DBH,  @sql_queue  );} return  $ret;} Open ( STDERR,  ">&stdout"  ), #if   ( $ #ARGV  < 0 )  {#print   "please input parameters,for example:\n1.system id :cts\n"; #exit (1); #}# $system  = uc (  $ARGV [0] );my  $ret  = main ();if  (  $ret  == 0  )  {print STDOUT  "complete\n";} else {print stdout  "fail\n"; #CITIC:: Send_mail (# "[$my _sysenv]  $log _name job  FAILED ", #" hi all,\n      job  $log _name failed. please  See attached log file for details.\nthanks! " #);} Exit ($ret); End {citic::showtime ();p rint  "return code is  $ret \ n"; Citic::close_logfile ($log _file);}


Perl Multi-threading, real-time monitoring of threads, support for Max thread

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.