我需要检测操作:何时节点将由CRON在Drupal 8中发布。
例如,:用户在Drupal中创建新文章,并在第二天设置调度选项(发布),当Drupal将发布文章时,我必须检测到此事件并将请求发送到另一台服务器。
hook_cron -帮不上忙。
hook_entity_update -在这个钩子中,我无法检测到CRON的更新。在这种情况下,我的意思是:如何检测谁更新节点CRON或用户?
我只需要通过CRON.找到节点的更新。
也许Drupal 8还有其他用于检测的动作或属性:通过CRON更新节点。
发布于 2020-08-17 12:59:11
我有一个类似的问题,在核心没有直接的解决方案,正如我所看到的。有一个信号量表,在该表中,cron在记录启动和结束时添加和删除记录,但这不能说明哪个线程/会话是cron运行。下面的会话测试解决了我的问题:
/**
* Tries to determinate if current session is a cron run.
*
* @return bool
* Returns true, the session belongs to a cron run, otherwise false.
*/
function _MODULE_is_cron_run(): bool {
$route = \Drupal::routeMatch();
// Check if the cron is executed from the interface OR
// if it was executed from the CLI with drush cron.
return (
$route->getRouteName() === 'system.run_cron' ||
PHP_SAPI === 'cli' && function_exists('drush_main') && in_array('cron', $_SERVER['argv'])
);
}我认为其余的都是直截了当的,你可以随时调用这个方法,它会告诉你它是否是cron。当然,如果您有另一个调用Drupal的解决方案,那么您可能需要对此进行调整以满足您的需求。
如果还需要签入批处理,则需要以下内容:
/**
* Tries to determinate if current session is a cron run.
*
* @return bool
* Returns true, the session belongs to a cron run, otherwise false.
*/
function _MODULE_is_cron_run(): bool {
$route = \Drupal::routeMatch();
// Check if the cron is executed from the interface OR
// if it was executed from the CLI with drush cron.
return (
$route->getRouteName() === 'system.run_cron' ||
PHP_SAPI === 'cli' && function_exists('drush_main') && (in_array('cron', $_SERVER['argv']) || in_array('batch-process', $_SERVER['argv']))
);
}https://stackoverflow.com/questions/49426642
复制相似问题