Skip to content

Syntax error on startup and regular job polling in multitenant application #454

@neilprince

Description

@neilprince

I am experiencing an error when the CAP service starts and on the regular polling defined with parameter runInterval. The call stack of the syntax error is:

[mtx] - Failed to parse metadata to JSON {
  error: SyntaxError: "undefined" is not valid JSON
      at JSON.parse (<anonymous>)
      at SaasProvisioningService._toMetadataJson (C:\wa-cap-js\myEventQueue\node_modules\@sap\cds-mtxs\srv\cf\abstract-provisioning-service.js:93:19)
      at C:\wa-cap-js\myEventQueue\node_modules\@sap\cds-mtxs\srv\cf\abstract-provisioning-service.js:112:92
      at Array.map (<anonymous>)
      at SaasProvisioningService._read (C:\wa-cap-js\myEventQueue\node_modules\@sap\cds-mtxs\srv\cf\abstract-provisioning-service.js:112:8)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async next (C:\wa-cap-js\myEventQueue\node_modules\@sap\cds\lib\srv\srv-dispatch.js:57:17)
      at async SaasProvisioningService.handle (C:\wa-cap-js\myEventQueue\node_modules\@sap\cds\lib\srv\srv-dispatch.js:55:10)
      at async _getAllTenantBase (C:\wa-cap-js\myEventQueue\node_modules\@cap-js-community\event-queue\src\shared\cdsHelper.js:137:10)
      at async Object.getAllTenantIds (C:\wa-cap-js\myEventQueue\node_modules\@cap-js-community\event-queue\src\shared\cdsHelper.js:141:20),
  metadata: undefined
}
[/eventQueue/runner] - updating of periodic events is disabled or no periodic events configured { updateEnabled: true, events: 0 }

event-queue is executing function _getAllTenantBase which connects to the cds.xt.SaasProvisioningService\tenant endpoint to read tenant details

const _getAllTenantBase = async () => {
  if (!config.isMultiTenancy) {
    return null;
  }

  // NOTE: tmp workaround until cds-mtxs fixes the connect.to service
  for (let i = 0; i < 10; i++) {
    if (cds.services["cds.xt.SaasProvisioningService"] || cds.services["saas-registry"]) {
      break;
    }
    await new Promise((resolve) => setTimeout(resolve, 1000).unref());
  }

  const ssp = await cds.connect.to("cds.xt.SaasProvisioningService");
  return await ssp.get("/tenant");
};

The error is then thrown from @sap\cds-mtxs\srv\cf\abstract-provisioning-service.js in function _read on this code:

    return (await cds.tx({ tenant: t0 }, tx =>
      tx.run(SELECT.from(Tenants).columns(['ID', 'metadata', 'createdAt', 'modifiedAt']))
    )).map(({ ID, metadata, createdAt, modifiedAt }) => ({ subscribedTenantId: ID, ...this._toMetadataJson(metadata, ID), createdAt, modifiedAt }))

The issue only occurs on HANA. On sqlite, the value in metadata is null so the json parsing does not error. On HANA, the SELECT.from(Tenants) returns uppercase field names for metadata, createdAt and modifiedAt. This is resulting with an undefined value being passed to the JSON parsing function which is resulting in the error. If I change all the field names to uppercase in this function, no syntax error occurs.

I have logged a ticket with SAP on this, but as this is being called through event-queue, they have redirected me to raise an issue here (even though I think it is an issue with mtxs)

I have a private repository that can replicate the error if this is of help.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions