-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-event.php
More file actions
executable file
·98 lines (75 loc) · 2.9 KB
/
worker-event.php
File metadata and controls
executable file
·98 lines (75 loc) · 2.9 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
<?php
declare(strict_types=1);
/**
* @author Lutvip19 <lutvip19@gmail.com>
*/
// file: worker-event.php
if (!defined('BASEPATH')) {
define('BASEPATH', __DIR__ );
}
/**
* Require Worker Bootstrap File.
*/
require_once BASEPATH . '/cron/bootstrap.php';
use App\Core\Support\App;
use App\Core\Events\EventWorker;
// KONFIGURASI
$db = null;
$redisConfig = null;
// $redisConfig = [
// 'host' => config('redis.default.host'),
// 'port' => config('redis.default.port'),
// 'database' => config('redis.default.database'),
// 'password' => config('redis.default.password'),
// ];
// Scan semua listener secara otomatis
App::bootListeners();
// Inisialisasi variabel di luar try agar bisa diakses di catch/finally
$lockFile = null;
$once = true; // Set true jika ingin mode Cron/Once
$lockPath = BASEPATH . '/worker.lock';
// Setelah ini, ListenerRegistry sudah penuh dengan callback dari folder App/Listeners
try {
// --- Mencegah Zombie Process (Locking) ---
if ($once) {
$lockFile = fopen($lockPath, 'c');
if (!$lockFile || !flock($lockFile, LOCK_EX | LOCK_NB)) {
echo "[!] Worker is already running. Skipping this execution." . PHP_EOL;
if ($lockFile) fclose($lockFile);
exit(0); // Keluar dengan tenang karena memang sudah ada yang jalan
}
// Tulis PID (Process ID) ke dalam lock file untuk kebutuhan debugging
ftruncate($lockFile, 0);
fwrite($lockFile, (string) getmypid());
}
// $worker = new EventWorker($db, $redisConfig);
$worker = new EventWorker();
echo "[*] Event System Started. Mode: " . ($once ? "Once" : "Daemon") . PHP_EOL;
if(!$once) {
echo "[*] Untuk keluar tekan CTRL+C" . PHP_EOL;
}
// // Set nama tabel kustom jika perlu
// $worker->setTable('event_queue_b');
// // Contoh: Simpan data selama 7 hari dan bersihkan setiap 12 jam
// // (Opsional, jika Anda menambahkan properti ini di class)
// $worker->retentionDays = 7;
// // Waktu eksekusi x detik per listener default 5 detik (bisa disesuaikan)
// $worker->timePerListener = 3;
// // Timeout dasar minimal default 30 detik (Naikan/Turunkan sesuai kebutuhan)
// $worker->baseTimeout = 10;
// Mulai mendengarkan antrean (Redis/MySQL Fallback)
$worker->run($once);
} catch (\Throwable $e) { // Gunakan Throwable agar Fatal Error juga tertangkap
echo "Fatal Worker Error: " . $e->getMessage() . PHP_EOL;
echo "File: " . $e->getFile() . " Line: " . $e->getLine() . PHP_EOL;
exit(1);
} finally {
// --- Handler Lock di Akhir (Selalu dijalankan) ---
if ($once && $lockFile) {
flock($lockFile, LOCK_UN);
fclose($lockFile);
// Opsional: Hapus file jika ingin folder tetap bersih
if (file_exists($lockPath)) unlink($lockPath);
echo "[*] Lock released and worker finished." . PHP_EOL;
}
}