zookeeperModel.js 1.32 KB
const model = require('../../../lib/model');
const {DegradeServer} = require('../../models');
const zookeeperHelpers = require('../../zookeeper/zookeeper-helpers');
const _ = require('lodash');

class ZookeeperModel extends model {
    constructor(ctx) {
        super(ctx);        
    }
    async getConfigAll() {
        return await DegradeServer.findAll();
    }
    async setPath(path, val) {
        let degradeServer = await DegradeServer.findOne({
            type: 'qcloud'
        });

        if (_.isEmpty(degradeServer)) {
            return false;
        }

        return zookeeperHelpers.creator(`${degradeServer.ip}:${degradeServer.port}`, path, val, true); // return Boolean
    }
    async getPath(path) {
        let degradeServer = await DegradeServer.findOne({
            type: 'qcloud'
        });

        if (_.isEmpty(degradeServer)) {
            return false;
        }

        return await zookeeperHelpers.getter(`${degradeServer.ip}:${degradeServer.port}`, path);
    }
    async removePath(path) {
        let degradeServer = await DegradeServer.findOne({
            type: 'qcloud'
        });

        if (_.isEmpty(degradeServer)) {
            return false;
        }

        return await zookeeperHelpers.remove(`${degradeServer.ip}:${degradeServer.port}`, path);
    }
}

module.exports = ZookeeperModel;