22
33namespace ProcessMaker \Jobs ;
44
5+ use Illuminate \Support \Arr ;
56use Illuminate \Support \Facades \Log ;
67use Illuminate \Support \Facades \Notification ;
78use ProcessMaker \Models \Process ;
1213
1314class ErrorHandling
1415{
15- public $ errorHandling = [];
16-
17- public static $ callback ;
16+ /**
17+ * Error handling settings that are set in modeler.
18+ *
19+ * These settings take precedence over those set in script or
20+ * data source configuration.
21+ *
22+ * @var array
23+ */
24+ public $ bpmnErrorHandling = [];
25+
26+ /**
27+ * Default settings that are set in script or data source configuration.
28+ *
29+ * $bpmnErrorHandling takes precedence over these if set.
30+ *
31+ * @var array
32+ */
33+ public $ defaultErrorHandling = [];
1834
1935 public function __construct (
36+ public $ element ,
2037 public $ processRequestToken ,
21- )
22- {
23-
38+ ) {
39+ $ this ->bpmnErrorHandling = json_decode ($ element ->getProperty ('errorHandling ' ), true ) ?? [];
2440 }
2541
2642 public function handleRetries ($ job , $ exception )
@@ -35,11 +51,9 @@ public function handleRetries($job, $exception)
3551 return $ message ;
3652 }
3753
38- $ message = __ ('Job failed after :attempts total attempts ' , ['attempts ' => $ job ->attemptNum - 1 ]) . "\n" . $ message ;
54+ $ message = __ ('Job failed after :attempts total attempts ' , ['attempts ' => $ job ->attemptNum ]) . "\n" . $ message ;
3955
4056 $ this ->sendExecutionErrorNotification ($ message );
41-
42- return $ message ;
4357 } else {
4458 $ this ->sendExecutionErrorNotification ($ message );
4559 }
@@ -70,70 +84,114 @@ public function sendExecutionErrorNotification(string $message)
7084 $ user = $ this ->processRequestToken ->processRequest ->processVersion ->manager ;
7185 if ($ user !== null ) {
7286 Log::info ('Send Execution Error Notification: ' . $ message );
73- Notification::send ($ user , new ErrorExecutionNotification ($ this ->processRequestToken , $ message , $ this ->errorHandling ));
87+ Notification::send ($ user , new ErrorExecutionNotification ($ this ->processRequestToken , $ message , $ this ->bpmnErrorHandling ));
7488 }
7589 }
7690 }
7791
7892 /**
79- * This sets the property errorHandling, and if the parameter doesn't exist,
80- * it returns the current value of property errorHandling.
93+ * Get the effective timeout
8194 *
82- * @param mixed $value
83- * @return mixed
95+ * @return int
8496 */
85- public function errorHandling ( $ value = null )
97+ public function timeout ( )
8698 {
87- Log::info ('Setting errorHandling ' , ['value ' => $ value ]);
88- if (is_array ($ value ) && !empty ($ value )) {
89- $ array = [
90- 'timeout ' ,
91- 'retry_attempts ' ,
92- 'retry_wait_time ' ,
93- ];
94- foreach ($ array as $ val ) {
95- $ digit = 0 ;
96- if (is_string ($ value [$ val ]) && ctype_digit ($ value [$ val ])) {
97- $ digit = intval ($ value [$ val ]);
98- }
99- if (is_int ($ value [$ val ])) {
100- $ digit = $ value [$ val ];
101- }
102- $ value [$ val ] = $ digit ;
103- }
104- $ this ->errorHandling = $ value ;
105- }
106-
107- return $ this ->errorHandling ;
99+ return $ this ->get ('timeout ' );
108100 }
109101
110102 /**
111- * This retrieves the value of errorHandling['timeout'].
103+ * Get the effective retry attempts
112104 *
113105 * @return int
114106 */
115- public function timeout ()
107+ public function retryAttempts ()
116108 {
117- return $ this ->errorHandling [ ' timeout ' ] ;
109+ return $ this ->get ( ' retry_attempts ' ) ;
118110 }
119111
120112 /**
121- * This retrieves the value of errorHandling['retry_attempts'] .
113+ * Get the effective retry wait time .
122114 *
123115 * @return int
124116 */
125- public function retryAttempts ()
117+ public function retryWaitTime ()
126118 {
127- return $ this ->errorHandling [ ' retry_attempts ' ] ;
119+ return $ this ->get ( ' retry_wait_time ' ) ;
128120 }
129121
130122 /**
131- * This retrieves the value of errorHandling['retry_wait_time'].
123+ * Get the attribute from the bpmnErrorHandling array but if it's not set
124+ * return the defaultErrorHandling value
132125 *
133- * @return int
126+ * @param string $attribute
127+ * @return void
134128 */
135- public function retryWaitTime ()
129+ public function get ($ attribute )
130+ {
131+ $ value = Arr::get (
132+ $ this ->bpmnErrorHandling ,
133+ $ attribute ,
134+ null
135+ );
136+
137+ if ($ value === null || $ value === '' ) {
138+ $ value = Arr::get ($ this ->defaultErrorHandling , $ attribute , 0 );
139+ }
140+
141+ return (int ) $ value ;
142+ }
143+
144+ /**
145+ * Set defaults from script model
146+ *
147+ * @param ScriptVersion $script
148+ * @return void
149+ */
150+ public function setDefaultsFromScript (ScriptVersion $ script )
136151 {
137- return $ this ->errorHandling ['retry_wait_time ' ];
152+ $ this ->defaultErrorHandling = [
153+ 'timeout ' => $ script ->timeout ,
154+ 'retry_attempts ' => $ script ->retry_attempts ,
155+ 'retry_wait_time ' => $ script ->retry_wait_time ,
156+ ];
157+ }
158+
159+ /**
160+ * Set defaults from data source model endpoint
161+ *
162+ * @param array $config
163+ * @return void
164+ */
165+ public function setDefaultsFromDataSourceConfig (array $ config )
166+ {
167+ // If this is not a Data Connecter, don't do any error handling
168+ $ id = Arr::get ($ config , 'dataSource ' , null );
169+ if (!$ id ) {
170+ return ;
171+ }
172+
173+ // Check if the data source package is installed
174+ $ class = "ProcessMaker\Packages\Connectors\DataSources\Models\DataSource " ;
175+ if (!class_exists ($ class )) {
176+ return ;
177+ }
178+
179+ // Check if the data source exists
180+ $ dataSource = $ class ::find ($ id );
181+ if (!$ dataSource ) {
182+ return ;
183+ }
184+
185+ // Check if the endpoint config exists in the data source
186+ $ endpointConfig = Arr::get ($ dataSource ->endpoints , Arr::get ($ config , 'endpoint ' , null ));
187+ if (!$ endpointConfig ) {
188+ return ;
189+ }
190+
191+ $ this ->defaultErrorHandling = [
192+ 'timeout ' => Arr::get ($ endpointConfig , 'timeout ' , 0 ),
193+ 'retry_attempts ' => Arr::get ($ endpointConfig , 'retry_attempts ' , 0 ),
194+ 'retry_wait_time ' => Arr::get ($ endpointConfig , 'retry_wait_time ' , 5 ),
195+ ];
138196 }
139197}
0 commit comments