Skip to content

Commit

Permalink
Merge pull request #310 from TWilkin/294.persistence-indexes
Browse files Browse the repository at this point in the history
#111/#294/#297 Add health check and indexes to persistence
  • Loading branch information
TWilkin authored Apr 3, 2023
2 parents c438204 + 84c8867 commit 0f637b5
Show file tree
Hide file tree
Showing 35 changed files with 1,020 additions and 631 deletions.
7 changes: 4 additions & 3 deletions .github/scripts/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ update_version() {
increase_version $helmVersion "macro"
helmVersion=$newVersion
echo "Increasing helm chart to v$helmVersion"
set_chart_version $helmPath $powerpiVersion $helmVersion $subchartVersion
set_chart_version $helmPath $powerpiVersion $helmVersion $service $subchartVersion

# commit the change
echo "Committing version changes"
Expand All @@ -81,14 +81,15 @@ set_chart_version() {
local path=$1
local appVersion=$2
local chartVersion=$3
local subchartVersion=$4
local service=$4
local subchartVersion=$5

yq e -i ".appVersion = \"$appVersion\"" $path
yq e -i ".version = \"$chartVersion\"" $path

if [ ! -z $subchartVersion ]
then
yq e -i "(.dependencies[] | select(.name == \"energy-monitor\").version) = \"$subchartVersion\"" $path
yq e -i "(.dependencies[] | select(.name == \"$service\").version) = \"$subchartVersion\"" $path
fi

git add $path
Expand Down
4 changes: 2 additions & 2 deletions common/node/common-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powerpi/common-test",
"version": "0.0.3",
"version": "0.0.4",
"description": "PowerPi Common Typescript Testing library",
"private": true,
"license": "GPL-3.0-only",
Expand Down Expand Up @@ -28,6 +28,6 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@powerpi/common": "^0.0.11"
"@powerpi/common": "^0.0.13"
}
}
11 changes: 10 additions & 1 deletion common/node/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ yarn build:common

## Testing

There are currently no automated tests for this library.
This library can be tested by executing the following commands.

```bash
# From the root of your PowerPi checkout
# Download the dependencies
yarn

# Run the tests
yarn test:common
```

## Local Execution

Expand Down
4 changes: 4 additions & 0 deletions common/node/common/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
8 changes: 5 additions & 3 deletions common/node/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powerpi/common",
"version": "0.0.12",
"version": "0.0.13",
"description": "PowerPi Common Typescript library",
"private": true,
"license": "GPL-3.0-only",
Expand All @@ -13,10 +13,12 @@
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"build": "tsc"
"build": "tsc && cp src/health.sh dist/",
"test": "jest"
},
"files": [
"dist/src"
"dist/src",
"dist/health.sh"
],
"devDependencies": {
"@types/dateformat": "^5.0.0",
Expand Down
36 changes: 36 additions & 0 deletions common/node/common/src/health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

# the number of seconds the health check file can be since last modification without being unhealthy
AGE=$1

# the path to the health check file
HEALTH_FILE=$2

if [ -z $AGE ]
then
AGE=12
fi

if [ -z $HEALTH_FILE ]
then
HEALTH_FILE=/home/node/app/powerpi_health
fi

if [ ! -f $HEALTH_FILE ]
then
# the file doesn't exist, that's unhealthy
exit 1
fi

NOW=$(date +%s)
HEALTH=$(stat $HEALTH_FILE -c %Y)
SINCE=$(expr $NOW - $HEALTH)

if [ $SINCE -gt $AGE ]
then
# the health file is older than AGE, that's unhealthy
exit 2
fi

# it's newer than AGE, that's healthy
exit 0
20 changes: 20 additions & 0 deletions common/node/common/src/services/FileService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { open, readFile, utimes } from "fs/promises";
import { Service } from "typedi";

@Service()
export default class FileService {
public async read(path: string) {
return await readFile(path);
}

public async touch(path: string) {
const now = new Date();

try {
await utimes(path, now, now);
} catch (e) {
const handle = await open(path, "a");
await handle.close();
}
}
}
13 changes: 7 additions & 6 deletions common/node/common/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from "fs";
import Container, { Service } from "typedi";
import util from "util";
import {
IDeviceConfigFile,
IFloorplanConfigFile,
Expand All @@ -9,6 +8,7 @@ import {
} from "../models/config";
import { Device, IDevice } from "../models/device";
import { ISensor, Sensor } from "../models/sensor";
import FileService from "./FileService";
import { IntervalParserService } from "./interval";

export enum ConfigFileType {
Expand All @@ -19,16 +19,13 @@ export enum ConfigFileType {
Users = "users",
}

// allow reading of files using await
const readAsync = util.promisify(fs.readFile);

@Service()
export class ConfigService {
protected interval: IntervalParserService;

private configs: { [key in ConfigFileType]?: { data: object; checksum: string } };

constructor() {
constructor(protected readonly fs: FileService) {
this.interval = Container.get(IntervalParserService);

this.configs = {};
Expand Down Expand Up @@ -76,6 +73,10 @@ export class ConfigService {
});
}

get healthCheckFile() {
return process.env["HEALTH_CHECK_FILE"] ?? "/home/node/app/powerpi_health";
}

get configWaitTime() {
const str = process.env["CONFIG_WAIT_TIME"];
if (str) {
Expand Down Expand Up @@ -151,7 +152,7 @@ export class ConfigService {
}

protected async readFile(filePath: string) {
return (await readAsync(filePath)).toString().trim();
return (await this.fs.read(filePath)).toString().trim();
}

private get databaseHost() {
Expand Down
1 change: 1 addition & 0 deletions common/node/common/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./config";
export { default as FileService } from "./FileService";
export * from "./interval";
export * from "./logger";
export * from "./mqtt";
Expand Down
29 changes: 29 additions & 0 deletions common/node/common/test/services/FileService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";
import { FileService } from "../../src";

describe("FileService", () => {
let subject: FileService;

beforeEach(() => (subject = new FileService()));

test("read", async () => {
const result = await subject.read(__filename);

expect(result).toBeDefined();

const str = new TextDecoder().decode(result);
expect(str).toContain('test("read",');
});

test("touch", async () => {
const tmpDir = fs.mkdtempSync(__filename);
const file = path.join(tmpDir, "touchy");

expect(fs.existsSync(file)).toBeFalsy();

await subject.touch(file);

expect(fs.existsSync(file)).toBeTruthy();
});
});
24 changes: 4 additions & 20 deletions kubernetes/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,54 @@ apiVersion: v2
name: powerpi
description: A Helm chart for deploying the PowerPi home automation stack
type: application
version: 0.1.6
appVersion: 0.1.4
version: 0.1.7
appVersion: 0.1.5
dependencies:
# services
- name: babel-fish
version: 0.1.0
condition: global.voiceAssistant

- name: clacks-config
version: 0.1.0
condition: global.config

- name: database
version: 0.1.2
version: 0.1.3
condition: global.persistence

- name: deep-thought
version: 0.1.1

- name: energy-monitor
version: 0.1.1
condition: global.energy-monitor

- name: freedns
version: 0.1.0
condition: global.freeDNS

- name: light-fantastic
version: 0.1.0
condition: global.scheduler

- name: mosquitto
version: 0.1.2

- name: persistence
version: 0.1.0
version: 0.1.1
condition: global.persistence

- name: smarter-device-manager
version: 0.1.1

- name: ui
version: 0.1.2

# controllers
- name: energenie-controller
version: 0.1.1
condition: global.energenie

- name: harmony-controller
version: 0.1.1
condition: global.harmony

- name: lifx-controller
version: 0.1.1
condition: global.lifx

- name: macro-controller
version: 0.1.1

- name: node-controller
version: 0.1.1
condition: global.node

- name: zigbee-controller
version: 0.1.1
condition: global.zigbee
2 changes: 1 addition & 1 deletion kubernetes/charts/database/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: database
description: A Helm chart for deploying the Postgres database for PowerPi
type: application
version: 0.1.2
version: 0.1.3
appVersion: 15.2-alpine3.17
2 changes: 1 addition & 1 deletion kubernetes/charts/database/templates/backup-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}}

{{- $data := dict
"Name" "database-backup-job"
"Name" "database-backup"
"Image" "postgres"
"RequestMemory" "50Mi"
"NodeSelector" $nodeSelector
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/charts/persistence/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: persistence
description: A Helm chart for the PowerPi database message persistence service
type: application
version: 0.1.0
appVersion: 0.1.5
version: 0.1.1
appVersion: 0.2.0
8 changes: 8 additions & 0 deletions kubernetes/charts/persistence/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@
)
)
)
"Probe" (dict
"Command" (list
"/bin/sh"
"/home/node/app/node_modules/@powerpi/common/dist/health.sh"
)
"ReadinessInitialDelay" 10
"LivenessInitialDelay" 20
)
}}
{{- include "powerpi.deployment" (merge (dict "Params" $data) . )}}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
"start:persistence": "yarn workspace @powerpi/persistence start:dev",
"start:ui": "yarn workspace @powerpi/ui start",
"test:babel-fish": "yarn workspace @powerpi/babel-fish test",
"test:common": "yarn workspace @powerpi/common test",
"test:clacks-config": "yarn workspace @powerpi/clacks-config test",
"test:persistence": "yarn workspace @powerpi/persistence test",
"test:ui": "yarn workspace @powerpi/ui test"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions services/babel-fish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"@jovotech/platform-core": "^4.2.22",
"@jovotech/server-express": "^4.0.0",
"@powerpi/api": "^0.0.17",
"@powerpi/common": "^0.0.12",
"@powerpi/common": "^0.0.13",
"source-map-support": "^0.5.19"
},
"devDependencies": {
"@jovotech/cli-core": "^4.0.0",
"@jovotech/db-filedb": "^4.0.0",
"@jovotech/filebuilder": "^0.0.1",
"@powerpi/common-test": "^0.0.3",
"@powerpi/common-test": "^0.0.4",
"@types/express": "^4.17.11",
"ts-node": "^10.9.1",
"tsc-watch": "^4.2.9"
Expand Down
2 changes: 1 addition & 1 deletion services/clacks-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ yarn
yarn build:common
yarn build:common-test

# Run the service locally
# Run the tests
yarn test:clacks-config
```

Expand Down
Loading

0 comments on commit 0f637b5

Please sign in to comment.