@shushed/helpers 0.0.58 → 0.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { JSONSchemaType } from 'ajv';
2
2
  import { DeepRedact } from '@hackylabs/deep-redact';
3
3
  import { Firestore, DocumentReference } from '@google-cloud/firestore';
4
+ import { Logging as Logging$1 } from '@google-cloud/logging';
4
5
  import { PubSub, Topic, Subscription } from '@google-cloud/pubsub';
5
6
  import { Context } from 'co-body';
6
7
  import * as _google_cloud_scheduler_build_protos_protos from '@google-cloud/scheduler/build/protos/protos';
@@ -28763,22 +28764,41 @@ declare function validate<T>(payload: T, schema: JSONSchemaType<T>, options: {
28763
28764
  declare const sanitizeToString: DeepRedact;
28764
28765
  declare const sanitize: DeepRedact;
28765
28766
 
28767
+ interface ILogging {
28768
+ log(...args: any[]): void;
28769
+ error(...args: any[]): void;
28770
+ }
28766
28771
  type Opts = {
28767
28772
  workflowId: string;
28768
28773
  triggerId: string;
28769
28774
  url: string;
28770
28775
  envName: string;
28776
+ logging?: ILogging;
28771
28777
  } | {
28772
28778
  workflowId: string;
28773
28779
  triggerId: string;
28774
28780
  url: string;
28775
- };
28781
+ logging?: ILogging;
28782
+ } | Runtime;
28783
+ declare class Logging implements ILogging {
28784
+ private workflowId;
28785
+ private triggerId;
28786
+ private systemEnvName;
28787
+ gcpLogging: Logging$1;
28788
+ metadata: any;
28789
+ logFn: (...args: any[]) => void;
28790
+ errorFn: (...args: any[]) => void;
28791
+ constructor(workflowId: string, triggerId: string, systemEnvName: string);
28792
+ log(...args: any[]): void;
28793
+ error(...args: any[]): void;
28794
+ }
28776
28795
  declare class Runtime {
28777
28796
  readonly workflowId: string;
28778
28797
  readonly triggerId: string;
28779
28798
  readonly envName: string;
28780
28799
  readonly systemEnvName: string;
28781
28800
  readonly runtimeUrl: string;
28801
+ readonly logging: ILogging;
28782
28802
  constructor(opts: Opts);
28783
28803
  }
28784
28804
 
@@ -28879,7 +28899,6 @@ declare class PubSubHelper extends Runtime {
28879
28899
  private getNameFromFullyQualifiedName;
28880
28900
  private updateSubscription;
28881
28901
  private ensureSubscribtionExists;
28882
- private ensureDLQSubscriptionExists;
28883
28902
  deleteSubscriptions(topicName: string): Promise<{
28884
28903
  mainTopic: Topic | undefined;
28885
28904
  mainDlqTopic: Topic | undefined;
@@ -29229,8 +29248,11 @@ type index_BigQueryHelper = BigQueryHelper;
29229
29248
  declare const index_BigQueryHelper: typeof BigQueryHelper;
29230
29249
  type index_EnvEngine = EnvEngine;
29231
29250
  declare const index_EnvEngine: typeof EnvEngine;
29251
+ type index_ILogging = ILogging;
29232
29252
  type index_JWKSHelper = JWKSHelper;
29233
29253
  declare const index_JWKSHelper: typeof JWKSHelper;
29254
+ type index_Logging = Logging;
29255
+ declare const index_Logging: typeof Logging;
29234
29256
  type index_Message = Message;
29235
29257
  type index_NodeOptions = NodeOptions;
29236
29258
  type index_PubSubHelper = PubSubHelper;
@@ -29253,7 +29275,7 @@ declare const index_shortHash: typeof shortHash;
29253
29275
  declare const index_validate: typeof validate;
29254
29276
  declare const index_validateGoogleAuth: typeof validateGoogleAuth;
29255
29277
  declare namespace index {
29256
- export { index_BigQueryHelper as BigQueryHelper, index_EnvEngine as EnvEngine, index_JWKSHelper as JWKSHelper, type index_Message as Message, type index_NodeOptions as NodeOptions, index_PubSubHelper as PubSubHelper, index_Runtime as Runtime, index_SchedulerHelper as SchedulerHelper, index_Secrets as Secrets, type index_TriggerOnCreateOptions as TriggerOnCreateOptions, type index_TriggerOnExecuteOptions as TriggerOnExecuteOptions, index_getEventTime as getEventTime, index_isCronMessage as isCronMessage, index_isPubSubRequest as isPubSubRequest, index_parseDateOrDefault as parseDateOrDefault, index_sanitize as sanitize, index_sanitizeToString as sanitizeToString, index_shortHash as shortHash, index_validate as validate, index_validateGoogleAuth as validateGoogleAuth };
29278
+ export { index_BigQueryHelper as BigQueryHelper, index_EnvEngine as EnvEngine, type index_ILogging as ILogging, index_JWKSHelper as JWKSHelper, index_Logging as Logging, type index_Message as Message, type index_NodeOptions as NodeOptions, index_PubSubHelper as PubSubHelper, index_Runtime as Runtime, index_SchedulerHelper as SchedulerHelper, index_Secrets as Secrets, type index_TriggerOnCreateOptions as TriggerOnCreateOptions, type index_TriggerOnExecuteOptions as TriggerOnExecuteOptions, index_getEventTime as getEventTime, index_isCronMessage as isCronMessage, index_isPubSubRequest as isPubSubRequest, index_parseDateOrDefault as parseDateOrDefault, index_sanitize as sanitize, index_sanitizeToString as sanitizeToString, index_shortHash as shortHash, index_validate as validate, index_validateGoogleAuth as validateGoogleAuth };
29257
29279
  }
29258
29280
 
29259
29281
  export { index as lib, index$9 as schema, index$1 as types };
package/dist/index.js CHANGED
@@ -288,6 +288,7 @@ __export(src_public_exports, {
288
288
  BigQueryHelper: () => bigquery_default,
289
289
  EnvEngine: () => env_default,
290
290
  JWKSHelper: () => jwks_default,
291
+ Logging: () => Logging,
291
292
  PubSubHelper: () => pubsub_default,
292
293
  Runtime: () => Runtime,
293
294
  SchedulerHelper: () => scheduler_default,
@@ -362,26 +363,90 @@ var import_crypto = __toESM(require("crypto"));
362
363
  var import_jose = require("jose");
363
364
 
364
365
  // src-public/runtime.ts
366
+ var import_logging = require("@google-cloud/logging");
365
367
  var DEFAULT_BRANCH = "migratedprod";
366
- var Runtime = class {
368
+ var Logging = class {
369
+ constructor(workflowId, triggerId, systemEnvName) {
370
+ this.workflowId = workflowId;
371
+ this.triggerId = triggerId;
372
+ this.systemEnvName = systemEnvName;
373
+ this.gcpLogging = new import_logging.Logging({ projectId: process.env.GCLOUD_PROJECT });
374
+ this.metadata = {
375
+ resource: { type: "cloud_run_revision" },
376
+ labels: {
377
+ project_id: process.env.GCLOUD_PROJECT
378
+ }
379
+ };
380
+ if (process.env.GCLOUD_PROJECT && process.env.RUNTIME_URL) {
381
+ const log = this.gcpLogging.log(`projects/${process.env.GCLOUD_PROJECT}/logs/rowy-logging`, {
382
+ removeCircular: true
383
+ });
384
+ this.logFn = (...args) => log.entry(Object.assign({
385
+ severity: import_logging.Severity.info
386
+ }, this.metadata), ...args);
387
+ this.errorFn = (...args) => log.entry(Object.assign({
388
+ severity: import_logging.Severity.error
389
+ }, this.metadata), ...args);
390
+ } else {
391
+ this.logFn = console.log;
392
+ this.errorFn = console.error;
393
+ }
394
+ }
395
+ gcpLogging;
396
+ metadata;
397
+ logFn;
398
+ errorFn;
399
+ log(...args) {
400
+ this.logFn({
401
+ payload: args,
402
+ workflowId: this.workflowId,
403
+ wId: this.workflowId,
404
+ triggerId: this.triggerId,
405
+ tId: this.triggerId,
406
+ env: this.systemEnvName
407
+ });
408
+ }
409
+ error(...args) {
410
+ this.errorFn({
411
+ payload: args,
412
+ workflowId: this.workflowId,
413
+ wId: this.workflowId,
414
+ triggerId: this.triggerId,
415
+ tId: this.triggerId,
416
+ env: this.systemEnvName
417
+ });
418
+ }
419
+ };
420
+ var Runtime = class _Runtime {
367
421
  workflowId;
368
422
  triggerId;
369
423
  envName;
370
424
  systemEnvName;
371
425
  runtimeUrl;
426
+ logging;
372
427
  constructor(opts) {
373
- this.workflowId = opts.workflowId;
374
- this.triggerId = opts.triggerId;
375
- this.systemEnvName = typeof opts.envName !== "undefined" ? opts.envName : (() => {
376
- const hostId = opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0].split(".")[0].toLowerCase();
377
- let branch = DEFAULT_BRANCH;
378
- if (hostId.includes("-")) {
379
- branch = hostId.split("-")[0];
380
- }
381
- return branch;
382
- })();
383
- this.runtimeUrl = "https://" + new URL("https://" + opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0]).host;
384
- this.envName = this.systemEnvName === DEFAULT_BRANCH ? "prod" : this.systemEnvName;
428
+ if (opts instanceof _Runtime) {
429
+ this.workflowId = opts.workflowId;
430
+ this.triggerId = opts.triggerId;
431
+ this.systemEnvName = opts.systemEnvName;
432
+ this.runtimeUrl = opts.runtimeUrl;
433
+ this.envName = opts.envName;
434
+ this.logging = opts.logging || new Logging(this.workflowId, this.triggerId, this.systemEnvName);
435
+ } else {
436
+ this.workflowId = opts.workflowId;
437
+ this.triggerId = opts.triggerId;
438
+ this.systemEnvName = typeof opts.envName !== "undefined" ? opts.envName : (() => {
439
+ const hostId = opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0].split(".")[0].toLowerCase();
440
+ let branch = DEFAULT_BRANCH;
441
+ if (hostId.includes("-")) {
442
+ branch = hostId.split("-")[0];
443
+ }
444
+ return branch;
445
+ })();
446
+ this.runtimeUrl = "https://" + new URL("https://" + opts.url.replace(/^(?:https?:)?\/\//i, "").split("/")[0]).host;
447
+ this.envName = this.systemEnvName === DEFAULT_BRANCH ? "prod" : this.systemEnvName;
448
+ this.logging = opts.logging || new Logging(this.workflowId, this.triggerId, this.systemEnvName);
449
+ }
385
450
  }
386
451
  };
387
452
 
@@ -567,13 +632,6 @@ var EnvEngine = class extends Runtime {
567
632
  };
568
633
  }
569
634
  async set(envs, level = "trigger", encryptWithValue) {
570
- const timeout = 300;
571
- await Promise.race([this.initializeIfNeeded(level), new Promise((resolve, reject) => setTimeout(() => {
572
- reject(new Error(`Waitied ${timeout}ms. Terminating firestore initialize during the set function`));
573
- }, timeout))]).catch((err) => {
574
- console.warn("Invariant: While initializing the env engine during set - " + err.message + ` - ${this.envName} - ${this.workflowId} - ${this.triggerId}.
575
- Failing quietly as the set might proceed, but this is not a good thing.`);
576
- });
577
635
  let aes = null;
578
636
  if (encryptWithValue) {
579
637
  aes = new AES256GCM(Buffer.from(encryptWithValue, "utf8"));
@@ -598,7 +656,6 @@ var EnvEngine = class extends Runtime {
598
656
  throw new Error(`Failed to set the ${envs.map((x) => x.name).join(", ")}. Error: ${err.message}`);
599
657
  }
600
658
  }
601
- Object.assign(this.store[level], obj);
602
659
  return void 0;
603
660
  }
604
661
  async initializeIfNeeded(level = "trigger") {
@@ -791,7 +848,9 @@ var PubSubHelper = class extends Runtime {
791
848
  return Promise.all(promises);
792
849
  }
793
850
  async createOrUpdate(opts) {
851
+ this.logging.log("createOrUpdate called with", opts);
794
852
  const [topics] = await this.pubSub.getTopics();
853
+ this.logging.log("topics discovered", topics);
795
854
  const infraPrefix = opts.topicName.indexOf("---") !== -1 ? opts.topicName : `${this.envName}---${opts.topicName}`;
796
855
  const mainDlqTopicName = `${infraPrefix}-${shortHash(this.triggerId)}-dlq`;
797
856
  const tableDlqTopicName = `${infraPrefix}-${shortHash(this.triggerId)}-bq-dlq`;
@@ -818,11 +877,28 @@ var PubSubHelper = class extends Runtime {
818
877
  let mainDlqSubscription = mainDlqTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainDlqSubscriptionName) || null;
819
878
  let tableSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === tableSubscriptionName) || null;
820
879
  let tableDlqSubscription = tableDlqTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === tableDlqSubscriptionName) || null;
880
+ this.logging.log("environment discovery", {
881
+ mainTopicName: opts.topicName,
882
+ mainDlqTopicName,
883
+ tableDlqTopicName,
884
+ mainSubscriptionName,
885
+ mainDlqSubscriptionName,
886
+ tableSubscriptionName,
887
+ tableDlqSubscriptionName,
888
+ mainTopicExists: mainTopic,
889
+ mainDlqTopicExists: mainDlqTopic,
890
+ tableDlqTopicExists: tableDlqTopic,
891
+ mainSubscriptionExists: mainSubscription,
892
+ mainDlqSubscriptionExists: mainDlqSubscription,
893
+ tableSubscriptionExists: tableSubscription,
894
+ tableDlqSubscriptionExists: tableDlqSubscription
895
+ });
821
896
  const topic = await this.ensureTopicExists(opts.topicName);
822
897
  if (opts.table || opts.pull || opts.push) {
823
898
  mainTopic = await this.ensureTopicExists(opts.topicName);
824
899
  }
825
900
  if (!opts.table) {
901
+ this.logging.log("createOrUpdate deleting table subscriptions");
826
902
  if (tableSubscription) {
827
903
  await tableSubscription.delete();
828
904
  tableSubscription = null;
@@ -832,12 +908,17 @@ var PubSubHelper = class extends Runtime {
832
908
  tableDlqSubscription = null;
833
909
  }
834
910
  } else {
911
+ this.logging.log("createOrUpdate creating table subscription");
835
912
  if (opts.createDeadLetterQueues) {
913
+ this.logging.log("createOrUpdate ensuring dlqs exist for table subscription");
836
914
  tableDlqTopic = await this.ensureTopicExists(tableDlqTopicName);
837
- tableDlqSubscription = await this.ensureDLQSubscriptionExists(tableDlqSubscriptionName, {
838
- topic: tableDlqTopic
915
+ tableDlqSubscription = await this.ensureSubscribtionExists(tableDlqSubscriptionName, {
916
+ topic: tableDlqTopic,
917
+ endpointUrl: null,
918
+ dlqTopic: null
839
919
  });
840
920
  }
921
+ this.logging.log("createOrUpdate ensuring the table subscription exists");
841
922
  tableSubscription = await this.ensureSubscribtionExists(tableSubscriptionName, {
842
923
  topic,
843
924
  tableId: opts.table.tableId,
@@ -850,6 +931,7 @@ var PubSubHelper = class extends Runtime {
850
931
  }
851
932
  const pullPushSubscription = opts.pull || opts.push;
852
933
  if (!pullPushSubscription) {
934
+ this.logging.log("createOrUpdate deleting main subscriptions");
853
935
  if (mainSubscription) {
854
936
  await mainSubscription.delete();
855
937
  mainSubscription = null;
@@ -860,22 +942,26 @@ var PubSubHelper = class extends Runtime {
860
942
  }
861
943
  } else {
862
944
  if (opts.createDeadLetterQueues) {
945
+ this.logging.log("createOrUpdate creating dlqs for main subscription");
863
946
  mainDlqTopic = await this.ensureTopicExists(mainDlqTopicName);
864
- mainDlqSubscription = await this.ensureDLQSubscriptionExists(mainDlqSubscriptionName, {
865
- topic: mainDlqTopic
866
- });
867
- mainSubscription = await this.ensureSubscribtionExists(mainSubscriptionName, {
868
- topic,
869
- dlqTopic: mainDlqTopic,
870
- endpointUrl: opts.push ? this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?topic_name=${opts.topicName}` : null,
871
- retryMinDelay: pullPushSubscription.retryMinDelay,
872
- retryMaxDelay: pullPushSubscription.retryMaxDelay,
873
- retryLimit: pullPushSubscription.retryLimit,
874
- ackDeadline: pullPushSubscription.ackDeadline
947
+ mainDlqSubscription = await this.ensureSubscribtionExists(mainDlqSubscriptionName, {
948
+ topic: mainDlqTopic,
949
+ dlqTopic: null,
950
+ endpointUrl: null
875
951
  });
876
952
  }
953
+ mainSubscription = await this.ensureSubscribtionExists(mainSubscriptionName, {
954
+ topic,
955
+ dlqTopic: mainDlqTopic || null,
956
+ endpointUrl: opts.push ? this.runtimeUrl + `/executeWorkflow/${this.workflowId}/${this.triggerId}?topic_name=${opts.topicName}` : null,
957
+ retryMinDelay: pullPushSubscription.retryMinDelay,
958
+ retryMaxDelay: pullPushSubscription.retryMaxDelay,
959
+ retryLimit: pullPushSubscription.retryLimit,
960
+ ackDeadline: pullPushSubscription.ackDeadline
961
+ });
877
962
  }
878
963
  if (!opts.createDeadLetterQueues) {
964
+ this.logging.log("createOrUpdate deleting all dlqs");
879
965
  if (mainDlqSubscription) {
880
966
  await mainDlqSubscription.delete();
881
967
  mainDlqSubscription = null;
@@ -905,11 +991,20 @@ var PubSubHelper = class extends Runtime {
905
991
  return topics.find((topic) => this.getNameFromFullyQualifiedName(topic.name) === topicName);
906
992
  }
907
993
  async ensureTopicExists(topicName) {
994
+ this.logging.log("ensureTopicExists called with", topicName);
908
995
  let topic = await this.findTopic(topicName);
996
+ this.logging.log(`ensureTopicExists topic ${!topic ? "does not " : ""}exists`, topic);
997
+ let topicMetadata;
998
+ try {
999
+ [topicMetadata] = await topic?.getMetadata() || [null];
1000
+ } catch (err) {
1001
+ throw new Error("Invariant: While getting topic metadata - " + err.message);
1002
+ }
1003
+ this.logging.log(`ensureTopicExists retrieved topic metadata`, topicMetadata);
909
1004
  const metadata = {
910
1005
  labels: {
911
1006
  "created-at": (/* @__PURE__ */ new Date()).getTime().toString(),
912
- "last-modified-at": (/* @__PURE__ */ new Date()).getTime().toString(),
1007
+ "last-modified-at": topicMetadata?.labels?.["last-modified-at"] || (/* @__PURE__ */ new Date()).getTime().toString(),
913
1008
  "created-by-bs-triggerid": this.triggerId.toLowerCase(),
914
1009
  "created-by-bs-workflowid": this.workflowId.toLowerCase(),
915
1010
  "bs-envname": this.systemEnvName.toLowerCase(),
@@ -917,21 +1012,25 @@ var PubSubHelper = class extends Runtime {
917
1012
  [`w-${this.workflowId}-${this.triggerId}`.toLowerCase()]: "true"
918
1013
  }
919
1014
  };
920
- let topicMetadata;
921
- try {
922
- [topicMetadata] = await topic?.getMetadata() || [null];
923
- } catch (err) {
924
- throw new Error("Invariant: While getting topic metadata - " + err.message);
925
- }
926
1015
  if (topic) {
927
1016
  const simMetadata = Object.assign({}, topicMetadata?.labels || {}, metadata.labels);
928
1017
  if (!(0, import_lodash.default)(topicMetadata?.labels, simMetadata)) {
929
1018
  simMetadata["last-modified-at"] = (/* @__PURE__ */ new Date()).getTime().toString();
930
- await topic.setMetadata({
931
- labels: simMetadata
932
- });
1019
+ this.logging.log(`ensureTopicExists topic metadata has changed updating with`, simMetadata);
1020
+ try {
1021
+ await topic.setMetadata({
1022
+ labels: simMetadata
1023
+ });
1024
+ } catch (err) {
1025
+ this.logging.error("ensureTopicExists received error while setting metadata", err.message, err.stack);
1026
+ throw err;
1027
+ }
933
1028
  }
934
1029
  } else {
1030
+ this.logging.log(`ensureTopicExists creating new topic`, {
1031
+ name: topicName,
1032
+ labels: metadata.labels
1033
+ });
935
1034
  await this.pubSub.createTopic({
936
1035
  name: topicName,
937
1036
  labels: metadata.labels
@@ -954,11 +1053,20 @@ var PubSubHelper = class extends Runtime {
954
1053
  retryMaxDelay: 360,
955
1054
  ackDeadline: 90
956
1055
  };
1056
+ this.logging.log("updateSubscription called with", subscriptionName, subscription, subscriptionMetaData, inputOpts);
957
1057
  const opts = Object.assign({}, defaultOptions, inputOpts);
1058
+ this.logging.log("updateSubscription final opts", opts);
958
1059
  let nextMetadata = null;
959
1060
  if (isSubscriptionOptionsPush(opts)) {
960
1061
  if (opts.endpointUrl !== subscriptionMetaData?.pushConfig?.pushEndpoint || this.serviceAccountEmail !== subscriptionMetaData?.pushConfig.oidcToken?.serviceAccountEmail || process.env.GCLOUD_PROJECT + "/" + this.triggerId !== subscriptionMetaData.pushConfig.oidcToken.audience) {
961
1062
  if (subscriptionMetaData?.pushConfig?.pushEndpoint && subscription) {
1063
+ this.logging.log("updateSubscription pushConfig changed, push configuration already exists, modifying", {
1064
+ pushEndpoint: opts.endpointUrl,
1065
+ oidcToken: {
1066
+ serviceAccountEmail: this.serviceAccountEmail,
1067
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1068
+ }
1069
+ });
962
1070
  try {
963
1071
  await subscription.modifyPushConfig({
964
1072
  pushEndpoint: opts.endpointUrl,
@@ -1048,10 +1156,11 @@ var PubSubHelper = class extends Runtime {
1048
1156
  nextMetadata.bigqueryConfig = null;
1049
1157
  }
1050
1158
  if (subscription && nextMetadata) {
1159
+ this.logging.log("updateSubscription updating subscription", nextMetadata);
1051
1160
  try {
1052
1161
  await subscription.setMetadata(nextMetadata);
1053
1162
  } catch (err) {
1054
- console.error("Invariant: While updating subscription - " + err.message + ` - ${this.pubSub.projectId} - ${subscriptionName} - ${JSON.stringify(nextMetadata)}`);
1163
+ this.logging.error("updateSubscription received error while setting metadata", err.message, err.stack);
1055
1164
  throw new Error("Invariant: While updating subscription - " + err.message);
1056
1165
  }
1057
1166
  }
@@ -1060,14 +1169,17 @@ var PubSubHelper = class extends Runtime {
1060
1169
  async ensureSubscribtionExists(subscriptionName, opts) {
1061
1170
  let [subscriptions] = await opts.topic.getSubscriptions();
1062
1171
  let subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName) || null;
1172
+ this.logging.log("ensureSubscribtionExists called with", subscriptionName, opts);
1063
1173
  let subscriptionMetaData;
1064
1174
  try {
1065
1175
  [subscriptionMetaData] = await subscription?.getMetadata() || [null];
1066
1176
  } catch (err) {
1067
1177
  throw new Error("Invariant: While getting subscription metadata - " + err.message);
1068
1178
  }
1179
+ this.logging.log("ensureSubscribtionExists retrieved subscriptionMetaData", subscriptionMetaData);
1069
1180
  const createPayload = await this.updateSubscription(subscriptionName, subscription, subscriptionMetaData, opts);
1070
- if (createPayload) {
1181
+ if (createPayload && !subscription) {
1182
+ this.logging.log(`ensureSubscribtionExists creating subscription: ${subscriptionName} on topic: ${opts.topic.name} with`, createPayload);
1071
1183
  await opts.topic.createSubscription(subscriptionName, createPayload);
1072
1184
  [subscriptions] = await opts.topic.getSubscriptions();
1073
1185
  subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName) || null;
@@ -1077,20 +1189,8 @@ var PubSubHelper = class extends Runtime {
1077
1189
  }
1078
1190
  return subscription;
1079
1191
  }
1080
- async ensureDLQSubscriptionExists(subscriptionName, opts) {
1081
- let [subscriptions] = await opts.topic.getSubscriptions();
1082
- let subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName);
1083
- if (!subscription) {
1084
- await opts.topic.createSubscription(subscriptionName);
1085
- [subscriptions] = await opts.topic.getSubscriptions();
1086
- subscription = subscriptions.find((subscription2) => this.getNameFromFullyQualifiedName(subscription2.name) === subscriptionName);
1087
- }
1088
- if (!subscription) {
1089
- throw new Error(`Invariant: While creating subscription - subscription is not found`);
1090
- }
1091
- return subscription;
1092
- }
1093
1192
  async deleteSubscriptions(topicName) {
1193
+ this.logging.log("deleteSubscriptions called with", topicName);
1094
1194
  return this.createOrUpdate({
1095
1195
  topicName,
1096
1196
  createDeadLetterQueues: false,
@@ -1102,14 +1202,18 @@ var PubSubHelper = class extends Runtime {
1102
1202
  async isRunning(topicName) {
1103
1203
  const [topics] = await this.pubSub.getTopics();
1104
1204
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1205
+ this.logging.log("isRunning called with", topicName);
1105
1206
  const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1106
1207
  let mainTopicSubscriptions = null;
1107
1208
  if (mainTopic) {
1108
1209
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1109
1210
  }
1110
1211
  const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1212
+ this.logging.log("isRunning checking the", mainSubscriptionName);
1111
1213
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1214
+ this.logging.log("isRunning found the", mainSubscription);
1112
1215
  const [metadata] = await mainSubscription?.getMetadata() || [null];
1216
+ this.logging.log("isRunning received metadata", metadata);
1113
1217
  return {
1114
1218
  subscriptionId: mainSubscription?.name,
1115
1219
  isRunning: metadata?.pushConfig?.pushEndpoint !== null
@@ -1118,17 +1222,25 @@ var PubSubHelper = class extends Runtime {
1118
1222
  async pause(topicName) {
1119
1223
  const [topics] = await this.pubSub.getTopics();
1120
1224
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1225
+ this.logging.log("paused called with", topicName);
1121
1226
  const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1122
1227
  let mainTopicSubscriptions = null;
1123
1228
  if (mainTopic) {
1124
1229
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1125
1230
  }
1126
1231
  const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1232
+ this.logging.log("isRunning checking the", mainSubscriptionName);
1127
1233
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1234
+ this.logging.log("pause found the", mainSubscription);
1128
1235
  if (mainSubscription) {
1129
- await mainSubscription.setMetadata({
1130
- pushConfig: null
1131
- });
1236
+ try {
1237
+ await mainSubscription.setMetadata({
1238
+ pushConfig: null
1239
+ });
1240
+ } catch (err) {
1241
+ this.logging.error("pause received error while setting metadata", err.message, err.stack);
1242
+ throw err;
1243
+ }
1132
1244
  return {
1133
1245
  subscriptionId: mainSubscription.name
1134
1246
  };
@@ -1140,23 +1252,40 @@ var PubSubHelper = class extends Runtime {
1140
1252
  async resume(topicName) {
1141
1253
  const [topics] = await this.pubSub.getTopics();
1142
1254
  const mainTopic = topics.find((x) => this.getNameFromFullyQualifiedName(x.name) === topicName);
1255
+ this.logging.log("resume called with", topicName);
1143
1256
  const infraPrefix = topicName.indexOf("---") !== -1 ? topicName : `${this.envName}---${topicName}`;
1144
1257
  let mainTopicSubscriptions = null;
1145
1258
  if (mainTopic) {
1146
1259
  [mainTopicSubscriptions] = await mainTopic.getSubscriptions();
1147
1260
  }
1148
1261
  const mainSubscriptionName = `${infraPrefix}-${shortHash(this.triggerId)}`;
1262
+ this.logging.log("resume checking the", mainSubscriptionName);
1149
1263
  const mainSubscription = mainTopicSubscriptions?.find((x) => this.getNameFromFullyQualifiedName(x.name) === mainSubscriptionName) || null;
1264
+ this.logging.log("resume found the", mainSubscription);
1150
1265
  if (mainSubscription) {
1151
- await mainSubscription.setMetadata({
1152
- pushConfig: {
1153
- pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1154
- oidcToken: {
1155
- serviceAccountEmail: this.serviceAccountEmail,
1156
- audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1266
+ try {
1267
+ this.logging.log("resume setting metadata", {
1268
+ pushConfig: {
1269
+ pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1270
+ oidcToken: {
1271
+ serviceAccountEmail: this.serviceAccountEmail,
1272
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1273
+ }
1157
1274
  }
1158
- }
1159
- });
1275
+ });
1276
+ await mainSubscription.setMetadata({
1277
+ pushConfig: {
1278
+ pushEndpoint: this.runtimeUrl + "/executeWorkflow/" + this.workflowId + "/" + this.triggerId + `?topic_name=${topicName}`,
1279
+ oidcToken: {
1280
+ serviceAccountEmail: this.serviceAccountEmail,
1281
+ audience: process.env.GCLOUD_PROJECT + "/" + this.triggerId
1282
+ }
1283
+ }
1284
+ });
1285
+ } catch (err) {
1286
+ this.logging.error("resume received error while setting metadata", err.message, err.stack);
1287
+ throw err;
1288
+ }
1160
1289
  return {
1161
1290
  subscriptionId: mainSubscription.name
1162
1291
  };
package/dist/package.json CHANGED
@@ -5,6 +5,7 @@
5
5
  "license": "UNLICENSED",
6
6
  "description": "",
7
7
  "dependencies": {
8
+ "@google-cloud/logging": "^11.2.0",
8
9
  "@hackylabs/deep-redact": "^2.2.1",
9
10
  "ajv": "^8.17.1",
10
11
  "ajv-formats": "^3.0.1",
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@shushed/helpers",
3
- "version": "0.0.58",
3
+ "version": "0.0.60",
4
4
  "author": "",
5
5
  "license": "UNLICENSED",
6
6
  "description": "",
7
7
  "dependencies": {
8
+ "@google-cloud/logging": "^11.2.0",
8
9
  "@hackylabs/deep-redact": "^2.2.1",
9
10
  "ajv": "^8.17.1",
10
11
  "ajv-formats": "^3.0.1",