Skip to content

Commit

Permalink
Dont use redis inside the workers (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Niek Candaele <22315101+niekcandaele@users.noreply.github.com>
  • Loading branch information
halkeye and niekcandaele committed Jun 11, 2020
1 parent 354bdbb commit d3b0208
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 30 deletions.
12 changes: 7 additions & 5 deletions api/hooks/sdtdLogs/LoggingObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class LoggingObject extends EventEmitter {
};

async init(ms = sails.config.custom.logCheckInterval) {

if (!ms) {
ms = 3000;
}
Expand Down Expand Up @@ -91,7 +90,6 @@ class LoggingObject extends EventEmitter {
result = JSON.parse(result);
}


if (result.serverId.toString() !== this.serverId) {
// not one of ours
return;
Expand Down Expand Up @@ -128,6 +126,7 @@ class LoggingObject extends EventEmitter {
}

await this.setFailedToZero();
await this.setLastLogLine(result.lastLogLine);
await this.addFetchJob();
}

Expand All @@ -153,14 +152,17 @@ class LoggingObject extends EventEmitter {
);
}

async setLastLogLine() {
async setLastLogLine(lastLogLine) {
const server = await SdtdServer.findOne(this.serverId)
const webUIUpdate = await SdtdApi.getWebUIUpdates(SdtdServer.getAPIConfig(server));
const lastLogLine = parseInt(webUIUpdate.newlogs) + 1;
if (!lastLogLine && lastLogLine !== 0) {
const webUIUpdate = await SdtdApi.getWebUIUpdates(SdtdServer.getAPIConfig(server));
lastLogLine = parseInt(webUIUpdate.newlogs) + 1;
}
await sails.helpers.redis.set(
`sdtdserver:${this.serverId}:sdtdLogs:lastLogLine`,
lastLogLine
);
this.lastLogLine = lastLogLine;
this.emptyResponses = 0;
return lastLogLine;
}
Expand Down
20 changes: 1 addition & 19 deletions api/hooks/sdtdLogs/logProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ const handleLogLine = require("./handleLogLine");

module.exports = async function(job) {
const resultLogs = [];
// Get latest log line from Redis
let lastLogLine = parseInt(
await sails.helpers.redis.get(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastLogLine`
)
);

let lastLogLine = job.data.lastLogLine;
// If latest log line is not found, get it from the server
if (!lastLogLine) {
const webUIUpdate = await sails.helpers.sdtdApi.getWebUIUpdates(SdtdServer.getAPIConfig(job.data.server));
Expand All @@ -22,12 +16,6 @@ module.exports = async function(job) {
// Adjust latest log line based on new logs we got
lastLogLine = lastLogLine + newLogs.entries.length;

// Set latest log line in Redis
await sails.helpers.redis.set(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastLogLine`,
lastLogLine
);

// Parse these logs into usable data
let index = -1;
for (const line of newLogs.entries) {
Expand All @@ -46,12 +34,6 @@ module.exports = async function(job) {
}
}

// Set last success date in Redis
await sails.helpers.redis.set(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastSuccess`,
Date.now()
);

return Promise.resolve({
serverId: job.data.server.id,
lastLogLine,
Expand Down
8 changes: 4 additions & 4 deletions test/integration/cron-job/cron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ var expect = require("chai").expect;

describe('POST /api/sdtdserver/cron', function () {

it('should return 200 with valid info', function (done) {
supertest(sails.hooks.http.app)
it('should return 200 with valid info', async function () {
const response = await supertest(sails.hooks.http.app)
.post('/api/sdtdserver/cron')
.send({
serverId: 1,
command: 'help',
temporalValue: '0 */6 * * *'
})
.expect('Content-Type', /json/)
.expect(200, done);
expect(response.statusCode).to.be.eql(200);
expect(response.header['content-type']).to.include('json');
});
it('should return 400 when command or temporal value is not given', function (done) {
supertest(sails.hooks.http.app)
Expand Down
3 changes: 1 addition & 2 deletions test/integration/hooks/sdtdLogs/logProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const logProcessor = require("../../../../api/hooks/sdtdLogs/logProcessor");

describe('logProcessor', function () {
it('Confirm able to fetch log messages', async function () {
sails.cache[`sdtdserver:${sails.testServer.id}:sdtdLogs:lastLogLine`] = 1100;
sinon.stub(sails.helpers.sdtdApi, "getWebUIUpdates").callsFake(async function () {
return {
newlogs: 1100
newlogs: 1099
}
});
sinon.stub(sails.helpers.sdtdApi, "getLog").callsFake(async function () {
Expand Down

0 comments on commit d3b0208

Please sign in to comment.