php 使用moodle-cron-API添加cronjob无法正常工作

xdnvmnnf  于 2022-12-25  发布在  PHP
关注(0)|答案(1)|浏览(107)

我正在尝试按照以下说明在moodle-cron-api中调度任务:https://docs.moodle.org/dev/Task_API.
根文件夹位于/local,根文件夹的名称为mod_hygene。
我有一个cut_my_toe_nails.php在/local/mod_hygene/classes/task它是:

namespace mod_hygene\task;

/**
* An example of a scheduled task.
*/
class cut_my_toe_nails extends \core\task\scheduled_task {

/**
* Return the task's name as shown in admin screens.
*
* @return string
*/
public function get_name() {
return get_string('cutmytoenails', 'mod_hygene');
}

/**
* Execute the task.
*/
public function execute() {
// Apply fungus cream.
// Apply chainsaw.
// Apply olive oil.
echo 'do';
}
}

下面是/mod_hygene/db/tasks. php文件夹:

$tasks = [
[
'classname' => 'mod_hygene\task\cut_my_toe_nails',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '1,7',
'dayofweek' => '*',
],
];

正如你所看到的,任务应该每分钟运行一次。

docker-compose up -d --build moodle

在终端我应该看到'do'打印每分钟。但是,我没有看到任何东西。我徘徊到网站管理/服务器/任务计划。在那里我没有看到这个任务。但我检查了网站管理/插件/插件概述,可以找到local_mod_hygene在本地插件。
有人能帮我吗?我需要对Dockerfile做一些修改吗?

pvcm50d1

pvcm50d11#

只是一个提示,不要使用前缀mod_为本地插件,它可能会被混淆为一个活动插件在mod文件夹。
假设您的代码在/local/hygene/
任务文件应为/local/hygene/classes/task/cut_my_toe_nails.php
类命名空间应为

namespace local_hygene\task;

任务文件应为/local/hygene/db/tasks.php,其中

'classname' => 'local_hygene\task\cut_my_toe_nails',

安装后,检查是否通过启用了任务
站点管理〉服务器〉任务
或直接到
yourwebsite/admin/tool/task/scheduledtasks.php
如果已安装并启用,则从命令行运行它以进行测试。
首先查看其是否已列出-如果未列出,则说明安装不正确

php admin/cli/scheduled_task.php  --list

如果已列出,请记下名称并手动运行任务-请注意,必须使用\\转义\

php admin/cli/scheduled_task.php --execute=\\local_hygene\\task\\cut_my_toe_nails

如果任务运行正常,则等待cron
或者,根据设置,您可以在以管理员身份登录时通过yourmoodlewebsite/admin/cron.php手动运行cron
或从命令行

php admin/cli/cron.php

相关问题