2022-02-24 19:16:21 +00:00
|
|
|
import * as plugins from './bobcat.plugins';
|
2022-02-25 20:25:29 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2022-02-24 19:16:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* maps to an individual bobcat miner
|
|
|
|
*/
|
|
|
|
export class Bobcat {
|
2022-02-25 19:36:44 +00:00
|
|
|
// STATIC
|
2022-02-27 18:10:44 +00:00
|
|
|
public static minerAuthObject = {
|
|
|
|
headers: {
|
|
|
|
Authorization: 'Basic ' + Buffer.from('bobcat:miner').toString('base64')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async createFromNetworkAddress (networkAddressArg: string) {
|
2022-02-25 19:36:44 +00:00
|
|
|
const newBobcat = new Bobcat(networkAddressArg);
|
2022-02-25 20:48:26 +00:00
|
|
|
try {
|
2022-02-27 20:19:06 +00:00
|
|
|
await plugins.smartpromise.timeoutAndContinue(newBobcat.checkMinerStatus());
|
|
|
|
await plugins.smartpromise.timeoutAndContinue(newBobcat.gatherMinerDetails());
|
2022-02-27 18:10:44 +00:00
|
|
|
} catch (err) {
|
2022-02-25 20:48:26 +00:00
|
|
|
console.log('initial adding completed with errors');
|
|
|
|
}
|
2022-02-25 19:36:44 +00:00
|
|
|
return newBobcat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
public networkAddress: string;
|
2022-02-25 20:25:29 +00:00
|
|
|
public latestStatus: interfaces.IMinerStatus;
|
|
|
|
public latestMinerDetails: interfaces.IMinerDetailsResponse
|
2022-02-25 19:36:44 +00:00
|
|
|
|
2022-02-25 19:57:46 +00:00
|
|
|
constructor(networkAddressArg: string) {
|
2022-02-25 21:45:11 +00:00
|
|
|
console.log(`adding bobcat at ${networkAddressArg}`);
|
2022-02-25 19:57:46 +00:00
|
|
|
this.networkAddress = networkAddressArg;
|
2022-02-25 19:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks the status of the miner
|
|
|
|
*/
|
2022-02-27 18:10:44 +00:00
|
|
|
public async checkMinerStatus () {
|
2022-02-25 21:45:11 +00:00
|
|
|
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/status.json`, {
|
2022-02-27 21:36:17 +00:00
|
|
|
timeout: 60000
|
2022-02-25 21:45:11 +00:00
|
|
|
});
|
2022-02-25 20:25:29 +00:00
|
|
|
const body: interfaces.IMinerStatus = response.body;
|
|
|
|
this.latestStatus = body;
|
|
|
|
return this.latestStatus;
|
2022-02-25 19:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gathers the miner details
|
|
|
|
*/
|
2022-02-27 18:10:44 +00:00
|
|
|
public async gatherMinerDetails () {
|
2022-02-25 21:45:11 +00:00
|
|
|
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/miner.json`, {
|
2022-02-27 21:36:17 +00:00
|
|
|
timeout: 60000
|
2022-02-25 21:45:11 +00:00
|
|
|
});
|
2022-02-25 20:25:29 +00:00
|
|
|
const body: interfaces.IMinerDetailsResponse = response.body;
|
|
|
|
this.latestMinerDetails = body;
|
|
|
|
return this.latestMinerDetails;
|
2022-02-25 19:36:44 +00:00
|
|
|
}
|
2022-02-25 20:33:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* runs maintenance on the bobcat
|
|
|
|
*/
|
2022-02-27 18:10:44 +00:00
|
|
|
public async runMaintenance () {
|
2022-02-27 21:36:17 +00:00
|
|
|
await plugins.smartpromise.timeoutAndContinue(this.checkMinerStatus());
|
2022-02-27 22:21:35 +00:00
|
|
|
await plugins.smartdelay.delayFor(10000);
|
|
|
|
await plugins.smartpromise.timeoutAndContinue(this.gatherMinerDetails());
|
|
|
|
await plugins.smartdelay.delayFor(10000);
|
|
|
|
await plugins.smartpromise.timeoutAndContinue(this.checkMinerStatus());
|
|
|
|
await plugins.smartdelay.delayFor(10000);
|
2022-02-27 21:36:17 +00:00
|
|
|
await plugins.smartpromise.timeoutAndContinue(this.gatherMinerDetails());
|
2022-02-27 22:23:51 +00:00
|
|
|
await plugins.smartdelay.delayFor(10000);
|
2022-02-27 22:21:35 +00:00
|
|
|
if (this.latestStatus.status === 'Synced' && parseInt(this.latestStatus.gap) > -100 && parseInt(this.latestStatus.gap) < 50) {
|
2022-02-25 21:45:11 +00:00
|
|
|
console.log(`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Synced. ok!`)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.latestStatus.status === 'Syncing') {
|
2022-02-27 18:10:44 +00:00
|
|
|
console.log(
|
|
|
|
`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Syncing... ok!`
|
|
|
|
);
|
2022-02-25 21:45:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:33:24 +00:00
|
|
|
if (this.latestStatus.status !== 'Synced') {
|
|
|
|
console.log(`Miner ${this.latestMinerDetails.animal} is not synced. Restarting now!`);
|
2022-02-25 21:45:11 +00:00
|
|
|
try {
|
2022-02-27 22:21:35 +00:00
|
|
|
await this.restart();
|
|
|
|
return;
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.latestStatus.status === 'Synced' && parseInt(this.latestStatus.gap) < -100) {
|
|
|
|
console.log(`Miner ${this.latestMinerDetails.animal} is Synced, but strangely ahead of blockchain. Restarting!`);
|
|
|
|
try {
|
|
|
|
await this.restart();
|
|
|
|
return;
|
2022-02-25 21:45:11 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
}
|
2022-02-25 20:33:24 +00:00
|
|
|
}
|
2022-02-27 22:21:35 +00:00
|
|
|
|
|
|
|
console.log(`Looks like miner ${this.latestMinerDetails.animal} is Synced, but does not fall under predefined statuses!`);
|
2022-02-25 20:33:24 +00:00
|
|
|
}
|
2022-02-25 20:45:21 +00:00
|
|
|
|
2022-02-27 18:10:44 +00:00
|
|
|
/**
|
|
|
|
* triggers a fast resync
|
|
|
|
*/
|
|
|
|
public async triggerFastResync() {
|
|
|
|
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/fastsync`, {
|
|
|
|
method: 'POST',
|
|
|
|
...Bobcat.minerAuthObject
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restarts the miner
|
|
|
|
*/
|
|
|
|
public async restart () {
|
2022-02-25 21:45:11 +00:00
|
|
|
console.log(`cooling down before restart`);
|
|
|
|
await plugins.smartdelay.delayFor(10000);
|
2022-02-25 20:45:21 +00:00
|
|
|
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/reboot`, {
|
|
|
|
method: 'POST',
|
2022-02-27 21:36:17 +00:00
|
|
|
timeout: 60000,
|
2022-02-27 18:10:44 +00:00
|
|
|
...Bobcat.minerAuthObject
|
2022-02-25 20:45:21 +00:00
|
|
|
});
|
|
|
|
console.log(response.statusCode);
|
|
|
|
}
|
2022-02-24 19:16:21 +00:00
|
|
|
}
|