diff --git a/.claude/worktrees/bit3884-metadata-editing b/.claude/worktrees/bit3884-metadata-editing new file mode 160000 index 0000000..67563bb --- /dev/null +++ b/.claude/worktrees/bit3884-metadata-editing @@ -0,0 +1 @@ +Subproject commit 67563bb6d0ad2b83012b8e666b52acee68df57b1 diff --git a/.env.example b/.env.example index 0123ec0..c6435bd 100644 --- a/.env.example +++ b/.env.example @@ -15,28 +15,32 @@ WP_USER= WP_PASSWORD= # rest (default) | xmlrpc — per-site override: wp_transport in config/sites/*.json WP_TRANSPORT=rest -# Retries for WP REST GET/POST (publish, tags, wp:test users/me) and XML-RPC probe posts -WP_HTTP_MAX_ATTEMPTS=3 -WP_RETRY_BASE_MS=500 # Social / X (Twitter) listening (optional) TWITTER_BEARER_TOKEN= -# Retries for X API v2 recent search (429 / 5xx / transient network) -X_HTTP_MAX_ATTEMPTS=3 -X_RETRY_BASE_MS=1000 # Telegram listening (optional) TELEGRAM_BOT_TOKEN= +# HTTP Retry Configuration (consolidated across all HTTP clients) +# Unified vars (recommended — set once for all clients): +HTTP_MAX_ATTEMPTS=3 # Max retry attempts (1–10, default: 3) +HTTP_RETRY_BASE_MS=500 # Base exponential backoff delay in ms (100–120000, default: 500) +HTTP_RETRY_MAX_MS=60000 # Max delay cap for exponential backoff (100–600000, default: 60000) + +# Legacy component-specific vars (deprecated — for backward compatibility only): +# Priority: HTTP_* > component-specific vars > built-in defaults +# ⚠️ Deprecation notice: Use HTTP_* vars above instead. Component-specific vars will be removed in v1.0. +# - RSS_HTTP_MAX_ATTEMPTS, RSS_RETRY_BASE_MS (RSS crawler) +# - X_HTTP_MAX_ATTEMPTS, X_RETRY_BASE_MS (X/Twitter API client) +# - WP_HTTP_MAX_ATTEMPTS, WP_RETRY_BASE_MS (WordPress REST + XML-RPC publisher) +# - SCRAPER_HTTP_MAX_ATTEMPTS, SCRAPER_RETRY_BASE_MS (article scraper) + # HTTP client tuning (RSS + article scraper) HTTP_USER_AGENT=ai-news-writer/0.1 (+https://bitreport.pt) RSS_TIMEOUT_MS=10000 -RSS_HTTP_MAX_ATTEMPTS=3 -RSS_RETRY_BASE_MS=500 RSS_FETCH_CONCURRENCY=3 SCRAPER_TIMEOUT_MS=12000 -SCRAPER_HTTP_MAX_ATTEMPTS=3 -SCRAPER_RETRY_BASE_MS=500 # App — HTTP status (listen/auto mode only; bind all interfaces for NPM → LXC) HOST=0.0.0.0 diff --git a/README.md b/README.md index 0dafd37..b1280d5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ node src/index.js write "FC Porto match result" | `npm run verify:deploy -- ` | Smoke-test `GET /health` (expects `listen` mode); needs `curl` + `node` | | `npm run wp:test` | **REST:** `GET /wp-json/wp/v2/users/me`. **XML-RPC:** `wp.getUsersBlogs` when `wp_transport` or `WP_TRANSPORT` is `xmlrpc`. Same site selection as before. | | `npm run wp:probe-xmlrpc` | POST `xmlrpc.php` with `system.listMethods` — confirms XML-RPC is enabled (no credentials); same site selection as `wp:test` for `wp_url` | -| `npm run rss:test` | Probe each RSS URL in `config/sources.json` (transient HTTP retries via `RSS_HTTP_MAX_ATTEMPTS` / `RSS_RETRY_BASE_MS`); stdout `{ "ok", "feeds" }`; exit 1 if any feed fails | +| `npm run rss:test` | Probe each RSS URL in `config/sources.json` (transient HTTP retries via `HTTP_MAX_ATTEMPTS` / `HTTP_RETRY_BASE_MS`, or legacy `RSS_*` vars); stdout `{ "ok", "feeds" }`; exit 1 if any feed fails | | `npm run x:test` | X API v2 **recent search** for each `x.queries[]` in `config/social.json`; needs `TWITTER_BEARER_TOKEN` | | `npm run ops:check` | **`npm test`** first, then **`rss:test` always**; **`wp:test` only** if `config/sites/*.json` (≠ example) or `WP_URL`+`WP_USER`+`WP_PASSWORD` | | `npm start` | Same as `write` | @@ -91,7 +91,7 @@ RSS Feeds / News Sites detectTopics() → generateArticle() → publishToWordPress() ``` -**X (Twitter) — optional:** copy `config/social.example.json` → `config/social.json`, set `TWITTER_BEARER_TOKEN` in `.env`. In `listen` mode, tweets are merged with RSS before keyword filtering. Recent-search HTTP uses `axiosGetWithRetry` (429 / 502–504 / transient network); tune with `X_HTTP_MAX_ATTEMPTS` and `X_RETRY_BASE_MS` (see `.env.example`). Uses [X API v2 recent search](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent) (plan limits apply). +**X (Twitter) — optional:** copy `config/social.example.json` → `config/social.json`, set `TWITTER_BEARER_TOKEN` in `.env`. In `listen` mode, tweets are merged with RSS before keyword filtering. Recent-search HTTP uses `axiosGetWithRetry` (429 / 502–504 / transient network); tune with `HTTP_MAX_ATTEMPTS` and `HTTP_RETRY_BASE_MS` (unified across all clients), or legacy `X_HTTP_MAX_ATTEMPTS` and `X_RETRY_BASE_MS` (see `.env.example`). Uses [X API v2 recent search](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent) (plan limits apply). ## Configuration @@ -133,8 +133,11 @@ Credentials come from your vault — never commit real passwords. | `WP_URL` | WordPress base URL (fallback) | | `WP_USER` | WP application username | | `WP_PASSWORD` | WP application password | -| `WP_HTTP_MAX_ATTEMPTS` | Retries for WP REST calls (default 3, max 5) | -| `WP_RETRY_BASE_MS` | First backoff delay in ms for WP retries (default 500) | +| `HTTP_MAX_ATTEMPTS` | **Unified retry attempts (1–10, default 3).** Used by RSS crawler, X API, WordPress, and article scraper. Takes precedence over component-specific vars. | +| `HTTP_RETRY_BASE_MS` | **Unified exponential backoff base in ms (100–120000, default 500)** | +| `HTTP_RETRY_MAX_MS` | **Unified max delay cap for backoff (100–600000, default 60000)** | +| `WP_HTTP_MAX_ATTEMPTS` | ⚠️ *Deprecated.* Use `HTTP_MAX_ATTEMPTS` instead. Kept for backward compatibility. | +| `WP_RETRY_BASE_MS` | ⚠️ *Deprecated.* Use `HTTP_RETRY_BASE_MS` instead. Kept for backward compatibility. | | `MODE` | `auto` or `manual` | | `AUTO_CRON` | Cron expression for auto mode | | `LOG_LEVEL` | `debug`, `info`, `warn`, `error` | @@ -142,8 +145,6 @@ Credentials come from your vault — never commit real passwords. | `RSS_TIMEOUT_MS` | Per-feed HTTP timeout for `rss-parser` (default 10000, max 120000) | | `RSS_FETCH_CONCURRENCY` | Parallel RSS fetches in `fetchAllFeeds` (default 3, max 20) | | `SCRAPER_TIMEOUT_MS` | Article scrape timeout (default 12000, max 120000) | -| `SCRAPER_HTTP_MAX_ATTEMPTS` | Retries per scrape GET (default 3, max 5) | -| `SCRAPER_RETRY_BASE_MS` | First backoff for scrape retries (default 500) | | `TWITTER_BEARER_TOKEN` | X API v2 bearer (optional; required when using `social.json` + listen mode) | | `PUBLISH_HOURS` | Optional editorial window for WordPress post status, e.g. `8-20` (hours 8–19 inclusive in `CRON_TIMEZONE`, half-open range `[start, end)`). When set, posts inside the window use `publish`; outside the window they are saved as `draft`. When unset, only per-site `status_default` / `wp_status` applies (unchanged behaviour). | | `CRON_TIMEZONE` | IANA zone used for the RSS cron schedule **and** for interpreting `PUBLISH_HOURS` (e.g. `Europe/Lisbon`). If invalid or empty, the host local timezone is used for the publish window. | diff --git a/src/util/http-retry-config.js b/src/util/http-retry-config.js index 62f92b3..4e14e67 100644 --- a/src/util/http-retry-config.js +++ b/src/util/http-retry-config.js @@ -5,6 +5,18 @@ * Consolidates retry logic from RSS, X, and WordPress clients into a single class. * Supports backward-compatible environment variables while encouraging migration to unified vars. */ + +/** + * Emit deprecation warning for legacy component-specific env vars. + * @private + */ +function deprecationWarningFor(component, legacyVar, newVar) { + if (typeof process !== 'undefined' && process.stderr) { + const msg = `⚠️ [HttpRetryConfig] Deprecated env var ${legacyVar} (${component}). Use ${newVar} instead.`; + console.warn(msg); + } +} + class HttpRetryConfig { /** * @param {Object} options @@ -70,12 +82,14 @@ class HttpRetryConfig { // Try legacy abbreviated name (RSS_, X_, WP_) let legacy = HttpRetryConfig._parseIntEnv(`${abbrev}_HTTP_MAX_ATTEMPTS`); if (legacy !== null) { + deprecationWarningFor(component, `${abbrev}_HTTP_MAX_ATTEMPTS`, 'HTTP_MAX_ATTEMPTS'); return Math.min(5, Math.max(1, legacy)); } // Try full component name as fallback legacy = HttpRetryConfig._parseIntEnv(`${component.toUpperCase()}_HTTP_MAX_ATTEMPTS`); if (legacy !== null) { + deprecationWarningFor(component, `${component.toUpperCase()}_HTTP_MAX_ATTEMPTS`, 'HTTP_MAX_ATTEMPTS'); return Math.min(5, Math.max(1, legacy)); } } @@ -105,12 +119,14 @@ class HttpRetryConfig { // Try legacy abbreviated name (RSS_, X_, WP_) let legacy = HttpRetryConfig._parseIntEnv(`${abbrev}_RETRY_BASE_MS`); if (legacy !== null) { + deprecationWarningFor(component, `${abbrev}_RETRY_BASE_MS`, 'HTTP_RETRY_BASE_MS'); return Math.min(60000, Math.max(200, legacy)); } // Try full component name as fallback legacy = HttpRetryConfig._parseIntEnv(`${component.toUpperCase()}_RETRY_BASE_MS`); if (legacy !== null) { + deprecationWarningFor(component, `${component.toUpperCase()}_RETRY_BASE_MS`, 'HTTP_RETRY_BASE_MS'); return Math.min(60000, Math.max(200, legacy)); } } @@ -196,6 +212,21 @@ class HttpRetryConfig { }; } + /** + * Get configuration as an object for observability/metrics. + * Useful for debugging and monitoring which env vars are in effect. + * @returns {Object} + */ + toObservability() { + return { + component: this.component, + maxAttempts: this.maxAttempts, + baseDelayMs: this.baseDelayMs, + maxDelayMs: this.maxDelayMs, + timeoutMs: this.timeoutMs + }; + } + /** * String representation for debugging. */ diff --git a/src/util/http-retry-config.test.js b/src/util/http-retry-config.test.js index 1f6a3bb..59f1a30 100644 --- a/src/util/http-retry-config.test.js +++ b/src/util/http-retry-config.test.js @@ -292,4 +292,33 @@ describe('HttpRetryConfig', () => { ); assert.equal(config.maxAttempts, 3); }); + + it('toObservability returns configuration object', () => { + const config = new HttpRetryConfig({ + maxAttempts: 5, + baseDelayMs: 1000, + maxDelayMs: 120000, + component: 'rss', + timeoutMs: 10000 + }); + + const obs = config.toObservability(); + assert.equal(obs.component, 'rss'); + assert.equal(obs.maxAttempts, 5); + assert.equal(obs.baseDelayMs, 1000); + assert.equal(obs.maxDelayMs, 120000); + assert.equal(obs.timeoutMs, 10000); + }); + + it('toObservability includes timeout when set', () => { + const config = HttpRetryConfig.forWordPress({ timeoutMs: 15000 }); + const obs = config.toObservability(); + assert.equal(obs.timeoutMs, 15000); + }); + + it('toObservability omits undefined timeout', () => { + const config = HttpRetryConfig.forX(); + const obs = config.toObservability(); + assert.equal(obs.timeoutMs, undefined); + }); });