From 4ebff2d94137ea821116c9defb872c1f606e3f34 Mon Sep 17 00:00:00 2001 From: Vladyslav Hrytsenko Date: Mon, 29 Jan 2024 08:33:59 +0200 Subject: [PATCH] added name to worker and api clients --- packages/nexrender-api/src/index.js | 10 +++++++++- packages/nexrender-core/src/index.js | 1 + packages/nexrender-worker/src/bin.js | 7 +++++++ packages/nexrender-worker/src/index.js | 6 +++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/nexrender-api/src/index.js b/packages/nexrender-api/src/index.js index 9cb1e0a0..33992171 100644 --- a/packages/nexrender-api/src/index.js +++ b/packages/nexrender-api/src/index.js @@ -5,7 +5,9 @@ const fetchAgent = typeof process == 'undefined' ? null : { // eslint-disable-li https: require('https').Agent, } -const createClient = ({ host, secret, polling, headers }) => { +const pkg = require('../package.json') + +const createClient = ({ host, secret, polling, headers, name }) => { const wrappedFetch = async (path, options) => { options = options || {} const defaultHeaders = {}; @@ -26,6 +28,12 @@ const createClient = ({ host, secret, polling, headers }) => { options.headers['nexrender-secret'] = secret } + if (name) { + options.headers['nexrender-name'] = name + } + + options.headers['user-agent'] = ('nexrender-api/' + pkg.version + ' ' + (options.headers['user-agent'] || '')).trim() + if (typeof process != 'undefined') { // NOTE: keepalive is enabled by default in node-fetch, so we need to disable it because of a bug // related to an invalidated session by the client diff --git a/packages/nexrender-core/src/index.js b/packages/nexrender-core/src/index.js index d14974cc..d1c4aa4a 100644 --- a/packages/nexrender-core/src/index.js +++ b/packages/nexrender-core/src/index.js @@ -158,6 +158,7 @@ const render = (jobConfig, settings = {}) => { .then(job => state(job, settings, postrender, 'postrender')) .then(job => state(job, settings, cleanup, 'cleanup')) .catch(e => { + console.log('catching the error internally') state(job, settings, cleanup, 'cleanup'); throw e; }); diff --git a/packages/nexrender-worker/src/bin.js b/packages/nexrender-worker/src/bin.js index 4257dad9..694caa4b 100644 --- a/packages/nexrender-worker/src/bin.js +++ b/packages/nexrender-worker/src/bin.js @@ -14,6 +14,7 @@ const args = arg({ '--cleanup': Boolean, '--host': String, + '--name': String, '--secret': String, '--binary': String, @@ -49,6 +50,7 @@ const args = arg({ '-t': '--tag-selector', '-c': '--cleanup', '-h': '--help', + '-n': '--name', '-s': '--secret', '-b': '--binary', '-w': '--workpath', @@ -86,6 +88,10 @@ if (args['--help']) { specify which host {cyan nexrender-server} is running at, and where all api requests will be forwarded to + -n, --name {underline unique_worker_name} + specify which name the {cyan nexrender-worker} will have, + and how it will be identified in the {cyan nexrender-server} + -s, --secret {underline secret_string} specify a secret that will be required for every incoming http request to validate again @@ -199,6 +205,7 @@ if (settings.hasOwnProperty('ae-params')) { settings['aeParams'] = settings['ae-params'] } +opt('name', '--name'); opt('binary', '--binary'); opt('workpath', '--workpath'); opt('no-license', '--no-license'); diff --git a/packages/nexrender-worker/src/index.js b/packages/nexrender-worker/src/index.js index 5a340870..dccb6322 100644 --- a/packages/nexrender-worker/src/index.js +++ b/packages/nexrender-worker/src/index.js @@ -1,6 +1,7 @@ const { createClient } = require('@nexrender/api') const { init, render } = require('@nexrender/core') const { getRenderingStatus } = require('@nexrender/types/job') +const pkg = require('../package.json') const NEXRENDER_API_POLLING = process.env.NEXRENDER_API_POLLING || 30 * 1000; const NEXRENDER_TOLERATE_EMPTY_QUEUES = process.env.NEXRENDER_TOLERATE_EMPTY_QUEUES; @@ -71,7 +72,10 @@ const start = async (host, secret, settings, headers) => { settings.tolerateEmptyQueues = NEXRENDER_TOLERATE_EMPTY_QUEUES; } - const client = createClient({ host, secret, headers }); + headers = headers || {}; + headers['user-agent'] = ('nexrender-worker/' + pkg.version + ' ' + (headers['user-agent'] || '')).trim(); + + const client = createClient({ host, secret, headers, name: settings.name }); settings.track('Worker Started', { worker_tags_set: !!settings.tagSelector,