-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-job.php
More file actions
118 lines (95 loc) · 3.6 KB
/
run-job.php
File metadata and controls
118 lines (95 loc) · 3.6 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
<?php
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
die("This script should only be run from the command line.");
}
// Bootstrap the Laravel application
$app = require_once __DIR__ . '/bootstrap/app.php';
// Ensure the app is bootstrapped fully
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
// Access Laravel's service container
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$config = require __DIR__ . '/config/background-jobs.php';
// Get input arguments
$className = $argv[1] ?? null;
$methodName = $argv[2] ?? null;
$params = isset($argv[3]) ? explode(',', $argv[3]) : [];
try {
if (!class_exists($className)) {
throw new Exception("Class $className does not exist.");
}
if (!isset($config['allowed_jobs'][$className])) {
throw new Exception("Execution of class $className is not allowed.");
}
$classInstance = new $className(...$params);
if (!method_exists($classInstance, $methodName)) {
throw new Exception("Method $methodName does not exist in class $className.");
}
if (!in_array($methodName, $config['allowed_jobs'][$className])) {
throw new Exception("Execution of method $methodName in class $className is not allowed.");
}
executeJobWithRetry($className, $classInstance, $methodName, $params, $config);
} catch (Exception $e) {
logJobError($e);
}
/**
* Execute the job with retry logic.
*
* @param string $className The class name
* @param object $classInstance The class instance
* @param string $methodName The method to call
* @param array $params The parameters to pass to the method
* @return void
*/
function executeJobWithRetry($className, $classInstance, $methodName, $params, $config)
{
$maxAttempts = $config['retry_attempts'];
$retryDelay = $config['retry_delay'];
$attempts = 0;
while ($attempts < $maxAttempts) {
try {
$attempts++;
$result = call_user_func_array([$classInstance, $methodName], $params);
logJobExecution($className, $methodName, 'success');
break;
} catch (Exception $e) {
if ($attempts < $maxAttempts) {
logJobExecution($className, $methodName, 'retry', $e->getMessage());
sleep($retryDelay);
} else {
logJobExecution($className, $methodName, 'failure', $e->getMessage());
}
}
}
}
/**
* Log the job execution details.
*
* @param string $className The class name
* @param string $methodName The method name
* @param string $status The job status (success, retry, failure)
* @param string|null $error The error message if failed
* @return void
*/
function logJobExecution($className, $methodName, $status, $error = null)
{
$logFile = __DIR__ . '/storage/logs/background_jobs.log';
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[$timestamp] Class: $className, Method: $methodName, Status: $status";
if ($status === 'failure' || $status === 'retry') {
$logMessage .= ", Error: $error";
}
file_put_contents($logFile, $logMessage . PHP_EOL, FILE_APPEND);
}
/**
* Log the full exception to errors log.
*/
function logJobError(Exception $e)
{
$errorLogFile = __DIR__ . '/storage/logs/background_jobs_errors.log';
$timestamp = date('Y-m-d H:i:s');
$errorMessage = "[$timestamp] Exception: " . $e->getMessage() .
" in " . $e->getFile() . " on line " . $e->getLine() .
"\nStack trace:\n" . $e->getTraceAsString() . "\n";
file_put_contents($errorLogFile, $errorMessage . PHP_EOL, FILE_APPEND);
}