mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Merge pull request #174 from MrRozdor/log-research-sync
Add logging to researchSync plugin
This commit is contained in:
1 file changed
+71
-19
@@ -1,24 +1,79 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path')
|
||||||
const needle = require("needle");
|
const needle = require("needle");
|
||||||
|
|
||||||
|
|
||||||
|
function time() {
|
||||||
|
let d = new Date()
|
||||||
|
return `${d.getUTCHours()}:${d.getUTCMinutes()}:${d.getUTCSeconds()}.${d.getUTCMilliseconds()}`
|
||||||
|
}
|
||||||
|
|
||||||
class ResearchSync {
|
class ResearchSync {
|
||||||
constructor(slaveConfig, messageInterface, extras = {}){
|
constructor(slaveConfig, messageInterface, extras = {}){
|
||||||
this.config = slaveConfig;
|
this.config = slaveConfig
|
||||||
this.messageInterface = messageInterface;
|
this.messageInterface = messageInterface
|
||||||
|
|
||||||
this.functions = {
|
this.functions = {
|
||||||
dumpResearch: this.loadFunc("dumpResearch.lua"),
|
dumpResearch: this.loadFunc("dumpResearch.lua"),
|
||||||
enableResearch: this.loadFunc("enableResearch.lua"),
|
enableResearch: this.loadFunc("enableResearch.lua"),
|
||||||
updateProgress: this.loadFunc("updateProgress.lua")
|
updateProgress: this.loadFunc("updateProgress.lua")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.research = {};
|
this.log_folder = './logs'
|
||||||
this.prev_research = {};
|
if (!fs.existsSync(this.log_folder))
|
||||||
|
fs.mkdirSync(this.log_folder);
|
||||||
|
this.log_file = path.join(this.log_folder, `${this.config.unique}-research.log`)
|
||||||
|
this.get_node_name_and_move_log()
|
||||||
|
|
||||||
|
this.research = {}
|
||||||
|
this.prev_research = {}
|
||||||
this.initial_request_own_data(
|
this.initial_request_own_data(
|
||||||
() => this.setup_sync_task(extras)
|
() => this.setup_sync_task(extras)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(data) {
|
||||||
|
try {
|
||||||
|
console.log(`researchSync: ${data}`)
|
||||||
|
fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
error(data) {
|
||||||
|
try {
|
||||||
|
console.error(`researchSync: ${data}`)
|
||||||
|
fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get_node_name_and_move_log() {
|
||||||
|
const url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
|
||||||
|
needle.get(url, (err, res, slaves_data) => {
|
||||||
|
if (err)
|
||||||
|
return this.error(err)
|
||||||
|
|
||||||
|
slaves_data = Object.values(slaves_data)
|
||||||
|
let node_data = slaves_data.find(
|
||||||
|
slave_data => slave_data.unique === this.config.unique.toString()
|
||||||
|
)
|
||||||
|
if (node_data === undefined)
|
||||||
|
return this.error('slave was not found')
|
||||||
|
let node_name = node_data.instanceName
|
||||||
|
let log_file = path.join(this.log_folder, `${node_name}-research.log`)
|
||||||
|
if (fs.existsSync(log_file)) {
|
||||||
|
fs.appendFileSync(log_file, fs.readFileSync(this.log_file));
|
||||||
|
fs.unlinkSync(this.log_file)
|
||||||
|
} else {
|
||||||
|
fs.renameSync(this.log_file, log_file)
|
||||||
|
}
|
||||||
|
this.log_file = log_file
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
initial_request_own_data(callback) {
|
initial_request_own_data(callback) {
|
||||||
const url = `${this.config.masterIP}:${this.config.masterPort}/api/getSlaveMeta`
|
const url = `${this.config.masterIP}:${this.config.masterPort}/api/getSlaveMeta`
|
||||||
const data = {
|
const data = {
|
||||||
@@ -31,12 +86,12 @@ class ResearchSync {
|
|||||||
}
|
}
|
||||||
needle.post(url, data, options, (err, res, techs) => {
|
needle.post(url, data, options, (err, res, techs) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(`Can't get own slave data:`)
|
this.log(`Can't get own slave data:`)
|
||||||
console.error(err)
|
this.error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (res.statusCode === 404) {
|
if (res.statusCode === 404) {
|
||||||
console.log('researchSync: slave is not registered yet. Delaying for 5 secs')
|
this.log('slave is not registered yet. Delaying for 5 secs')
|
||||||
setTimeout(
|
setTimeout(
|
||||||
() => this.initial_request_own_data(callback),
|
() => this.initial_request_own_data(callback),
|
||||||
5000
|
5000
|
||||||
@@ -44,14 +99,14 @@ class ResearchSync {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200) {
|
||||||
console.log(`Can't get own slave data:`)
|
this.log(`Can't get own slave data:`)
|
||||||
console.error(`status code ${res.statusCode}, ${res.body}`)
|
this.error(`status code ${res.statusCode}, ${res.body}`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
techs = JSON.parse(techs)
|
techs = JSON.parse(techs)
|
||||||
if (typeof techs.research === 'object')
|
if (typeof techs.research === 'object')
|
||||||
this.research = techs.research
|
this.research = techs.research
|
||||||
console.log('researchSync: techs imported from master')
|
this.log('techs imported from master')
|
||||||
callback()
|
callback()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -103,13 +158,10 @@ class ResearchSync {
|
|||||||
instanceID: this.config.unique,
|
instanceID: this.config.unique,
|
||||||
password: this.config.clientPassword,
|
password: this.config.clientPassword,
|
||||||
meta: {research: this.research}
|
meta: {research: this.research}
|
||||||
}, {headers: {'x-access-token': this.config.masterAuthToken}, json: true}, function (err, resp) {
|
}, {headers: {'x-access-token': this.config.masterAuthToken}, json: true}, (err, resp) => {
|
||||||
// it doesn't get called
|
|
||||||
if (err)
|
if (err)
|
||||||
console.error(err)
|
this.error(err)
|
||||||
else
|
})
|
||||||
console.log('sent')
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear_contribution_to_researched_techs() {
|
clear_contribution_to_researched_techs() {
|
||||||
@@ -234,7 +286,7 @@ class ResearchSync {
|
|||||||
let log_message = tech.infinite
|
let log_message = tech.infinite
|
||||||
? `Unlocking infinite research ${name} at level ${this.research[name].level}`
|
? `Unlocking infinite research ${name} at level ${this.research[name].level}`
|
||||||
: `Unlocking research ${name}`
|
: `Unlocking research ${name}`
|
||||||
console.log(log_message);
|
this.log(log_message);
|
||||||
this.messageInterface(log_message);
|
this.messageInterface(log_message);
|
||||||
this.research[name] = tech;
|
this.research[name] = tech;
|
||||||
}
|
}
|
||||||
@@ -252,7 +304,7 @@ class ResearchSync {
|
|||||||
command = command.replace(/{last_check_progress}/g, progress)
|
command = command.replace(/{last_check_progress}/g, progress)
|
||||||
command = command.replace(/{new_progress}/g, tech.progress)
|
command = command.replace(/{new_progress}/g, tech.progress)
|
||||||
this.messageInterface(command);
|
this.messageInterface(command);
|
||||||
console.log(
|
this.log(
|
||||||
`Updating ${name}: ${this.research[name].progress} += ${tech.progress - this.research[name].progress}`
|
`Updating ${name}: ${this.research[name].progress} += ${tech.progress - this.research[name].progress}`
|
||||||
);
|
);
|
||||||
this.research[name].progress = tech.progress
|
this.research[name].progress = tech.progress
|
||||||
@@ -267,7 +319,7 @@ class ResearchSync {
|
|||||||
continue
|
continue
|
||||||
let diff = this.research[name].contribution - this.prev_research[name].contribution
|
let diff = this.research[name].contribution - this.prev_research[name].contribution
|
||||||
if (Math.abs(diff) > Number.EPSILON * 1000)
|
if (Math.abs(diff) > Number.EPSILON * 1000)
|
||||||
console.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
|
this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user