PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / QueueService.php

QueueService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/QueueService.php

69 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Services;
4
5 use Averta\WordPress\Utility\JSON;
6 use Depicter;
7
8 class QueueService
9 {
10 const CRON_HOOK = 'depicter/queue/cron';
11
12 public function __construct() {
13 add_action( 'init', [ $this, 'schedule_queue_table' ] );
14 add_filter( 'cron_schedules', [ $this, 'cron_schedules' ] );
15 add_action( self::CRON_HOOK, [ $this, 'process' ] );
16 }
17
18 public function cron_schedules( $schedules ) {
19 $schedules['every_minute'] = [
20 'interval' => 60,
21 'display' => __( 'Every Minute', 'depicter' )
22 ];
23
24 return $schedules;
25 }
26
27 public function schedule_queue_table() {
28 if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
29 wp_schedule_event( time(), 'every_minute', self::CRON_HOOK );
30 }
31 }
32 public function process() {
33 $jobs = \Depicter::queueJobsRepository()->job()->where('status', 'in', ['pending', 'failed'])->take(5)->get();
34 if ( ! $jobs ) {
35 return;
36 }
37
38 $jobs = $jobs->toArray();
39 foreach ( $jobs as $job ) {
40 \Depicter::queueJobsRepository()->update( $job['id'], [
41 'status' => 'processing',
42 ] );
43
44 $success = false;
45 $last_error = '';
46 $payload = JSON::decode($job['payload'], true);
47 if ( $job['queue'] == 'mailchimp' ) {
48 $response = \Depicter::integration()->mailchimp()->submitToMailchimp( $payload['lead_id'] );
49 $success = $response['success'];
50 $last_error = $success ? '' : $response['error'];
51 }
52
53 if ( $success ) {
54 \Depicter::queueJobsRepository()->update( $job['id'], [
55 'status' => 'completed',
56 ] );
57 } else {
58 \Depicter::queueJobsRepository()->update( $job['id'], [
59 'status' => 'failed',
60 'attempts' => $job['attempts'] + 1,
61 'last_error' => $last_error,
62 ]);
63 }
64 }
65 }
66
67
68 }
69