PHP[008]:实现延迟触发器

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
<?php

$timeManager = [];
$timeMark = 0;

$i = 1000;
while ($i--) {
$jobTime = (time() + mt_rand(0, 1000));
echo (1000 - $i) . '个任务 将于' . date('Y-m-d H:i:s', $jobTime) . '执行' . PHP_EOL;
$timeManager[$jobTime][] = '$i 任务 ' . $i;
}

while (true) {
ksort($timeManager);
$timeMark = key($timeManager);
if (count($timeManager) > 0 && $timeMark < time()) {
var_dump($timeMark);
var_dump(date('Y-m-d H:i:s', $timeMark));
$job = $timeManager[$timeMark];
unset($timeManager[$timeMark]);
#$job = array_shift($timeManager);
} else {
sleep(1);
}
}