From f47b066024dcdd493e6dc552fd5bc7bbdf59d2e3 Mon Sep 17 00:00:00 2001 From: jysperm Date: Sun, 20 Jul 2014 01:03:51 +0800 Subject: [PATCH] finish memcached plugin --- core/plugin.coffee | 8 +++---- core/router/panel.coffee | 40 ++++++++++++++++++++++++--------- core/view/panel.jade | 2 +- plugin/linux/monitor.coffee | 2 +- plugin/memcached/index.coffee | 1 + plugin/memcached/service.coffee | 27 +++++++++++++++++----- 6 files changed, 58 insertions(+), 22 deletions(-) diff --git a/core/plugin.coffee b/core/plugin.coffee index c25fd9a..0c01eb5 100644 --- a/core/plugin.coffee +++ b/core/plugin.coffee @@ -7,8 +7,6 @@ i18n = require './i18n' config = require './../config' {requestAuthenticate} = require './router/middleware' -exports.switchButton = [] - exports.get = (name) -> return require path.join(__dirname, "../plugin/#{name}") @@ -18,9 +16,6 @@ exports.loadPlugins = (app) -> plugin = exports.get name - if plugin.switch - exports.switchButton.push name - if plugin.action app.use ('/plugin/' + name), plugin.action @@ -36,3 +31,6 @@ exports.writeConfig = (path, content, callback) -> fs.unlink path, -> callback() + +exports.sudoSu = (account, command) -> + return "sudo su #{account.username} -c '#{command}'" \ No newline at end of file diff --git a/core/router/panel.coffee b/core/router/panel.coffee index c4bdd43..65c7dfd 100644 --- a/core/router/panel.coffee +++ b/core/router/panel.coffee @@ -51,21 +51,41 @@ exports.get '/', requestAuthenticate, (req, res) -> account.attribute.remaining_time = Math.ceil(billing.calcRemainingTime(account) / 24) - async.map account.attribute.services, (item, callback) -> - p = plugin.get item - async.map (p.panel_widgets ? []), (widgetBuilder, callback) -> - widgetBuilder account, (html) -> - callback null, - plugin: p - html: html - , (err, result) -> - callback null, result + switch_buttons = [] + + async.map account.attribute.services, (service_name, callback) -> + service_plugin = plugin.get service_name + + if service_plugin.switch + switch_buttons.push service_name + + async.parallel + widgets: (callback) -> + async.map (service_plugin.panel_widgets ? []), (widgetBuilder, callback) -> + widgetBuilder account, (html) -> + callback null, + plugin: service_plugin + html: html + , (err, result) -> + callback null, result + + switch_status: (callback) -> + if service_plugin.switch_status + service_plugin.switch_status account, (is_enable) -> + account.attribute.plugin[service_name].is_enable = is_enable + callback() + else + callback() + + , callback , (err, result) -> widgets = [] + for item in result - widgets = widgets.concat item + widgets = widgets.concat item.widgets res.render 'panel', + switch_buttons: switch_buttons plugin: plugin account: account plans: plans diff --git a/core/view/panel.jade b/core/view/panel.jade index bd565a6..d852f1f 100644 --- a/core/view/panel.jade +++ b/core/view/panel.jade @@ -50,7 +50,7 @@ block content #service-switch.row header 服务开关 - for name in plugin.switchButton + for name in switch_buttons if account.attribute.plugin[name].is_enable button(data-name= name).btn.btn-danger 关闭 #{t('plugins.' + name + '.name')} else diff --git a/plugin/linux/monitor.coffee b/plugin/linux/monitor.coffee index 4c58be2..17538ce 100644 --- a/plugin/linux/monitor.coffee +++ b/plugin/linux/monitor.coffee @@ -69,7 +69,7 @@ exports.getProcessList = (callback) -> command: result[11] } - app.redis.setex 'rp:process_list', 10, JSON.stringify(plist), -> + app.redis.setex 'rp:process_list', 5, JSON.stringify(plist), -> callback plist exports.monitoring = -> diff --git a/plugin/memcached/index.coffee b/plugin/memcached/index.coffee index 0170bf7..4550223 100644 --- a/plugin/memcached/index.coffee +++ b/plugin/memcached/index.coffee @@ -9,3 +9,4 @@ module.exports = service: service switch: true + switch_status: service.switch_status diff --git a/plugin/memcached/service.coffee b/plugin/memcached/service.coffee index d743824..5157986 100644 --- a/plugin/memcached/service.coffee +++ b/plugin/memcached/service.coffee @@ -5,9 +5,12 @@ tmp = require 'tmp' fs = require 'fs' plugin = require '../../core/plugin' +monitor = require '../linux/monitor' mAccount = require '../../core/model/account' +MEMCACHED_FLAGS = '-d -m 16' + module.exports = enable: (account, callback) -> mAccount.update _id: account._id, @@ -17,13 +20,27 @@ module.exports = callback() delete: (account, callback) -> - if account.attribute.plugin.memcached.is_enable - this.switch account, false, callback - else - callback() + callback() switch: (account, is_enable, callback) -> - callback() + if is_enable + app.redis.del 'rp:process_list', => + @switch_status account, (is_act_enable) -> + if is_act_enable + return callback() + + child_process.exec plugin.sudoSu(account, "memcached #{MEMCACHED_FLAGS} -s ~/memcached.sock"), -> + callback() + else + child_process.exec plugin.sudoSu(account, "pkill -exf -u #{account.username} \"memcached #{MEMCACHED_FLAGS} -s /home/#{account.username}/memcached.sock\""), -> + callback() + + switch_status: (account, callback) -> + monitor.getProcessList (plist) -> + process = _.find plist, (i) -> + return i.user == account.username and i.command == "memcached #{MEMCACHED_FLAGS} -s /home/#{account.username}/memcached.sock" + + callback if process then true else false preview: (callback) -> jade.renderFile path.join(__dirname, 'view/preview.jade'), {}, (err, html) ->