Skip to content

Commit

Permalink
feat: add error to the observe callback (fixes #1)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: observer callback signature change.
  • Loading branch information
gajus committed Dec 12, 2019
1 parent 9845737 commit bb07437
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,23 @@ const main = async () => {
if (isKubernetesCredentialsPresent()) {
const resourceObserver = createResourceObserver();

resourceObserver.observe((podResourceSpecification, podResourceUsage) => {
for (const containerResourceSpecification of podResourceSpecification.containers) {
if (containerResourceSpecification.resources.limits && containerResourceSpecification.resources.limits.memory) {
const containerResourceUsage = podResourceUsage.containers.find((container) => {
return container.name === containerResourceSpecification.name;
});

if (!containerResourceUsage) {
throw new Error('Unexpected state.');
}

if (containerResourceUsage.usage.memory / containerResourceSpecification.resources.limits.memory > MAXIMUM_MEMORY_USAGE) {
lightship.shutdown();
resourceObserver.observe((error, podResourceSpecification, podResourceUsage) => {
if (error) {
// Handle error.
} else {
for (const containerResourceSpecification of podResourceSpecification.containers) {
if (containerResourceSpecification.resources.limits && containerResourceSpecification.resources.limits.memory) {
const containerResourceUsage = podResourceUsage.containers.find((container) => {
return container.name === containerResourceSpecification.name;
});

if (!containerResourceUsage) {
throw new Error('Unexpected state.');
}

if (containerResourceUsage.usage.memory / containerResourceSpecification.resources.limits.memory > MAXIMUM_MEMORY_USAGE) {
lightship.shutdown();
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dependencies": {
"bytes-iec": "^3.1.0",
"got": "^9.6.0",
"roarr": "^2.14.6"
"roarr": "^2.14.6",
"serialize-error": "^5.0.0"
},
"description": "Retrieves & observes Kubernetes Pod resource (CPU, memory) utilisation.",
"devDependencies": {
Expand Down
27 changes: 20 additions & 7 deletions src/factories/createResourceObserver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// @flow

import {
serializeError,
} from 'serialize-error';
import {
getCredentials,
getPodResourceSpecification,
Expand Down Expand Up @@ -69,15 +72,25 @@ export default () => {
const context = await ready;

const podResourceSpecification = context.podResourceSpecification;
const podResourceUsage = await getPodResourceUsage();

log.debug({
podResourceSpecification,
podResourceUsage,
}, 'observed resource usage');
try {
const podResourceUsage = await getPodResourceUsage();

log.debug({
podResourceSpecification,
podResourceUsage,
}, 'observed resource usage');

// eslint-disable-next-line callback-return
callback(null, podResourceSpecification, podResourceUsage);
} catch (error) {
log.error({
error: serializeError(error),
}, 'cannot get resource usage');

// eslint-disable-next-line callback-return
callback(podResourceSpecification, podResourceUsage);
// eslint-disable-next-line callback-return
callback(error, null, null);
}

timeout = setTimeout(() => {
tick();
Expand Down
6 changes: 5 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export type PodResourceSpecificationType = {|
+name: string,
|};

export type IntervalCallbackType = (podResourceSpecification: PodResourceSpecificationType, podResourceUsage: PodResourceUsageType) => void;
export type IntervalCallbackType = (
error: Error | null,
podResourceSpecification: PodResourceSpecificationType | null,
podResourceUsage: PodResourceUsageType | null
) => void;

0 comments on commit bb07437

Please sign in to comment.