'stop application if pushed thread had exception in Perl

I have aplication which runs in parallel mode. Jobs are runing using threads with implmented subroutine. Subroutine "worker3" have three argumens, one parameter and two path for files. This subroutine execute R script using system commands.

if ($ENV{"model"} eq "alaef_cy40_5km"){

      sub worker3 {
      my $parameter2 = $_[0];
       print $parameter2, "\n";
      $ENV{"grb_path"} = $models{$model}{"grb"}{"path"}{$parameter2};
      $ENV{"grb_file"} = $models{$model}{"grb"}{"file"}{$parameter2};
      print"FROM sub:", "\n";
      print $ENV{"grb_path"}, "\n";
      print $ENV{"grb_file"}, "\n";
      say "Job  Started\n";
      system("Rscript $root/read_ALAEF.R @_ ");

 
}
      
      for my $param( 'T2m','RH2m'){
      push @threads, my $thr1=threads ->create('worker3', $param ,$ENV{"grb_file"},$ENV{"grb_path"})

      }

      $_->join() for threads->list();
     
   }

Problem is when R script finish with Execution halted, application final status is ok. What i need is when R script finished with error, the whole program needs to be stopped. So, if one thread fails, application needs to stops, and display error.

This is output:

T2m
FROM sub:
/data/nwp/products/a-laef_stream
A-LAEF_mem_{MBR2}_{YYYY}{MM}{DD}{HH}_surface.grb
Job  Started

RH2m
FROM sub:
/data/nwp/products/a-laef_stream
A-LAEF_mem_{MBR2}_{YYYY}{MM}{DD}{HH}_surface.grb
Job  Started

-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.6     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.1.0     v forcats 0.5.1
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.6     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.1.0     v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
here() starts at /work/users/p6095/2022-02-19_00/harp.119
Loading required package: harpIO

Attaching package: 'harpIO'

The following object is masked from 'package:purrr':

    accumulate

Loading required package: harpPoint
Loading required package: harpVis
Loading required package: shiny
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
here() starts at /work/users/p6095/2022-02-19_00/harp.119
Loading required package: harpIO

Attaching package: 'harpIO'

The following object is masked from 'package:purrr':

    accumulate

Loading required package: harpPoint
Loading required package: harpVis
Loading required package: shiny
Loading required package: harpSpatial
Error: unexpected string constant in:
"
'4'"
Execution halted
Loading required package: harpSpatial
Error: unexpected string constant in:
"
'4'"
Execution halted

----------------------------------------------------
Application finished OK at: 21-02-2022  15:46:11 UTC
----------------------------------------------------

As you see, it shows that application finished OK, no matter if is the error inside R code. I need this : if pushed thread had exception -> stop application perl



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source