{"id":21433727,"url":"https://github.com/lvinkim/swim-console","last_synced_at":"2026-05-19T11:05:01.961Z","repository":{"id":62520548,"uuid":"145201231","full_name":"lvinkim/swim-console","owner":"lvinkim","description":"使用 swoole 对 symfony console 的守护任务，计划定时任务，定时器任务","archived":false,"fork":false,"pushed_at":"2018-09-24T04:24:20.000Z","size":40,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-23T08:45:52.439Z","etag":null,"topics":["console","crontab","daemon","php7","process","shell","swoole","timer"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lvinkim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-18T07:54:20.000Z","updated_at":"2018-09-24T04:24:22.000Z","dependencies_parsed_at":"2022-11-02T10:31:51.151Z","dependency_job_id":null,"html_url":"https://github.com/lvinkim/swim-console","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvinkim%2Fswim-console","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvinkim%2Fswim-console/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvinkim%2Fswim-console/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvinkim%2Fswim-console/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lvinkim","download_url":"https://codeload.github.com/lvinkim/swim-console/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243945525,"owners_count":20372894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["console","crontab","daemon","php7","process","shell","swoole","timer"],"created_at":"2024-11-22T23:29:37.247Z","updated_at":"2026-05-19T11:05:01.884Z","avatar_url":"https://github.com/lvinkim.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swim-console\n使用 swoole 对 symfony console 的守护任务，计划定时任务，定时重复任务\n\n\u003e 版本支持  \n\u003e php \u003e= 7.1  \n\u003e swoole \u003e= 1.7.7  \n\n\n### 安装\n```php\ncomposer require lvinkim/swim-console\n```\n\n### 守护任务\n\n```php\nrequire_once dirname(__DIR__) . '/../../vendor/autoload.php';\n\n$debug = false;\n$logDir = __DIR__ . '/../var';\n$date = date('Y-m-d');\n\ntry {\n\n    $daemonJobber = new \\Lvinkim\\SwimConsole\\DaemonJobber($debug);\n    $daemonJobber-\u003esetDebugLogFile($logDir . \"/daemon-job-debug.log.\" . $date);\n\n    $daemonJobber-\u003eadd('cmd-first', [\n        'command' =\u003e \\Tests\\App\\Command\\DaemonFirstCommand::class,\n        \"commandParam\" =\u003e [\"pass-\" . rand(100, 999)],\n        'sleep' =\u003e 2,   // s\n        'enabled' =\u003e true,\n        \"output\" =\u003e $logDir . \"/daemon-cmd-first.log.\" . $date,\n    ]);\n\n    $daemonJobber-\u003eadd('cmd-second', [\n        'command' =\u003e \\Tests\\App\\Command\\DaemonSecondCommand::class,\n        'commandOptions' =\u003e [\"--caller\" =\u003e \"daemon\"],\n        'depends' =\u003e true,\n        'enabled' =\u003e true,\n        \"output\" =\u003e $logDir . \"/daemon-cmd-second.log.\" . $date,\n    ]);\n\n    $daemonJobber-\u003erun();\n\n} catch (\\Throwable $e) {\n    null;\n}\n```\n\n\n### 计划定时任务\n\n```php\nrequire dirname(__DIR__) . '/../../vendor/autoload.php';\n\n$debug = false;\n$period = 60 * 1000;    // 每隔 60*1000ms (1分钟) 触发一次\n$mission = function ($timerId) use ($debug) {\n\n    $logDir = __DIR__ . '/../var';\n    $date = date('Y-m-d');\n\n    try {\n\n        $crontabJobber = new \\Lvinkim\\SwimConsole\\CrontabJobber($debug);\n        $crontabJobber-\u003esetDebugLogFile($logDir . \"/crontab-job-debug.log.\" . $date);\n\n        $crontabJobber-\u003eadd(\"cmd-first\", [\n            \"command\" =\u003e \\Tests\\App\\Command\\CrontabFirstCommand::class,\n            \"schedule\" =\u003e \"* * * * *\",\n            \"enabled\" =\u003e true,\n            \"output\" =\u003e $logDir . \"/crontab-cmd-first.log.\" . $date,\n        ]);\n\n        $crontabJobber-\u003eadd('cmd-second', [\n            \"command\" =\u003e \\Tests\\App\\Command\\CrontabSecondCommand::class,\n            \"commandOptions\" =\u003e [\"--caller\" =\u003e \"crontab\"],\n            \"schedule\" =\u003e '* * * * *',\n            \"enabled\" =\u003e true,\n            \"output\" =\u003e $logDir . \"/crontab-cmd-second.log.\" . $date,\n        ]);\n\n        $crontabJobber-\u003erun();\n\n    } catch (\\Throwable $e) {\n        null;\n    }\n};\n\nif ($debug) {\n    $mission(uniqid());\n} else {\n    $timer = swoole_timer_tick($period, $mission);\n}\n\n```\n\n\n### 定时重复任务\n\n```php\nrequire dirname(__DIR__) . '/../../vendor/autoload.php';\n\n$debug = false;\n$logDir = __DIR__ . '/../var';\n$date = date('Y-m-d');\n\ntry {\n\n    $repeatJobber = new Lvinkim\\SwimConsole\\RepeatJobber($debug);\n    $repeatJobber-\u003esetDebugLogFile($logDir . \"/repeat-job-debug.log.\" . $date);\n\n    $repeatJobber-\u003eadd('cmd-first', [\n        'command' =\u003e \\Tests\\App\\Command\\RepeatFirstCommand::class,\n        'depends' =\u003e true,\n        'enabled' =\u003e true,\n        \"output\" =\u003e $logDir . \"/repeat-cmd-first.log.\" . $date,\n    ]);\n\n    $repeatJobber-\u003eadd('cmd-second', [\n        'command' =\u003e \\Tests\\App\\Command\\RepeatSecondCommand::class,\n        'interval' =\u003e 8000, // ms\n        'enabled' =\u003e true,\n        \"output\" =\u003e $logDir . \"/repeat-cmd-second.log.\" . $date,\n    ]);\n\n    $repeatJobber-\u003erun();\n\n} catch (\\Error $e) {\n    null;\n} catch (Exception $e) {\n    null;\n} finally {\n    null;\n}\n```\n\n### 计划定时系统命令\n\n```php\n\nrequire dirname(__DIR__) . '/../../vendor/autoload.php';\n\n$debug = false;\n$period = 60 * 1000;    // 每隔 60*1000ms (1分钟) 触发一次\n$mission = function ($timerId) use ($debug) {\n\n    $console = __DIR__ . '/console.php';\n    $logDir = __DIR__ . '/../var';\n    $date = date('Y-m-d');\n\n    try {\n\n        $shellJobber = new \\Lvinkim\\SwimConsole\\ShellJobber($debug);\n        $shellJobber-\u003esetDebugLogFile($logDir . \"/shell-crontab-job-debug.log.\" . $date);\n\n        $shellJobber-\u003eadd(\"cmd-first\", [\n            \"command\" =\u003e \"/usr/bin/env php {$console} cmd:shell-crontab:first --caller=shell-crontab\",\n            \"schedule\" =\u003e \"* * * * *\",\n            \"enabled\" =\u003e true,\n            \"output\" =\u003e $logDir . \"/shell-crontab-cmd-first.log.\" . $date,\n        ]);\n\n        $shellJobber-\u003eadd(\"cmd-second\", [\n            \"command\" =\u003e \"/usr/bin/env php {$console} cmd:shell-crontab:second --caller=shell-crontab\",\n            \"schedule\" =\u003e \"* * * * *\",\n            \"enabled\" =\u003e true,\n            \"output\" =\u003e $logDir . \"/shell-crontab-cmd-second.log.\" . $date,\n        ]);\n\n        $shellJobber-\u003erun();\n\n    } catch (\\Throwable $e) {\n        null;\n    }\n};\n\nif ($debug) {\n    $mission(uniqid());\n} else {\n    $timer = swoole_timer_tick($period, $mission);\n}\n\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flvinkim%2Fswim-console","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flvinkim%2Fswim-console","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flvinkim%2Fswim-console/lists"}