/var/www/html_uk/wp-content/plugins/automatewoo/includes/Jobs/ActionSchedulerJobMonitor.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace AutomateWoo\Jobs;

use 
AutomateWoo\ActionScheduler\ActionSchedulerInterface;

defined'ABSPATH' ) || exit;

/**
 * Class ActionSchedulerJobMonitor
 *
 * @since 5.1.0
 */
class ActionSchedulerJobMonitor {

    
/**
     * @var ActionSchedulerInterface
     */
    
protected $action_scheduler;

    
/**
     * ActionSchedulerInterface constructor.
     *
     * @param ActionSchedulerInterface $action_scheduler
     */
    
public function __constructActionSchedulerInterface $action_scheduler ) {
        
$this->action_scheduler $action_scheduler;
    }

    
/**
     * Check whether the failure rate is above a threshold within the last hour.
     *
     * To protect against failing jobs running forever the job's failure rate is checked before creating a new batch.
     * By default, a job is stopped if it has 5 failures in the last hour.
     *
     * @param ActionSchedulerJobInterface $job
     *
     * @throws JobException If the job's error rate is above the threshold.
     */
    
public function validate_failure_rateActionSchedulerJobInterface $job ) {
        
$failed_actions $this->action_scheduler->search(
            [
                
'hook'         => $job->get_process_item_hook(),
                
'status'       => $this->action_scheduler::STATUS_FAILED,
                
'per_page'     => $this->get_failure_rate_threshold(),
                
'date'         => gmdate'U' ) - HOUR_IN_SECONDS,
                
'date_compare' => '>',
            ],
            
'ids'
        
);

        if ( 
count$failed_actions ) === $this->get_failure_rate_threshold() ) {
            throw 
JobException::stopped_due_to_high_failure_rateesc_html$job->get_name() ) );
        }
    }

    
/**
     * Get the batched job failure rate threshold (per hour).
     *
     * @return int
     */
    
protected function get_failure_rate_threshold() {
        return 
absintapply_filters'automatewoo/batched_job_monitor/failure_rate_threshold') );
    }
}