This commit is contained in:
2022-02-27 18:10:44 +00:00
parent 99fa906630
commit 0e64a2076b
5 changed files with 181 additions and 86 deletions

View File

@ -6,12 +6,18 @@ import * as interfaces from './interfaces';
*/
export class Bobcat {
// STATIC
public static async createFromNetworkAddress(networkAddressArg: string) {
public static minerAuthObject = {
headers: {
Authorization: 'Basic ' + Buffer.from('bobcat:miner').toString('base64')
}
}
public static async createFromNetworkAddress (networkAddressArg: string) {
const newBobcat = new Bobcat(networkAddressArg);
try {
await newBobcat.checkMinerStatus();
await newBobcat.gatherMinerDetails();
} catch(err) {
} catch (err) {
console.log('initial adding completed with errors');
}
return newBobcat;
@ -30,7 +36,7 @@ export class Bobcat {
/**
* checks the status of the miner
*/
public async checkMinerStatus() {
public async checkMinerStatus () {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/status.json`, {
timeout: 30000
});
@ -42,7 +48,7 @@ export class Bobcat {
/**
* gathers the miner details
*/
public async gatherMinerDetails() {
public async gatherMinerDetails () {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/miner.json`, {
timeout: 30000
});
@ -54,16 +60,18 @@ export class Bobcat {
/**
* runs maintenance on the bobcat
*/
public async runMaintenance() {
public async runMaintenance () {
await this.checkMinerStatus();
await this.gatherMinerDetails();
if (this.latestStatus.status === 'Synced') {
if (this.latestStatus.status === 'Synced' && parseInt(this.latestStatus.gap) > -50 && parseInt(this.latestStatus.gap) < 50) {
console.log(`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Synced. ok!`)
return;
}
if (this.latestStatus.status === 'Syncing') {
console.log(`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Syncing... ok!`)
console.log(
`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Syncing... ok!`
);
return;
}
@ -77,15 +85,26 @@ export class Bobcat {
}
}
public async restart() {
/**
* 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 () {
console.log(`cooling down before restart`);
await plugins.smartdelay.delayFor(10000);
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/reboot`, {
method: 'POST',
headers: {
Authorization: 'Basic ' + Buffer.from('bobcat:miner').toString('base64')
},
timeout: 30000
timeout: 30000,
...Bobcat.minerAuthObject
});
console.log(response.statusCode);
}

View File

@ -1,13 +1,36 @@
import * as plugins from './bobcat.plugins';
import { Bobcat } from './bobcat.classes.bobcat';
import { Bobcat } from './bobcat.classes.bobcat';
/**
*
* a manager for managing multiple bobcats
*/
export class BobcatManager {
public taskmanager = new plugins.taskbuffer.TaskManager();
public bobcats: Bobcat[] = [];
public async addBobcat(networkAddressArg: string) {
/**
* a store for knowing what has happened retrospectively
*/
public actionStore: {
actionName: string;
actionPayload: string;
}[] = [];
constructor() {
this.taskmanager.addAndScheduleTask(new plugins.taskbuffer.Task({
name: 'contMaintenance',
taskFunction: async () => {
this.actionStore.
await this.runMaintenance();
}
}), '0 0 * * * *');
}
/**
* adds a bobcat to the manager
* @param networkAddressArg
*/
public async addBobcat (networkAddressArg: string) {
const newBobcat = await Bobcat.createFromNetworkAddress(networkAddressArg);
this.bobcats.push(newBobcat);
console.log(`added ${newBobcat.latestMinerDetails.animal} at ${newBobcat.networkAddress}`);
@ -16,7 +39,7 @@ export class BobcatManager {
/**
* runs the maintenance on all managed bobcats
*/
public async runMaintenance() {
public async runMaintenance () {
console.log(`now running maintenance on ${this.bobcats.length} bobcats!`);
console.log(`cooling down for 10 seconds`);
await plugins.smartdelay.delayFor(10000);
@ -25,4 +48,18 @@ export class BobcatManager {
await bobcat.runMaintenance();
}
};
/**
* starts continuous maintenance of the bobcat miners
*/
public async startTaskmanager () {
this.taskmanager.start();
}
/**
* stops the taskmanager
*/
public async stopTaskmanager () {
this.taskmanager.stop();
}
}

View File

@ -1,9 +1,11 @@
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartrequest from '@pushrocks/smartrequest';
import * as taskbuffer from '@pushrocks/taskbuffer';
export {
smartdelay,
smartnetwork,
smartrequest
smartrequest,
taskbuffer,
}