首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Drupal 8中由Cron发布的实体何时检测到事件?

在Drupal 8中由Cron发布的实体何时检测到事件?
EN

Stack Overflow用户
提问于 2018-03-22 10:45:34
回答 1查看 389关注 0票数 0

我需要检测操作:何时节点将由CRONDrupal 8中发布。

调度选项

例如,用户在Drupal中创建新文章,并在第二天设置调度选项(发布),当Drupal将发布文章时,我必须检测到此事件并将请求发送到另一台服务器。

hook_cron -帮不上忙。

hook_entity_update -在这个钩子中,我无法检测到CRON的更新。在这种情况下,我的意思是:如何检测谁更新节点CRON或用户?

我只需要通过CRON.找到节点的更新。

也许Drupal 8还有其他用于检测的动作或属性:通过CRON更新节点。

EN

回答 1

Stack Overflow用户

发布于 2020-08-17 12:59:11

我有一个类似的问题,在核心没有直接的解决方案,正如我所看到的。有一个信号量表,在该表中,cron在记录启动和结束时添加和删除记录,但这不能说明哪个线程/会话是cron运行。下面的会话测试解决了我的问题:

代码语言:javascript
运行
复制
/**
 * 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的解决方案,那么您可能需要对此进行调整以满足您的需求。

如果还需要签入批处理,则需要以下内容:

代码语言:javascript
运行
复制
/**
 * 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']))
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49426642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档