Skip to content

Commit

Permalink
feat: redis config may be set using env; updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ron96G committed Feb 6, 2023
1 parent aeeeb05 commit 8a2dcd2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Docker Image CI

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:
Expand Down
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Furthermore, a cron job is executed every n seconds which collects the current s

Thus, the following metrics are collected:

| Metric | type | description |
| Metric | Type | Description |
| ------------------------- | --------- | ------------------------------------------------------- |
| bullmq_processed_duration | histogram | Processing time for completed jobs |
| bullmq_completed_duration | histogram | Completion time for jobs |
Expand All @@ -46,23 +46,56 @@ Each metric also has the attribute `queue` which indicated which queue the metri

## How to use

### Variables

These environment variables may be set to overwrite the values in the config file.
Note that not all values are supported.

| Name | Description |
| -------------- | ----------------------------------- |
| REDIS_HOST | Redis host, e. g. "localhost:6379/" |
| REDIS_USERNAME | Redis username |
| REDIS_PASSWORD | Redis password |
| REDIS_SSL | Wether to use ssl |

### Local

1. Install the dependencies

```
```bash
npm install
```

2. Default environment is `local`. This can be set using the `NODE_ENV` variable.

```
```bash
export NODE_ENV=production
```

3. Make sure that the required config file is present: `./configs/config-${NODE_ENV}.json` (see [local](./configs/config-local.json)).
4. Start the server

```
```bash
npm run dev
```

5. Access the resources `http://localhost:8080/bullmq/ui/login` or `http://localhost:8080/prometheus/metrics`

### Docker

The Dockerimage is published using the [local](./configs/config-local.json) configuration. In most cases that will not be sufficient and should be overwritten.
This can be done using environment variables (see [here](#variables)) or by mounting a separate file.

```bash
# This needs a config file under ./configs/config-dev.json
docker run \
-it \
--mount type=bind,source=$(pwd)/configs,target=/app/configs \
--env=NODE_ENV=dev \
--env=REDIS_HOST=some-host:6379/ \
rgummich/bullmq-exporter
```

### Kubernetes

In Kubernetes this may be done using [Secrets](https://kubernetes.io/docs/concepts/configuration/secret/).
14 changes: 12 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ export interface Config {
users?: Array<any>;
}

function setFromEnv(key: string, def: any) {
const val = process.env[key.toUpperCase()];
return val === undefined ? def : val;
}

const prefix = process.env.NODE_ENV?.toLowerCase() || "local";
const jsonRaw = readFileSync(`./configs/config-${prefix}.json`);
const config = JSON.parse(jsonRaw.toString());
const config = JSON.parse(jsonRaw.toString()) as Config;

config.redis.host = setFromEnv("REDIS_HOST", config.redis.host);
config.redis.username = setFromEnv("REDIS_USERNAME", config.redis.username);
config.redis.password = setFromEnv("REDIS_PASSWORD", config.redis.password);
config.redis.ssl = setFromEnv("REDIS_SSL", config.redis.ssl);

export default config as Config;
export default config;

0 comments on commit 8a2dcd2

Please sign in to comment.