Skip to content

Commit f74615b

Browse files
authored
Merge pull request #15 from bcgov/feat/rotationRules
feat: add rotation rules and fix json post
2 parents 6df4dea + 4640505 commit f74615b

File tree

7 files changed

+46
-11
lines changed

7 files changed

+46
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ The environment variable `CRON_BACKUP` is used to schedule the back of the compr
3030

3131
The environment variable `CRON_JANITOR` is used to schedule the janitor which removes files after they have been backed up. The number of log files to retain can be configured by setting `JANITOR_COPIES`.
3232

33+
## Rotation Setups
34+
35+
The default rotates files once every day. If you change the cron to run hourly, then it will rotate hourly. The minimum file size environment variable can be set to skip rotating files until they grow larger enough. The age maximum can ensure files don't remain on the server indefinitely.
36+
37+
#### LOGROTATE_FILESIZE_MIN
38+
39+
The minimum file size (in bytes) before the file is rotated. Empty files are always skipped. If you set the minimum and run cron frequently, you will prevent files from growing much larger than this size. Default: 0
40+
41+
#### LOGROTATE_AGE_MAX
42+
43+
The maximum age (in milliseconds) of a file before it is rotated (even if the minimum file size is not met). Values less than 1 are ignored. Default: 0
44+
3345
## Local Testing
3446

3547
1. Copy `setenv-tmpl.sh` to `setenv-local.sh`.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nr-objectstore-rotate",
3-
"version": "1.1.8",
3+
"version": "2.0.0",
44
"description": "Sidecar for rotating log files to objectstore",
55
"main": "index.js",
66
"scripts": {

src/broker/broker.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ export class BrokerService {
2222

2323
const params = ttl ? `?ttl=${ttl}&quickstart=true` : '?quickstart=true';
2424
const url = `${BROKER_URL}v1/intention/open${params}`;
25-
const message = intention;
2625
const headers = {
2726
'Content-Type': 'application/json',
2827
Authorization: `Bearer ${this.brokerJwt}`,
2928
};
3029

31-
const response = await sendHttpsRequest(url, 'POST', headers, message);
30+
const response = await sendHttpsRequest(url, 'POST', headers, intention);
3231

3332
if (isResponseSuccess(response.statusCode)) {
3433
this.openResponse = JSON.parse(response.body);
@@ -120,7 +119,7 @@ export class BrokerService {
120119
[HEADER_BROKER_TOKEN]: actionToken,
121120
};
122121

123-
return await sendHttpsRequest(url, 'POST', headers, JSON.stringify(file));
122+
return await sendHttpsRequest(url, 'POST', headers, file);
124123
}
125124
}
126125

src/broker/http-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function sendHttpsRequest(
88
url: string,
99
method: string,
1010
headers: any,
11-
body?: string,
11+
body?: any,
1212
): Promise<any> {
1313
return new Promise<any>((resolve, reject) => {
1414
const options: https.RequestOptions = {

src/constants.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export const LOGROTATE_DIRECTORY = process.env.LOGROTATE_DIRECTORY ?? 'logs';
77
export const LOGROTATE_STATUSFILE =
88
process.env.LOGROTATE_STATUSFILE ?? 'cron.db';
99

10+
export const LOGROTATE_FILESIZE_MIN = process.env.LOGROTATE_FILESIZE_MIN
11+
? Number.parseInt(process.env.LOGROTATE_FILESIZE_MIN)
12+
: 1;
13+
export const LOGROTATE_AGE_MAX = process.env.LOGROTATE_AGE_MAX
14+
? Number.parseInt(process.env.LOGROTATE_AGE_MAX)
15+
: 0;
16+
1017
export const LOGROTATE_SUFFIX = process.env.LOGROTATE_SUFFIX ?? 'log';
1118
export const LOGROTATE_POSTROTATE_COMMAND =
1219
process.env.LOGROTATE_POSTROTATE_COMMAND ?? '';
@@ -26,8 +33,10 @@ export const OBJECT_STORAGE_ACCESS_KEY =
2633
export const OBJECT_STORAGE_BUCKET = process.env.OBJECT_STORAGE_BUCKET ?? '';
2734
export const OBJECT_STORAGE_SECRET_KEY =
2835
process.env.OBJECT_STORAGE_SECRET_KEY ?? '';
29-
export const OBJECT_STORAGE_FILENAME_PREFIX =
30-
process.env.OBJECT_STORAGE_FILENAME_PREFIX ?? '';
36+
export const OBJECT_STORAGE_FILENAME_PREFIX = process.env
37+
.OBJECT_STORAGE_FILENAME_PREFIX
38+
? `${process.env.OBJECT_STORAGE_FILENAME_PREFIX}.`
39+
: '';
3140
// Optional key/value JSON object with metadata
3241
export const OBJECT_STORAGE_METADATA =
3342
process.env.OBJECT_STORAGE_METADATA ?? '';

src/cron/rotate.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@ import {
77
LOGROTATE_POSTROTATE_COMMAND,
88
LOGROTATE_DIRECTORY,
99
LOGROTATE_SUFFIX,
10+
LOGROTATE_FILESIZE_MIN,
11+
LOGROTATE_AGE_MAX,
1012
} from '../constants';
1113
import { DatabaseService } from '../services/database.service';
1214

1315
export async function rotateLogs(db: DatabaseService) {
14-
const files = fs.readdirSync(LOGROTATE_DIRECTORY);
15-
const logFiles = files.filter((file) => file.endsWith(LOGROTATE_SUFFIX));
1616
console.log('rotate: start');
17+
const files = fs.readdirSync(LOGROTATE_DIRECTORY);
18+
const now = new Date().getTime();
19+
let logFiles = files.filter((file) => file.endsWith(LOGROTATE_SUFFIX));
20+
logFiles = files.filter((file) => {
21+
const stats = fs.statSync(file);
22+
const endTime = stats.ctime.getTime() + LOGROTATE_AGE_MAX;
23+
const rotateFile =
24+
stats.size > 0 &&
25+
((LOGROTATE_AGE_MAX > 0 && now > endTime) ||
26+
stats.size > LOGROTATE_FILESIZE_MIN);
27+
if (!rotateFile) {
28+
console.log(`rotate: ${file} (skip)`);
29+
}
30+
return rotateFile;
31+
});
1732
if (logFiles.length > 0) {
1833
for (const file of logFiles) {
1934
await rotateLog(db, file);

0 commit comments

Comments
 (0)