-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleQueue.module.php
More file actions
200 lines (166 loc) · 7.46 KB
/
SimpleQueue.module.php
File metadata and controls
200 lines (166 loc) · 7.46 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace ProcessWire;
/** @property \ProcessWire\ProcessWire $wire */
class SimpleQueue extends WireData implements Module, ConfigurableModule
{
/** @var \SimpleWire\Queue\Queue */
protected $queue;
public static function getModuleInfo(): array
{
return [
'title' => 'SimpleQueue',
'version' => '0.1.0',
'summary' => 'Background job queue with priority, delayed execution, retry, and LazyCron-based processing.',
'icon' => 'tasks',
'author' => 'WireCodex',
'autoload' => true,
'singular' => true,
'requires' => 'ProcessWire>=3.0.200,PHP>=8.1',
];
}
// ========================================
// Lifecycle
// ========================================
public function init(): void
{
spl_autoload_register(function (string $class): void {
$prefix = 'SimpleWire\\Queue\\';
if (!str_starts_with($class, $prefix)) return;
$relative = substr($class, strlen($prefix));
// Support drivers/ subfolder: e.g. SimpleWire\Queue\FileDriver → classes/drivers/FileDriver.php
$file = __DIR__ . '/classes/' . str_replace('\\', '/', $relative) . '.php';
if (!file_exists($file)) {
$file = __DIR__ . '/classes/drivers/' . $relative . '.php';
}
if (file_exists($file)) require_once $file;
});
$config = array_merge(
\SimpleWire\Queue\Queue::getDefaults(),
(array) $this->wire('modules')->getConfig($this)
);
$this->queue = new \SimpleWire\Queue\Queue($this->wire, $config);
$this->wire('simplequeue', $this->queue);
require_once __DIR__ . '/functions.php';
}
// ========================================
// Install / Uninstall
// ========================================
public function ___install(): void
{
$cachePath = $this->wire('config')->paths->cache . 'SimpleWire/Queue/';
if (!is_dir($cachePath)) {
wireMkdir($cachePath, true);
}
$jobsPath = $this->wire('config')->paths->site . 'jobs/';
if (!is_dir($jobsPath)) {
wireMkdir($jobsPath, true);
}
}
public function ___uninstall(): void
{
// Remove file-driver cache directory
$cacheDir = $this->wire('config')->paths->cache . 'SimpleWire/Queue/';
if (is_dir($cacheDir)) {
wireRmdir($cacheDir, true);
}
// Drop the database table if DatabaseDriver was in use
try {
$driver = new \SimpleWire\Queue\DatabaseDriver($this->wire);
$driver->dropTable();
} catch (\Throwable $e) {
// Table may not exist — safe to ignore
}
}
// ========================================
// Config UI
// ========================================
public static function getModuleConfigInputfields(array $data): InputfieldWrapper
{
$modules = wire()->modules;
/** @var InputfieldWrapper $wrapper */
$wrapper = $modules->get('InputfieldWrapper');
// ---- Driver & Throughput ----
/** @var \ProcessWire\InputfieldFieldset $fieldset */
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->label = 'Driver & Throughput';
$fieldset->icon = 'tasks';
/** @var \ProcessWire\InputfieldSelect $field */
$field = $modules->get('InputfieldSelect');
$field->name = 'queue_driver';
$field->label = 'Queue Driver';
$field->description = 'Storage driver for the queue';
$field->addOption('FileDriver', 'File Driver (JSON files)');
$field->addOption('DatabaseDriver', 'Database Driver (ProcessWire database)');
$field->value = $data['queue_driver'] ?? 'FileDriver';
$field->columnWidth = 50;
$fieldset->add($field);
/** @var \ProcessWire\InputfieldInteger $field */
$field = $modules->get('InputfieldInteger');
$field->name = 'queue_jobsPerRun';
$field->label = 'Jobs Per Run';
$field->description = 'Maximum number of jobs to process in each LazyCron run';
$field->value = $data['queue_jobsPerRun'] ?? 10;
$field->min = 1;
$field->max = 100;
$field->columnWidth = 50;
$fieldset->add($field);
$wrapper->add($fieldset);
// ---- LazyCron Settings ----
/** @var \ProcessWire\InputfieldFieldset $fieldset */
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->label = 'LazyCron Settings';
$fieldset->icon = 'clock-o';
/** @var \ProcessWire\InputfieldCheckbox $field */
$field = $modules->get('InputfieldCheckbox');
$field->name = 'queue_processOnLazyCron';
$field->label = 'Process via LazyCron';
$field->description = 'Automatically process the queue using ProcessWire LazyCron';
$field->checked = !empty($data['queue_processOnLazyCron']);
$field->columnWidth = 50;
$fieldset->add($field);
/** @var \ProcessWire\InputfieldSelect $field */
$field = $modules->get('InputfieldSelect');
$field->name = 'queue_lazyCronInterval';
$field->label = 'LazyCron Interval';
$field->description = 'How often to process the queue automatically';
$field->addOption('every30Seconds', 'Every 30 seconds');
$field->addOption('everyMinute', 'Every minute');
$field->addOption('every2Minutes', 'Every 2 minutes');
$field->addOption('every5Minutes', 'Every 5 minutes');
$field->addOption('every15Minutes', 'Every 15 minutes');
$field->addOption('every30Minutes', 'Every 30 minutes');
$field->addOption('everyHour', 'Every hour');
$field->value = $data['queue_lazyCronInterval'] ?? 'everyMinute';
$field->showIf = 'queue_processOnLazyCron=1';
$field->columnWidth = 50;
$fieldset->add($field);
$wrapper->add($fieldset);
// ---- Pruning ----
/** @var \ProcessWire\InputfieldFieldset $fieldset */
$fieldset = $modules->get('InputfieldFieldset');
$fieldset->label = 'Pruning';
$fieldset->icon = 'trash';
/** @var \ProcessWire\InputfieldCheckbox $field */
$field = $modules->get('InputfieldCheckbox');
$field->name = 'queue_autoPrune';
$field->label = 'Auto-Prune Completed Jobs';
$field->description = 'Automatically remove old completed jobs once per day';
$field->checked = !empty($data['queue_autoPrune']);
$field->columnWidth = 50;
$fieldset->add($field);
/** @var \ProcessWire\InputfieldInteger $field */
$field = $modules->get('InputfieldInteger');
$field->name = 'queue_pruneAfter';
$field->label = 'Prune After (seconds)';
$field->description = 'Remove completed jobs older than this many seconds';
$field->notes = 'Default: 604800 (7 days)';
$field->value = $data['queue_pruneAfter'] ?? 604800;
$field->min = 3600;
$field->showIf = 'queue_autoPrune=1';
$field->columnWidth = 50;
$fieldset->add($field);
$wrapper->add($fieldset);
return $wrapper;
}
}