my @hosts = keys %nes; my @hosts_ok; my $max_threads = $config->val($section,'MaxThreads'); for my $threadno (0 .. ($max_threads-1)){ if (@hosts){ my $host = shift (@hosts); $thread_ip[$threadno] = $host; $thread_result[$threadno] = 0; $thread_time[$threadno] = time(); push @threads, threads->create('get_ne_data',$threadno); $threads[@threads-1]->detach(); write_log ('MAIN','STARTING THREADS',1,'001b','Starting thread',$threads[@threads-1]->tid(),'NE',$host); sleep(1); } } while (@hosts){ for my $threadno (0 .. (@threads-1)){ if ($threads[$threadno] and $threads[$threadno]->is_running() ){ if ($thread_result[$threadno]){ # thread as returned if ($thread_result[$threadno] == 2){ write_log ('MAIN','PROCESSING NES',2,'001c','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Finished OK'); push @hosts_ok, $thread_ip[$threadno]; } else { write_log ('MAIN','PROCESSING NES',2,'001d','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Finished NOK'); } $thread_time[$threadno] = time(); $thread_result[$threadno] = 0; if (@hosts){ $thread_ip[$threadno] = shift @hosts; } else { # print "\tFINISHED\n"; $thread_ip[$threadno] = 'FINISHED'; } write_log ('MAIN','PROCESSING NES',2,'001e','Thread',$threads[$threadno]->tid(),'is being assigned',$thread_ip[$threadno]); } elsif ($thread_time[$threadno] < (time-60) ){ write_log ('MAIN','PROCESSING NES',2,'001f','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Being killed'); # print "\t$thread_ip[$threadno] is being killed\n"; $threads[$threadno]->kill('STOP'); $thread_time[$threadno] = time(); $thread_result[$threadno] = 0; if (@hosts){ $thread_ip[$threadno] = shift @hosts; } else { # print "\tFINISHED\n"; $thread_ip[$threadno] = 'FINISHED'; } write_log ('MAIN','PROCESSING NES',2,'0020','Thread',$threads[$threadno]->tid(),'is being assigned',$thread_ip[$threadno]); } } else { # for some reason we don't have a thread here - possibly stopped due to long run time # print "\tTrying to restart thread\n"; if (@hosts){ my $host = shift (@hosts); $thread_ip[$threadno] = $host; $thread_result[$threadno] = 0; $thread_time[$threadno] = time(); $threads[$threadno] = threads->create('get_ne_data',$threadno); $threads[$threadno]->detach(); write_log ('MAIN','PROCESSING NES',4,'0021','THREAD NEEDS RESTARTING',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno]); } } } sleep(1); } write_log ('MAIN','',2,'0022','All hosts started'); my $threads_running = 1; while ($threads_running){ $threads_running = 0; for my $threadno (0 .. (@threads-1)){ if ($threads[$threadno] and $threads[$threadno]->is_running() ){ $threads_running++; if ($thread_result[$threadno]){ # thread as returned if ($thread_result[$threadno] == 2){ write_log ('MAIN','CLEARUP',2,'0022','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Finished OK'); push @hosts_ok, $thread_ip[$threadno]; } else { write_log ('MAIN','CLEARUP',2,'0023','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Finished NOK'); } $thread_ip[$threadno] = 'FINISHED'; $thread_result[$threadno] = 0; } elsif ($thread_time[$threadno] < (time-60) ){ write_log ('MAIN','CLEARUP',2,'0024','Thread',$threads[$threadno]->tid(),'NE',$thread_ip[$threadno],'Being killed'); $threads[$threadno]->kill('STOP'); $thread_time[$threadno] = time(); $thread_result[$threadno] = 0; $thread_ip[$threadno] = 'FINISHED'; } } } }