PluginProbe
MailerLite – WooCommerce integration / trunk
MailerLite – WooCommerce integration vtrunk
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / jobs / WooMailerLiteAbstractJob.php

WooMailerLiteAbstractJob.php in MailerLite – WooCommerce integration trunk, at includes/jobs/WooMailerLiteAbstractJob.php

111 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class WooMailerLiteAbstractJob
4 {
5 private $serial = true;
6
7 protected static $delay = 0;
8
9 public static $jobModel;
10
11 protected $retryDelay = 10;
12
13 protected $maxRetries = 3;
14
15 protected $resourceLimit = 100;
16
17 abstract public function handle($data = []);
18
19 public static function getInstance()
20 {
21 return new static();
22 }
23
24 public static function dispatch(array $data = []): void
25 {
26 $data['attempts'] = $data['attempts'] ?? 0;
27
28 // In local/test (e.g. wp-env), run sync immediately so jobs don't stay pending.
29 if (!isset($data['selfMechanism']['sync']) && defined('WOO_MAILERLITE_SYNC_IMMEDIATE') && WOO_MAILERLITE_SYNC_IMMEDIATE) {
30 $data['selfMechanism']['sync'] = true;
31 }
32
33 $jobClass = static::class;
34 $objectId = 0;
35
36 if ((!isset($data['selfMechanism']['sync']) || !$data['selfMechanism']['sync']) && function_exists('as_enqueue_async_action')) {
37 $objectId = as_enqueue_async_action($jobClass, [$data]);
38 WooMailerLiteCache::set('scheduled_jobs', true, 300);
39 }
40
41 static::$jobModel = WooMailerLiteJob::updateOrCreate(
42 ['job' => $jobClass],
43 ['object_id' => $objectId, 'data' => $data]
44 );
45
46 if (isset($data['selfMechanism']['sync'])) {
47 static::getInstance()->runSafely($data);
48 }
49 }
50
51 public static function dispatchSync(array $data = []): void
52 {
53 $data['selfMechanism']['sync'] = true;
54 static::dispatch($data);
55 }
56
57 public function runSafely($data = [])
58 {
59 try {
60 if (!static::$jobModel) {
61 static::$jobModel = WooMailerLiteJob::where('job', static::class)->first();
62 }
63
64 $this->handle($data);
65
66 if (static::$jobModel) {
67 static::$jobModel->delete();
68 }
69 WooMailerLiteCache::delete('scheduled_jobs');
70 return true;
71
72 } catch (Throwable $th) {
73 WooMailerLiteCache::delete('scheduled_jobs');
74 WooMailerLiteLog()->error("Failed Job: " . static::class, [
75 'error' => $th->getMessage(),
76 'trace' => $th->getTraceAsString()
77 ]);
78
79 $attempts = $data['attempts'] ?? 0;
80 $attempts++;
81
82 if (static::$jobModel) {
83 static::$jobModel->update([
84 'data' => array_merge($data, [
85 'status' => 'failed',
86 'error' => $th->getMessage(),
87 'attempts' => $attempts,
88 ])
89 ]);
90 }
91
92 if ($attempts < $this->maxRetries) {
93 $data['attempts'] = $attempts;
94
95 if (!isset($data['selfMechanism']['sync']) && function_exists('as_enqueue_async_action')) {
96 as_enqueue_async_action(static::class, [$data]);
97 }
98 } else {
99 WooMailerLiteLog()->error("Job " . static::class . " failed after max retries.", ['trace' => $th->getTraceAsString()]);
100 }
101 }
102 return true;
103 }
104
105 public static function delay(int $delay)
106 {
107 static::$delay = $delay;
108 return new static();
109 }
110 }
111