crontab定时任务组件,秒级任务会重复执行

september

bug描述

定时任务组件,1s执行一次的任务经常会重复执行

程序代码

        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

系统环境及workerman/webman等具体版本

mac和linux出现了,workerman/crontab版本v1.0.4

1260 3 0
3个回答

admin

是不是因为多进程的关系 被并发执行了

  • september 2023-02-17

    webman框架下调用的定时任务组件,基本都是默认配置

  • september 2023-02-17

    是偶尔出现,不是一直出现

walkor

如果你开了2个进程,每个进程定时执行一次可能会出现这种情况

  • september 2023-02-17

    老大好,配置项是webman默认的配置,这个在哪里看呢

  • walkor 2023-02-17

    webman进程数在 config/server.php 里用count设置。
    自定义进程在config/process.php里用count参数设置。

  • september 2023-02-17

    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
    ],
    ];

yzh52521

如果一个进程 你加redis 就是毫无意义 ,本来就是 一秒执行一次

  • september 2023-02-17

    执行了两次,所以我加锁了...

  • yzh52521 2023-02-17

    执行两次说明2个进程呗

  • yzh52521 2023-02-17

    设置 count =1

  • september 2023-02-17

    我试试

  • september 2023-02-17

    process.php 里设置了 'task' => [
    'handler' => process\Task::class,
    'count' => 1
    ],还是有重复执行的

  • walkor 2023-02-17

    定时器放在哪里执行的。把process.php里'task'进程配置删除,看下是否还有地方执行定时器

  • september 2023-02-17

    定时器只配置在process.php中执行,删除后没有再执行过了

  • september 2023-02-17

    已发送

  • walkor 2023-02-17

    你的workerman/crontab不是最新的,升级 workerman/crontab 升级到 v1.0.6

    composer workerman/crontab ^1.0.6
  • september 2023-02-17

    我试试

年代过于久远,无法发表回答
🔝