定時任務組件,1s執(zhí)行一次的任務經(jīng)常會重復執(zhí)行
new \Workerman\Crontab\Crontab('*/1 * * * * *', function () {
$time = time();
$timeLock = "task_lock:" . $time;
if (Redis::exists($timeLock)) {
echo "當前秒存在任務";
return;
}
Redis::set($timeLock, 1);
echo date('Y-m-d H:i:s') . PHP_EOL;
});
這里是輸出
2023-02-17 09:38:00
當前秒存在任務2023-02-17 09:38:01
當前秒存在任務2023-02-17 09:38:02
當前秒存在任務2023-02-17 09:38:03
當前秒存在任務2023-02-17 09:38:04
當前秒存在任務2023-02-17 09:38:05
當前秒存在任務2023-02-17 09:38:06
當前秒存在任務2023-02-17 09:38:07
當前秒存在任務2023-02-17 09:38:08
當前秒存在任務2023-02-17 09:38:09
當前秒存在任務2023-02-17 09:38:10
當前秒存在任務2023-02-17 09:38:11
當前秒存在任務2023-02-17 09:38:12
當前秒存在任務2023-02-17 09:38:13
當前秒存在任務2023-02-17 09:38:14
當前秒存在任務2023-02-17 09:38:15
當前秒存在任務2023-02-17 09:38:16
當前秒存在任務2023-02-17 09:38:17
mac和linux出現(xiàn)了,workerman/crontab版本v1.0.4
如果你開了2個進程,每個進程定時執(zhí)行一次可能會出現(xiàn)這種情況
webman進程數(shù)在 config/server.php 里用count設置。
自定義進程在config/process.php里用count參數(shù)設置。
process.php設置是這樣的,沒有設置count<?php
/**
use Workerman\Worker;
return [
// File update detection and automatic reload
'monitor' => [
'handler' => process\Monitor::class,
'reloadable' => false,
'constructor' => [
// Monitor these directories
'monitor_dir' => array_merge([
app_path(),
config_path(),
base_path() . '/process',
base_path() . '/support',
base_path() . '/resource',
base_path() . '/.env',
], glob(base_path() . '/plugin//app'), glob(base_path() . '/plugin//config'), glob(base_path() . '/plugin/*/api')),
// Files with these suffixes will be monitored
'monitor_extensions' => [
'php', 'html', 'htm', 'env'
],
'options' => [
'enable_file_monitor' => !Worker::$daemonize && DIRECTORY_SEPARATOR === '/',
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
]
]
],
'task' => [
'handler' => process\Task::class
],
];