【发布时间】:2018-06-26 15:08:11
【问题描述】:
我有以下功能可以更新我的空缺状态(基于日期)并记录发生变化的空缺数量,如下所示:
//Function used to check the statusses of every vacancy and update it accordingly
function checkVacanciesStatusses()
{
// this function can only be called from the command line
if (!$this->input->is_cli_request()) {
echo "Access is only allowed from the command line";
return;
}
$this->load->model('vacancy/vacancies_model');
$this->load->library('managers/LogManager');
$affected0 = $this->vacancies_model->updateToStatus0();
$this->logmanager->createCronLog("updateToStatus0", $affected0);
$affected1 = $this->vacancies_model->updateToStatus1();
$this->logmanager->createCronLog("updateToStatus1", $affected1);
$affected2 = $this->vacancies_model->updateToStatus2();
$this->logmanager->createCronLog("updateToStatus2", $affected2);
}
这被放入一个 cronjob 中,每 30 分钟执行一次,如下所示:
*/30 * * * * /usr/bin/php /var/www/index.php cli/cron checkVacanciesStatusses >/dev/null 2>&1
我的日志管理器中 createCronLog 的代码如下所示:
class LogManager {
protected $CI;
// We'll use a constructor, as you can't directly call a function
// from a property definition.
public function __construct()
{
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
$this->CI->load->helper('url');
$this->CI->load->model('logging/logCron_model');
}
/**
* Creates an entry in the log_cron table
*
* @param string $method the name of the method executed
* @param array $amount_updated the amount of rows updated during the method the cron logged for
*
* @return int row id
*/
public function createCronLog($method, $amount_updated)
{
//Create a LOG entry
$entity = new stdClass();
$entity->created_at = date('Y-m-d H:i:s');
$entity->hostname = gethostname();
$entity->pid = getmypid();
$entity->user = get_current_user();
$entity->uri = $_SERVER['REQUEST_URI'];
$entity->method = $method;
$entity->amount_updated = $amount_updated;
return $this->CI->logCron_model->add($entity);
}
}
我无法理解的是,空缺更新(因此模型调用)完美运行。但是日志记录不起作用。
当我评论检查它是否是 cli_request 的 if 子句并手动浏览到该函数时,空缺得到更新并且日志记录有效!
发生了什么? 它不会给出任何错误或任何错误,它只会在手动执行时起作用。
【问题讨论】:
-
检查我的答案,也许他们有帮助:stackoverflow.com/a/46910528/2275490 和 stackoverflow.com/a/36190095/2275490
-
@Vickel 感谢您的帖子!我之前检查了线程。我知道数据库访问工作正常,因为其他模型调用工作正常。我也使用相同的 if 检查,但我使用 is_cli_request() 而不是 is_cli() ,我对两者都进行了测试,结果是一样的。
标签: php codeigniter cron command-line-interface