Skip to content

Commit

Permalink
PUB-92: Fix logging for crashes (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Aug 8, 2023
1 parent 0c8a24f commit 4c79247
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-steaks-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@honestfoodcompany/pubsub': patch
---

Improve error handling and add custom fail function - PUB-92
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"../scripts/changesets/changelog.js",
{
"repo": "deliveryhero/hfc-pubsub",
"jiraUrl": "https://honesttech.atlassian.net"
"jiraUrl": "https://jira.deliveryhero.com"
}
],
"commit": true,
Expand Down
56 changes: 28 additions & 28 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

### Minor Changes

- [PUB-90](https://honesttech.atlassian.net/browse/PUB-90) - Add support for gzip compression/decompression ([#107](https://github.com/deliveryhero/hfc-pubsub/pull/107) by [@rndD](https://github.com/rndD))
- [PUB-90](https://jira.deliveryhero.com/browse/PUB-90) - Add support for gzip compression/decompression ([#107](https://github.com/deliveryhero/hfc-pubsub/pull/107) by [@rndD](https://github.com/rndD))

### Patch Changes

- [PUB-85](https://honesttech.atlassian.net/browse/PUB-85) - Load subscriptions in parallel ([#105](https://github.com/deliveryhero/hfc-pubsub/pull/105) by [@rndD](https://github.com/rndD))
- [PUB-85](https://jira.deliveryhero.com/browse/PUB-85) - Load subscriptions in parallel ([#105](https://github.com/deliveryhero/hfc-pubsub/pull/105) by [@rndD](https://github.com/rndD))

## 2.1.2

### Patch Changes

- [PUB-88](https://honesttech.atlassian.net/browse/PUB-88) - Pin dependencies to specific versions ([#92](https://github.com/deliveryhero/hfc-pubsub/pull/92) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-88](https://jira.deliveryhero.com/browse/PUB-88) - Pin dependencies to specific versions ([#92](https://github.com/deliveryhero/hfc-pubsub/pull/92) by [@rohit-gohri](https://github.com/rohit-gohri))

## 2.1.1

Expand All @@ -26,53 +26,53 @@

### Minor Changes

- [PUB-84](https://honesttech.atlassian.net/browse/PUB-84) - Add support for labels to subscirptions ([#88](https://github.com/deliveryhero/hfc-pubsub/pull/88) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-84](https://jira.deliveryhero.com/browse/PUB-84) - Add support for labels to subscirptions ([#88](https://github.com/deliveryhero/hfc-pubsub/pull/88) by [@rohit-gohri](https://github.com/rohit-gohri))

## 2.0.1

### Patch Changes

- [PUB-72](https://honesttech.atlassian.net/browse/PUB-72) - Divide subscriptions among multiple clients ([#86](https://github.com/deliveryhero/hfc-pubsub/pull/86) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-72](https://jira.deliveryhero.com/browse/PUB-72) - Divide subscriptions among multiple clients ([#86](https://github.com/deliveryhero/hfc-pubsub/pull/86) by [@rohit-gohri](https://github.com/rohit-gohri))

## 2.0.0

[Migration Guide](https://deliveryhero.github.io/hfc-pubsub/guides/migrating-to-v2)

### ⚠️ Major Changes ⚠️

- [PUB-68](https://honesttech.atlassian.net/browse/PUB-68) - Set min supported node version as 12.22 ([#72](https://github.com/deliveryhero/hfc-pubsub/pull/72) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-68](https://jira.deliveryhero.com/browse/PUB-68) - Set min supported node version as 12.22 ([#72](https://github.com/deliveryhero/hfc-pubsub/pull/72) by [@rohit-gohri](https://github.com/rohit-gohri))

- [PUB-52](https://honesttech.atlassian.net/browse/PUB-52) - Remove `grpc` ([#57](https://github.com/deliveryhero/hfc-pubsub/pull/57) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-52](https://jira.deliveryhero.com/browse/PUB-52) - Remove `grpc` ([#57](https://github.com/deliveryhero/hfc-pubsub/pull/57) by [@rohit-gohri](https://github.com/rohit-gohri))

- [PUB-48](https://honesttech.atlassian.net/browse/PUB-48) - Topic class now uses static properties and generics ([#81](https://github.com/deliveryhero/hfc-pubsub/pull/81) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-48](https://jira.deliveryhero.com/browse/PUB-48) - Topic class now uses static properties and generics ([#81](https://github.com/deliveryhero/hfc-pubsub/pull/81) by [@rohit-gohri](https://github.com/rohit-gohri))

- [PUB-77](https://honesttech.atlassian.net/browse/PUB-77) - Misc Changes ([#84](https://github.com/deliveryhero/hfc-pubsub/pull/84) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-77](https://jira.deliveryhero.com/browse/PUB-77) - Misc Changes ([#84](https://github.com/deliveryhero/hfc-pubsub/pull/84) by [@rohit-gohri](https://github.com/rohit-gohri))

- Change project-id env var to `GOOGLE_CLOUD_PROJECT`
- Removed export of private methods
- Refactor internals
- Change `ackDeadlineSeconds` to `ackDeadline`
- Add `metadata` to default error handler

- [PUB-65](https://honesttech.atlassian.net/browse/PUB-65) - Remove deprecated code ([#66](https://github.com/deliveryhero/hfc-pubsub/pull/66) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-65](https://jira.deliveryhero.com/browse/PUB-65) - Remove deprecated code ([#66](https://github.com/deliveryhero/hfc-pubsub/pull/66) by [@rohit-gohri](https://github.com/rohit-gohri))

### Minor Changes

- [PUB-73](https://honesttech.atlassian.net/browse/PUB-73) - Add/improve helpers and TS usage([#79](https://github.com/deliveryhero/hfc-pubsub/pull/79) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-73](https://jira.deliveryhero.com/browse/PUB-73) - Add/improve helpers and TS usage([#79](https://github.com/deliveryhero/hfc-pubsub/pull/79) by [@ishan123456789](https://github.com/ishan123456789))

1. Allows to make `_timestamp` optional
2. Removes double validation of topic names.
3. Use `publishJSON` instead of buffering the payload.
4. Adds `.toJSON` method to get parsed message/
5. Remove getters (like getProjects, getName, getClient) and just use `this.projects`, `this.name`, etc directly.

- [PUB-69](https://honesttech.atlassian.net/browse/PUB-69) - Changed init position for `SubscriptionService.instance` ([#77](https://github.com/deliveryhero/hfc-pubsub/pull/77) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-69](https://jira.deliveryhero.com/browse/PUB-69) - Changed init position for `SubscriptionService.instance` ([#77](https://github.com/deliveryhero/hfc-pubsub/pull/77) by [@ishan123456789](https://github.com/ishan123456789))

### Patch Changes

- [PUB-75](https://honesttech.atlassian.net/browse/PUB-75) - Using yarn instead of npm ([#83](https://github.com/deliveryhero/hfc-pubsub/pull/83) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-75](https://jira.deliveryhero.com/browse/PUB-75) - Using yarn instead of npm ([#83](https://github.com/deliveryhero/hfc-pubsub/pull/83) by [@ishan123456789](https://github.com/ishan123456789))

- [PUB-69](https://honesttech.atlassian.net/browse/PUB-69) - Throws error in case no subs found or wrong `PUBSUB_ROOT_DIR` env defined ([#76](https://github.com/deliveryhero/hfc-pubsub/pull/76) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-69](https://jira.deliveryhero.com/browse/PUB-69) - Throws error in case no subs found or wrong `PUBSUB_ROOT_DIR` env defined ([#76](https://github.com/deliveryhero/hfc-pubsub/pull/76) by [@ishan123456789](https://github.com/ishan123456789))

**Full Changelog: [v1.11.0...v2.0.0](https://github.com/deliveryhero/hfc-pubsub/compare/v1.11.0...v2.0.0)**

Expand All @@ -82,29 +82,29 @@

- Adds optional health check server to tell if all subs are open or not ([#73](https://github.com/deliveryhero/hfc-pubsub/pull/73) by [@ishan123456789](https://github.com/ishan123456789))

- [PUB-71](https://honesttech.atlassian.net/browse/PUB-71) - Add Subscription level handleError method ([#75](https://github.com/deliveryhero/hfc-pubsub/pull/75) by [@ketan-saxena](https://github.com/ketan-saxena))
- [PUB-71](https://jira.deliveryhero.com/browse/PUB-71) - Add Subscription level handleError method ([#75](https://github.com/deliveryhero/hfc-pubsub/pull/75) by [@ketan-saxena](https://github.com/ketan-saxena))

### Patch Changes

- [PUB-64](https://honesttech.atlassian.net/browse/PUB-64) - Update dependencies ([#78](https://github.com/deliveryhero/hfc-pubsub/pull/78) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-64](https://jira.deliveryhero.com/browse/PUB-64) - Update dependencies ([#78](https://github.com/deliveryhero/hfc-pubsub/pull/78) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.10.4

### Patch Changes

- [PUB-60](https://honesttech.atlassian.net/browse/PUB-60) - Deprecate v1,v2 style Subscriber classes ([#64](https://github.com/deliveryhero/hfc-pubsub/pull/64) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-60](https://jira.deliveryhero.com/browse/PUB-60) - Deprecate v1,v2 style Subscriber classes ([#64](https://github.com/deliveryhero/hfc-pubsub/pull/64) by [@rohit-gohri](https://github.com/rohit-gohri))

- [PUB-66](https://honesttech.atlassian.net/browse/PUB-66) - Fix close method creating new subscription instance rather than closing existing one ([#63](https://github.com/deliveryhero/hfc-pubsub/pull/63) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-66](https://jira.deliveryhero.com/browse/PUB-66) - Fix close method creating new subscription instance rather than closing existing one ([#63](https://github.com/deliveryhero/hfc-pubsub/pull/63) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.10.3

### Patch Changes

- [PUB-52](https://honesttech.atlassian.net/browse/PUB-52) - Deprecate native grpc usage ([#58](https://github.com/deliveryhero/hfc-pubsub/pull/58) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-52](https://jira.deliveryhero.com/browse/PUB-52) - Deprecate native grpc usage ([#58](https://github.com/deliveryhero/hfc-pubsub/pull/58) by [@rohit-gohri](https://github.com/rohit-gohri))

- Allows setting up env variables through CLI args ([#61](https://github.com/deliveryhero/hfc-pubsub/pull/61) by [@ishan123456789](https://github.com/ishan123456789))

- [PUB-50](https://honesttech.atlassian.net/browse/PUB-50) - Update documentation ([#60](https://github.com/deliveryhero/hfc-pubsub/pull/60) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-50](https://jira.deliveryhero.com/browse/PUB-50) - Update documentation ([#60](https://github.com/deliveryhero/hfc-pubsub/pull/60) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.10.2

Expand All @@ -124,40 +124,40 @@

### Minor Changes

- [PUB-44](https://honesttech.atlassian.net/browse/PUB-44) - Added option to add custom logger ([#45](https://github.com/deliveryhero/hfc-pubsub/pull/45) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-44](https://jira.deliveryhero.com/browse/PUB-44) - Added option to add custom logger ([#45](https://github.com/deliveryhero/hfc-pubsub/pull/45) by [@ishan123456789](https://github.com/ishan123456789))

### Patch Changes

- [PUB-44](https://honesttech.atlassian.net/browse/PUB-44) - Add changesets to manage versioning ([#44](https://github.com/deliveryhero/hfc-pubsub/pull/44) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-44](https://jira.deliveryhero.com/browse/PUB-44) - Add changesets to manage versioning ([#44](https://github.com/deliveryhero/hfc-pubsub/pull/44) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.9.0

### Minor Changes

- [PUB-47](https://honesttech.atlassian.net/browse/PUB-47) - Optimized DLQ configuration making PROJECT_NUMBER optional for assigning Publisher, Subscriber roles and allowing default subscription creation as a part of DLQ configuration ([#43](https://github.com/deliveryhero/hfc-pubsub/pull/43) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-47](https://jira.deliveryhero.com/browse/PUB-47) - Optimized DLQ configuration making PROJECT_NUMBER optional for assigning Publisher, Subscriber roles and allowing default subscription creation as a part of DLQ configuration ([#43](https://github.com/deliveryhero/hfc-pubsub/pull/43) by [@ishan123456789](https://github.com/ishan123456789))

## 1.8.1

### Patch Changes

- [PUB-46](https://honesttech.atlassian.net/browse/PUB-46) - Allow multiple entry points without checking for folder ([#42](https://github.com/deliveryhero/hfc-pubsub/pull/42) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-46](https://jira.deliveryhero.com/browse/PUB-46) - Allow multiple entry points without checking for folder ([#42](https://github.com/deliveryhero/hfc-pubsub/pull/42) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.8.0

### Minor Changes

- [PUB-42](https://honesttech.atlassian.net/browse/PUB-42) - Add attributes option to publish command in Topic ([#41](https://github.com/deliveryhero/hfc-pubsub/pull/41) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-42](https://jira.deliveryhero.com/browse/PUB-42) - Add attributes option to publish command in Topic ([#41](https://github.com/deliveryhero/hfc-pubsub/pull/41) by [@rohit-gohri](https://github.com/rohit-gohri))

### Patch Changes

- [PUB-34](https://honesttech.atlassian.net/browse/PUB-34) - Update subscriber metadata for existing subscriptions ([#32](https://github.com/deliveryhero/hfc-pubsub/pull/32) by [@ishan123456789](https://github.com/ishan123456789))
- [PUB-34](https://jira.deliveryhero.com/browse/PUB-34) - Update subscriber metadata for existing subscriptions ([#32](https://github.com/deliveryhero/hfc-pubsub/pull/32) by [@ishan123456789](https://github.com/ishan123456789))

- [PUB-43](https://honesttech.atlassian.net/browse/PUB-43) - Attach subscriber metadata to error object in case unable to start subscription ([#39](https://github.com/deliveryhero/hfc-pubsub/pull/39) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-43](https://jira.deliveryhero.com/browse/PUB-43) - Attach subscriber metadata to error object in case unable to start subscription ([#39](https://github.com/deliveryhero/hfc-pubsub/pull/39) by [@rohit-gohri](https://github.com/rohit-gohri))

## 1.7.12

### Patch Changes

- Fix build due to invalid import paths ([#38](https://github.com/deliveryhero/hfc-pubsub/pull/38) by [@rohit-gohri](https://github.com/rohit-gohri))

- [PUB-31](https://honesttech.atlassian.net/browse/PUB-31) - Add close method for graceful shutdowns ([#20](https://github.com/deliveryhero/hfc-pubsub/pull/20) by [@rohit-gohri](https://github.com/rohit-gohri))
- [PUB-31](https://jira.deliveryhero.com/browse/PUB-31) - Add close method for graceful shutdowns ([#20](https://github.com/deliveryhero/hfc-pubsub/pull/20) by [@rohit-gohri](https://github.com/rohit-gohri))
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Moved to [Docs](https://deliveryhero.github.io/hfc-pubsub/getting-started/instal

## CLI commands - starting and listing subscriptions

Moved to [CLI](https://deliveryhero.github.io/hfc-pubsub/cli)
Moved to [CLI](https://deliveryhero.github.io/hfc-pubsub/guides/cli)

## Subscriptions

Expand All @@ -57,4 +57,4 @@ Moved to [Service](https://deliveryhero.github.io/hfc-pubsub/server/service)

## Enabling Synchronous Driver

Moved to [Drivers](https://deliveryhero.github.io/hfc-pubsub/drivers)
Moved to [Drivers](https://deliveryhero.github.io/hfc-pubsub/guides/drivers)
2 changes: 1 addition & 1 deletion docs/getting-started/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The framework expects the following environment variables. They can be added to
| `root-dir` | `PUBSUB_ROOT_DIR` | must be the path to your project's pubsub directory. |
| `google-application-credentials` | `GOOGLE_APPLICATION_CREDENTIALS` | see <https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account> to generate this |
| `project-id` | `GOOGLE_CLOUD_PROJECT` | the project-id in Google Cloud Platform |
| `labels` | `GOOGLE_CLOUD_LABELS` | Labels in stringified JSON format |
| `labels` | `GOOGLE_CLOUD_LABELS` | Labels in stringified JSON format |
| `server-port` | `PUBSUB_SERVER_PORT` | PORT at which the pubsub should run the server at |
| `health-server` | `PUBSUB_HEALTH_SERVER` | If value assigned is true this would run a server showing health state and return 500 if not healthy |

Expand Down
2 changes: 1 addition & 1 deletion docs/subscribing/Internal Error Handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar_label: Error Handling
sidebar_position: 2
---

To handle any internal error that might occur on pubsub side, a the method `handleError` can be declared either in Subscriber Object or in SubscriptionService level.
To handle any **internal** error that might occur on pubsub side, the method `handleError` can be declared either in Subscriber Object or in SubscriptionService.

- If the error handler is provided at `Subscriber` object level then it would override the error handler provided at `SubscriptionService` level.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ const subscriber: SubscriberObject<Payload> = {
},

handleMessage: (message) => {
console.log('got message', message.data.toString());
console.log(message.toJSON(), typeof message.toJSON());
const x = message.toJSON();
console.log(x?.x);
message.ack();
},

handleError: (error) => {
console.error(error, 'error in sub');
},
};

export default subscriber;
29 changes: 27 additions & 2 deletions src/cli/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#! /usr/local/bin/node
require('dotenv').config({ path: require('find-config')('.env') });
import { isatty } from 'tty';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { Logger } from '../service/logger';
import commands from './commands';

require('yargs')
const parser = yargs(hideBin(process.argv))
.command(commands.start)
.command(commands.list)
.options({
Expand Down Expand Up @@ -30,4 +34,25 @@ require('yargs')
describe: 'If true runs server telling health state of subscriptions',
type: 'string',
},
}).argv;
})
.showHelpOnFail(false)
.fail(false);

async function main() {
await parser.parse();
}

if (require.main === module) {
main()
.then(() => {
Logger.Instance.info('Finished');
process.exit(0);
})
.catch(async (err) => {
Logger.Instance.error({ err }, `Command failed`);
if (isatty(process.stdout.fd)) {
console.info(await yargs.getHelp());
}
process.exit(1);
});
}
6 changes: 5 additions & 1 deletion src/client/googlePubSub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ export default class GooglePubSubAdapter implements PubSubClientV2 {
subscription.on('message', (message: GoogleCloudMessage): void => {
subscriberInstance
.handleMessage(Message.fromGCloud(message))
.catch(() => {
.catch((error) => {
Logger.Instance.error(
{ error },
'Unexpected error while processing message',
);
message.nack();
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/client/googlePubSub/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getSubscription(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return subscriptions.get(metadata.subscriptionName)!;
}
// NOTE: Each client can handle a max of 20 subs, but limiting to 15 https://honesttech.atlassian.net/browse/PUB-72
// NOTE: Each client can handle a max of 20 subs, but limiting to 15 https://jira.deliveryhero.com/browse/PUB-72
const isNewClientNeeded =
Math.floor(subscriptions.size / 15) > _clients.length - 1;

Expand Down
5 changes: 1 addition & 4 deletions src/service/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export class Logger {
Logger.logger = logger;
}
public static get Instance(): LoggerOptions {
if (!Logger.logger) {
Logger.logger = console;
}
return Logger.logger;
return Logger.logger ?? console;
}
}
export const setLogger = (logger: LoggerOptions): void => {
Expand Down

0 comments on commit 4c79247

Please sign in to comment.