-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.php
More file actions
executable file
·102 lines (80 loc) · 3.32 KB
/
worker.php
File metadata and controls
executable file
·102 lines (80 loc) · 3.32 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
<?php
/**
* @author Lutvip19 <lutvip19@gmail.com>
*/
if (!defined('BASEPATH')) {
define('BASEPATH', __DIR__ );
}
// Require Worker Bootstrap File.
require_once BASEPATH . '/cron/bootstrap.php';
use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\WorkerProcess;
use PHPStreamServer\Plugin\Scheduler\SchedulerPlugin;
use PHPStreamServer\Plugin\Scheduler\Worker\PeriodicProcess;
$server = new Server();
$server->addPlugin(
new SchedulerPlugin(),
);
$server->addWorker(
new WorkerProcess(
name: 'Worker',
count: 1,
onStart: static function (WorkerProcess $worker): void {
// Logger
$worker->logger->notice("Hello from worker!");
$worker->logger->info('Hello from worker', ['pid' => \posix_getpid()]);
}
),
new PeriodicProcess(
name: 'Periodic process 1',
schedule: '*/1 * * * *',
onStart: function (PeriodicProcess $worker): void {
// process include file php
// Logger
$worker->logger->info('Scheduler 1 is running', ['at' => date('d-m-Y H:i:s')]);
$worker->logger->info('Scheduler 1', ['pid' => \posix_getpid()]);
// Run included php file
include BASEPATH . '/cron/sample.php';
},
),
new PeriodicProcess(
name: 'Periodic process 2',
schedule: '*/2 * * * *',
onStart: function (PeriodicProcess $worker): void {
// process execute file php-ffi
// Logger
$worker->logger->info('Scheduler 2 is running', ['at' => date('d-m-Y H:i:s')]);
$worker->logger->info('Scheduler 2', ['pid' => \posix_getpid()]);
// Execute the command (Linux/macOS) or (Windows)
$source_path = BASEPATH . '/cron/test.php';
// append the output directly to the log file (opsi 1)
// $logFile = BASEPATH . '/cron_log.txt';
$logFile = logs_path('cron_log.txt');
system("php $source_path >> $logFile", $return_code);
// // or display the output directly to the console||browser (opsi 2)
// $last_line = system("php $source_path", $return_code);
// echo "Last line of output: $last_line" . PHP_EOL;
echo "Return code: $return_code" . PHP_EOL;
},
),
new PeriodicProcess(
name: 'Periodic process 3',
schedule: '*/1 * * * *',
onStart: function (PeriodicProcess $worker): void {
// process execute event worker
// Logger
$worker->logger->info('Scheduler 3 is running', ['at' => date('d-m-Y H:i:s')]);
$worker->logger->info('Scheduler 3', ['pid' => \posix_getpid()]);
// Execute the command (Linux/macOS) or (Windows)
$source_path = BASEPATH . '/worker-event.php';
// // append the output directly to the log file (opsi 1)
// $logFile = logs_path('events_log.txt');
// system("php $source_path >> $logFile", $return_code);
// or display the output directly to the console||browser (opsi 2)
$last_line = system("php $source_path", $return_code);
echo "Last line of output: $last_line" . PHP_EOL;
echo "Return code: $return_code" . PHP_EOL;
},
),
);
exit($server->run());