如何在 Magento 模块中设置 cron 作业?

2021-12-19 00:00:00 xml php magento

我想在我的模块中设置一个 cron 作业.我按照 Magento wiki 上的说明进行操作 - how_to_setup_a_cron_job,但我的 cron 作业根本没有执行.

I wanted to setup a cron job inside my module. I followed the instructions on Magento wiki - how_to_setup_a_cron_job, but my cron job is simply not executing.

这是我的 config.xml (app/code/local/Roomstory/Invoice/etc/config.xml)

This is my config.xml (app/code/local/Roomstory/Invoice/etc/config.xml)

<?xml version="1.0"?>
<config>    
    <modules>
        <Roomstory_Invoice>
            <version>0.1.1</version>
        </Roomstory_Invoice>
    </modules>
<!-- -->
    <crontab>
        <jobs>
            <roomstoryinvoice_setstatus>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>roomstory_invoice/setstatus::run</model></run>
            </roomstoryinvoice_setstatus>
        </jobs>
    </crontab>
</config>

这是我的课.(app/code/local/Roomstory/Invoice/Model/Setstatus.php)

And this is my class. (app/code/local/Roomstory/Invoice/Model/Setstatus.php)

<?php
class Roomstory_Invoice_Model_Setstatus {

  public function run() {
    return true;
  }

}
?>

我安装了一个 Cron 调度程序模块,它显示了我列出的 cron 作业,但是当我尝试立即运行"(用于调试)时,出现错误 -

I have installed a Cron Scheduler Module, which shows my cron job listed, but when I try to "run now" (for debugging), I get error -

无效回调:roomstory_invoice/setstatus::run 不存在

Invalid callback: roomstory_invoice/setstatus::run does not exist

这很简单,经过多次尝试,我仍然无法找到错误.请告诉一些其他的方法来做到这一点,或者指出这段代码中的错误.

This something simple, after much trying, I am still not able to find the error. Please tell some other way to do it, or indicate the error in this code.

谢谢!

推荐答案

在你的模块 config.xml 中加入以下内容:

In your modules config.xml put the following:

<config>
    <global>
        <models>
            <roomstoryinvoicecron>
                <class>Roomstory_Invoice_Model</class>
            </roomstoryinvoicecron>                         
        </models>
    </global>
    <crontab>
        <jobs>
            <roomstoryinvoicecron>
                <schedule>
                    <cron_expr>*/10 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>roomstoryinvoicecron/observer::setStatus</model>
                </run>
            </roomstoryinvoicecron>
        </jobs>
    </crontab>
</config>

app/code/local/Roomstory/Invoice/Model/Observer.php 中添加以下内容:

In app/code/local/Roomstory/Invoice/Model/Observer.php add the following:

<?php
class Roomstory_Invoice_Model_Observer {
    public function setStatus() {
        Mage::log("WORKS!");
    }
}

确保日志记录已启用并且它应该可以工作,检查日志以确保;)

Make sure logging is enabled and it should work, check the log to be sure ;)

相关文章