定時(shí)任務(wù)組件,1s執(zhí)行一次的任務(wù)經(jīng)常會(huì)重復(fù)執(zhí)行
new \Workerman\Crontab\Crontab('*/1 * * * * *', function () {
$time = time();
$timeLock = "task_lock:" . $time;
if (Redis::exists($timeLock)) {
echo "當(dāng)前秒存在任務(wù)";
return;
}
Redis::set($timeLock, 1);
echo date('Y-m-d H:i:s') . PHP_EOL;
});
這里是輸出
2023-02-17 09:38:00
當(dāng)前秒存在任務(wù)2023-02-17 09:38:01
當(dāng)前秒存在任務(wù)2023-02-17 09:38:02
當(dāng)前秒存在任務(wù)2023-02-17 09:38:03
當(dāng)前秒存在任務(wù)2023-02-17 09:38:04
當(dāng)前秒存在任務(wù)2023-02-17 09:38:05
當(dāng)前秒存在任務(wù)2023-02-17 09:38:06
當(dāng)前秒存在任務(wù)2023-02-17 09:38:07
當(dāng)前秒存在任務(wù)2023-02-17 09:38:08
當(dāng)前秒存在任務(wù)2023-02-17 09:38:09
當(dāng)前秒存在任務(wù)2023-02-17 09:38:10
當(dāng)前秒存在任務(wù)2023-02-17 09:38:11
當(dāng)前秒存在任務(wù)2023-02-17 09:38:12
當(dāng)前秒存在任務(wù)2023-02-17 09:38:13
當(dāng)前秒存在任務(wù)2023-02-17 09:38:14
當(dāng)前秒存在任務(wù)2023-02-17 09:38:15
當(dāng)前秒存在任務(wù)2023-02-17 09:38:16
當(dāng)前秒存在任務(wù)2023-02-17 09:38:17
mac和linux出現(xiàn)了,workerman/crontab版本v1.0.4
如果你開了2個(gè)進(jìn)程,每個(gè)進(jìn)程定時(shí)執(zhí)行一次可能會(huì)出現(xiàn)這種情況
webman進(jìn)程數(shù)在 config/server.php 里用count設(shè)置。
自定義進(jìn)程在config/process.php里用count參數(shù)設(shè)置。
process.php設(shè)置是這樣的,沒有設(shè)置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
],
];