Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
a9ff2e2ddc | |||
812a43449d | |||
806de97d22 | |||
5ab8115b34 | |||
d761b1459d | |||
26f0ac0508 | |||
efa5982dc9 | |||
db8f587d8d | |||
cbc3334994 | |||
d111e3709b | |||
66a8610bb1 | |||
e4c41b82ef | |||
47df20fd7e | |||
2182f51e09 | |||
35e4f129df | |||
c579f0d87f | |||
830fa7176d | |||
2734bf787b | |||
0e64a2076b | |||
99fa906630 | |||
5fb5979c91 | |||
cf61de9093 | |||
cf7c3cb910 | |||
b8eb82de12 | |||
8c01d5cabf | |||
1eef97ee9a | |||
45544fffd0 | |||
f227eaee37 | |||
74ea18dc76 | |||
c0f03d11b6 | |||
ceab0fb872 | |||
f17349309d | |||
cb98b63a04 | |||
e5fe74d2d2 | |||
babcb832f3 |
569
package-lock.json
generated
569
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/bobcat",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.23",
|
||||
"private": false,
|
||||
"description": "a module to talk to bobcat miners",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -15,6 +15,7 @@
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/qenv": "^4.0.10",
|
||||
"@pushrocks/tapbundle": "^4.0.7",
|
||||
"@types/node": "^17.0.21",
|
||||
"tslint": "^6.1.3",
|
||||
@ -36,6 +37,10 @@
|
||||
"readme.md"
|
||||
],
|
||||
"dependencies": {
|
||||
"@pushrocks/smartrequest": "^1.1.56"
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartnetwork": "^2.0.14",
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/smartrequest": "^1.1.56",
|
||||
"@pushrocks/taskbuffer": "^2.1.17"
|
||||
}
|
||||
}
|
||||
|
23
test/test.ts
23
test/test.ts
@ -1,17 +1,34 @@
|
||||
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit');
|
||||
|
||||
import * as bobcat from '../ts/index';
|
||||
|
||||
let testBobcatManager: bobcat.BobcatManager;
|
||||
let testBobcat: bobcat.Bobcat;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
tap.test('should create a bobcat manager', async () => {
|
||||
testBobcatManager = new bobcat.BobcatManager();
|
||||
expect(testBobcatManager).toBeInstanceOf(bobcat.BobcatManager);
|
||||
});
|
||||
|
||||
tap.test('', async () => {
|
||||
testBobcat = new bobcat.Bobcat();
|
||||
tap.test('should create a bobcat', async () => {
|
||||
testBobcat = new bobcat.Bobcat('bobcat.bleu.de');
|
||||
expect(testBobcat).toBeInstanceOf(bobcat.Bobcat);
|
||||
})
|
||||
|
||||
tap.test('should add a Bobcat miner', async () => {
|
||||
const bobcatAddresses = testQenv.getEnvVarOnDemand('BOBCATS').split(',');
|
||||
console.log(bobcatAddresses);
|
||||
|
||||
for (const bobcatAddress of bobcatAddresses) {
|
||||
await testBobcatManager.addBobcat(bobcatAddress);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should run maintenance on bobcats', async () => {
|
||||
await testBobcatManager.runMaintenance();
|
||||
})
|
||||
|
||||
tap.start();
|
||||
|
@ -1,8 +1,130 @@
|
||||
import * as plugins from './bobcat.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
/**
|
||||
* maps to an individual bobcat miner
|
||||
*/
|
||||
export class Bobcat {
|
||||
|
||||
// STATIC
|
||||
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 plugins.smartpromise.timeoutAndContinue(newBobcat.checkMinerStatus());
|
||||
await plugins.smartpromise.timeoutAndContinue(newBobcat.gatherMinerDetails());
|
||||
} catch (err) {
|
||||
console.log('initial adding completed with errors');
|
||||
}
|
||||
return newBobcat;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public networkAddress: string;
|
||||
public latestStatus: interfaces.IMinerStatus;
|
||||
public latestMinerDetails: interfaces.IMinerDetailsResponse
|
||||
|
||||
constructor(networkAddressArg: string) {
|
||||
console.log(`adding bobcat at ${networkAddressArg}`);
|
||||
this.networkAddress = networkAddressArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks the status of the miner
|
||||
*/
|
||||
public async checkMinerStatus () {
|
||||
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/status.json`, {
|
||||
timeout: 60000
|
||||
});
|
||||
const body: interfaces.IMinerStatus = response.body;
|
||||
this.latestStatus = body;
|
||||
return this.latestStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* gathers the miner details
|
||||
*/
|
||||
public async gatherMinerDetails () {
|
||||
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/miner.json`, {
|
||||
timeout: 60000
|
||||
});
|
||||
const body: interfaces.IMinerDetailsResponse = response.body;
|
||||
this.latestMinerDetails = body;
|
||||
return this.latestMinerDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* runs maintenance on the bobcat
|
||||
*/
|
||||
public async runMaintenance () {
|
||||
await plugins.smartpromise.timeoutAndContinue(this.checkMinerStatus());
|
||||
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);
|
||||
await plugins.smartpromise.timeoutAndContinue(this.gatherMinerDetails());
|
||||
await plugins.smartdelay.delayFor(10000);
|
||||
if (this.latestStatus.status === 'Synced' && parseInt(this.latestStatus.gap) > -100 && 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!`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.latestStatus.status !== 'Synced') {
|
||||
console.log(`Miner ${this.latestMinerDetails.animal} is not synced. Restarting now!`);
|
||||
try {
|
||||
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;
|
||||
} catch (err) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Looks like miner ${this.latestMinerDetails.animal} is Synced, but does not fall under predefined statuses!`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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',
|
||||
timeout: 60000,
|
||||
...Bobcat.minerAuthObject
|
||||
});
|
||||
console.log(response.statusCode);
|
||||
}
|
||||
}
|
@ -1,8 +1,74 @@
|
||||
import * as plugins from './bobcat.plugins';
|
||||
import { Bobcat } from './bobcat.classes.bobcat';
|
||||
|
||||
/**
|
||||
*
|
||||
* a manager for managing multiple bobcats
|
||||
*/
|
||||
export class BobcatManager {
|
||||
|
||||
public taskmanager = new plugins.taskbuffer.TaskManager();
|
||||
public bobcats: Bobcat[] = [];
|
||||
|
||||
/**
|
||||
* 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.push();
|
||||
await this.runMaintenance();
|
||||
}
|
||||
}), '0 0 * * * *');
|
||||
this.taskmanager.addAndScheduleTask(new plugins.taskbuffer.Task({
|
||||
name: 'contStatus',
|
||||
taskFunction: async () => {
|
||||
this.actionStore.push();
|
||||
for (const bobcat of this.bobcats) {
|
||||
bobcat.checkMinerStatus();
|
||||
};
|
||||
}
|
||||
}), '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}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* runs the maintenance on all managed bobcats
|
||||
*/
|
||||
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);
|
||||
for (const bobcat of this.bobcats) {
|
||||
console.log(`now running maintenance on ${bobcat.latestMinerDetails?.animal} at ${bobcat.networkAddress}`);
|
||||
await plugins.smartpromise.timeoutAndContinue(bobcat.runMaintenance());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* starts continuous maintenance of the bobcat miners
|
||||
*/
|
||||
public async startTaskmanager () {
|
||||
this.taskmanager.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* stops the taskmanager
|
||||
*/
|
||||
public async stopTaskmanager () {
|
||||
this.taskmanager.stop();
|
||||
}
|
||||
}
|
@ -1,5 +1,13 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as taskbuffer from '@pushrocks/taskbuffer';
|
||||
|
||||
export {
|
||||
smartrequest
|
||||
smartdelay,
|
||||
smartnetwork,
|
||||
smartpromise,
|
||||
smartrequest,
|
||||
taskbuffer,
|
||||
}
|
||||
|
35
ts/interfaces/index.ts
Normal file
35
ts/interfaces/index.ts
Normal file
@ -0,0 +1,35 @@
|
||||
export interface IMinerStatus {
|
||||
status: string;
|
||||
gap: string;
|
||||
miner_height: string;
|
||||
blockchain_height: string;
|
||||
epoch: 'rpc';
|
||||
}
|
||||
|
||||
export interface IMinerDetailsResponse {
|
||||
ota_version: string;
|
||||
region: 'region_eu868';
|
||||
frequency_plan: 'eu868';
|
||||
animal: string;
|
||||
pubkey: string;
|
||||
miner: {
|
||||
State: 'running';
|
||||
Status: string;
|
||||
Names: ['/miner'];
|
||||
Image: string;
|
||||
Created: number;
|
||||
};
|
||||
p2p_status: string[];
|
||||
miner_height: string;
|
||||
epoch: string;
|
||||
ports_desc: "only need to port forward 44158. For 22, only when need remote support. public port open/close isn't accurate here, if your listen_addr is IP address, it should be OK";
|
||||
ports: { [key: string]: string };
|
||||
private_ip: string;
|
||||
public_ip: string;
|
||||
peerbook: string[];
|
||||
height: string[];
|
||||
temp0: string;
|
||||
temp1: string;
|
||||
timestamp: string;
|
||||
errors: string;
|
||||
}
|
Reference in New Issue
Block a user