drupal-mods/newrelic_cron 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drupal-mods/newrelic_cron

Composer 安装命令:

composer require drupal-mods/newrelic_cron

包简介

Instruments Drupal cron execution and sends results to the New Relic Event API for alerting on failures, slow runs, and missing runs.

README 文档

README

pipeline status PHPCS PHPStan PHPUnit

A Drupal 10.3+ / 11 module that instruments cron execution and sends results to the New Relic Event API for alerting on cron failures, slow runs, and missing runs.

Features

  • Drush command (bt:newrelic-cron / btnrc) wraps \Drupal::cron()->run() with precise timing via hrtime().
  • Connection testing — validate your API credentials before enabling with drush bt:newrelic-test or the admin UI "Test Connection" button.
  • New Relic Event API integration — sends custom events to US or EU data centers.
  • Acquia Cloud Platform auto-detection — reads AH_SITE_ENVIRONMENT and AH_SITE_NAME environment variables automatically.
  • Admin settings form at /admin/config/system/newrelic-cron for full GUI configuration.
  • Failure isolation — New Relic transport errors never affect the cron exit code.
  • Success event suppression — optionally send events only on failures to reduce event volume.

Requirements

  • Drupal 10.3+ or 11.x
  • Drush 12+ or 13
  • PHP 8.1+
  • A New Relic account with an Insert API key (NRII-...)

Installation

Via Composer (recommended)

composer require drupal-mods/newrelic_cron
drush en newrelic_cron

Manual

Place the module directory at web/modules/custom/newrelic_cron/ and enable:

drush en newrelic_cron

Configuration

Navigate to Administration > Configuration > System > New Relic Cron (/admin/config/system/newrelic-cron).

Required settings

SettingDescription
EnabledToggle event reporting on/off
Account IDYour New Relic account ID
Insert API keyA New Relic Insert API key (NRII-…)
RegionUS or EU data center

Optional settings

SettingDefaultDescription
Event typeCronJobExecutionCustom event type name in New Relic
Job namedrupal-cronJob identifier in events
Environment label(auto-detected)Fallback if AH_SITE_ENVIRONMENT not set
Site URISite URI included in events
Send success eventsYesWhether to send events for successful runs
Max message length2000Truncates error messages at this length
HTTP timeout5Timeout in seconds for the API call

Securing the Insert API key

For production, override the key in settings.php instead of storing it in config:

$config['newrelic_cron.settings']['insert_key'] = getenv('NEWRELIC_INSERT_KEY');

Usage

Drush command

# Run instrumented cron
drush bt:newrelic-cron

# Short alias
drush btnrc

The command:

  1. Runs Drupal cron and captures the result and any exceptions.
  2. Measures wall-clock duration using hrtime().
  3. Builds an event payload and stores last_run metadata in Drupal state.
  4. Sends the event to New Relic (failures always; successes based on config).
  5. Returns 0 on success, 1 on failure.

Testing the connection

Before enabling cron reporting, validate your configuration:

# Test New Relic API connection
drush bt:newrelic-test

# Short alias
drush btnrc:test

This sends a test event to New Relic and reports success or failure with actionable hints:

  • Connection successful! Test event sent to New Relic (HTTP 200).
  • Authentication failed. Check your Insert API key.
  • Account not found. Check your Account ID.

You can also test the connection from the admin UI using the Test Connection button at /admin/config/system/newrelic-cron.

Acquia Cloud cron job

/usr/local/bin/cron-wrapper.sh /var/www/html/mysite/vendor/bin/drush --root=/var/www/html/mysite/docroot -l https://mysite.com btnrc

Scheduler (non-Acquia)

*/15 * * * * cd /var/www/mysite && vendor/bin/drush btnrc 2>&1 | logger -t drupal-cron

Event payload

Each event sent to New Relic contains:

FieldTypeExample
eventTypestringCronJobExecution
jobNamestringdrupal-cron
statusstringsuccess or failed
timestampint1711929600
durationMsfloat4523.17
environmentstringprod
siteNamestringmysite
siteUristringhttps://mysite.com
exitCodeint0
messagestring(empty on success)

Example NRQL queries

Recent cron executions

SELECT * FROM CronJobExecution
  SINCE 1 day ago
  LIMIT 100

Failure count by environment

SELECT count(*) FROM CronJobExecution
  WHERE status = 'failed'
  FACET environment
  SINCE 7 days ago

Average duration over time

SELECT average(durationMs) FROM CronJobExecution
  TIMESERIES AUTO
  SINCE 24 hours ago

Slow cron runs (> 60 seconds)

SELECT timestamp, environment, siteName, durationMs, message
  FROM CronJobExecution
  WHERE durationMs > 60000
  SINCE 7 days ago

Missing cron alert (NRQL condition)

SELECT count(*) FROM CronJobExecution
  WHERE environment = 'prod'
  SINCE 30 minutes ago

Set the alert threshold to count < 1 to fire when no cron event arrives within the expected window.

Architecture

NewRelicCronCommands (Drush command)
  ├── CronInterface::run()       — executes Drupal cron
  ├── CronExecutionRecorder      — builds payload, records state
  └── NewRelicEventClient        — HTTP POST to New Relic Event API
  • NewRelicEventClient — Posts JSON to the NR Event API with the configured Insert key. Supports US and EU endpoints.
  • CronExecutionRecorder — Builds event payloads with environment/site metadata, stores last_run in Drupal state, controls success-event gating.
  • NewRelicCronCommands — Drush command that orchestrates cron execution, timing, and event dispatch.

Development

Install dev dependencies

composer install

Run tests

vendor/bin/phpunit

Static analysis

vendor/bin/phpstan analyse

Coding standards

vendor/bin/phpcs
vendor/bin/phpcbf  # auto-fix

Future enhancements

  • Per-hook instrumentation (report individual hook_cron implementations).
  • Queue worker monitoring.
  • Dashboard integration with pre-built New Relic dashboards.
  • Drush command to send a test event for validating configuration.

License

This project is licensed under the GPL-2.0-or-later license.

drupal-mods/newrelic_cron 适用场景与选型建议

drupal-mods/newrelic_cron 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cron」 「monitoring」 「drupal」 「Drush」 「new-relic」 「observability」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 drupal-mods/newrelic_cron 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drupal-mods/newrelic_cron 我们能提供哪些服务?
定制开发 / 二次开发

基于 drupal-mods/newrelic_cron 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-04-01