cocalc/src / smc-project / node_modules / forever / lib / forever / service / adapters / systemv / index.js
50665 views/*1* index.js: Top-level include for the systemv foreverd service adapter2*3* (C) 2010 Nodejitsu Inc.4* MIT LICENCE5*6*/78var fs = require('fs'),9util = require('util'),10path = require('path'),11spawn = require('child_process').spawn,12nssocket = require('nssocket'),13forever = require('../../../../forever'),14Adapter = require('../adapter');1516//17// Classic init.d script adapter18// Sometimes called inittab, but its origin is called systemv19//20var SystemVAdapter = module.exports = function SystemVAdapter(service) {21Adapter.call(this, service);22this.daemonized = false;23};2425util.inherits(SystemVAdapter, Adapter);2627SystemVAdapter.prototype.install = function install(callback) {28//29// Copy the init.d script to the right location30// TODO Distribution fixes?31//32forever.config.set('root', path.join('/var', 'local', 'foreverd'));33var initdPath = path.join('/etc', 'init.d', 'foreverd'),34script,35target;3637try {38fs.mkdirSync(forever.config.get('root'), '0777');39fs.mkdirSync(path.join(forever.config.get('root'), 'services'), '0777');40}41catch (e) {42if (e.code !== 'EEXIST') {43return callback && callback(e);44}45}4647try {48script = fs.createReadStream(path.join(__dirname, 'foreverd'));49target = fs.createWriteStream(initdPath, { flags: 'w', mode: '0777' });5051script.pipe(target);52script.on('end', function () {53var directories = fs.readdirSync('/etc');54directories.forEach(function (directory) {55var match = directory.match(/^rc(\d+)\.d$/),56killOrStart;5758if (match) {59killOrStart = { 0: true, 1: true, 6: true }[match[1]] ? 'K' : 'S';6061try {62fs.symlinkSync(initdPath, path.join('/etc', directory, killOrStart + '20foreverd'));63}64catch (e) {65if (e.code !== 'EEXIST') {66return callback && callback(e);67}68}69}70});7172return callback && callback();73});74}75catch (e) {76if (e.code !== 'EEXIST') {77return callback && callback(e);78}79}80};8182//83//84//85SystemVAdapter.prototype.load = function load(callback) {86forever.config.set('root', path.join('/var', 'local', 'foreverd'));87var serviceFiles = fs.readdirSync(path.join(forever.config.get('root'), 'services')),88services = [];8990if (serviceFiles.length !== 0) {91serviceFiles.forEach(function loadServiceFiles(serviceFile, index) {92var serviceFilePath = path.join(forever.config.get('root'), 'services', serviceFile),93service = JSON.parse(fs.readFileSync(serviceFilePath)),94file = service.file,95options = service.options;9697options.minUptime = 200;98services.push({99file: service.file,100options: service.options101});102});103}104105callback(services);106};107108SystemVAdapter.prototype.start = function start(callback) {109spawn('/etc/init.d/foreverd', ['start']);110return callback && callback();111};112113SystemVAdapter.prototype.run = function run(callback) {114if (this.daemonized) {115return callback();116}117118var self = this,119pidFilePath = path.join('/var', 'run', 'foreverd.pid'),120logFilePath = path.join('/var', 'log', 'foreverd');121122process.on('exit', function removePIDFile() {123try {124fs.unlinkSync(pidFilePath);125}126catch (err) {127// we are exiting anyway. this may have some noexist error already128}129});130131fs.open(logFilePath, 'w+', function serviceLogOpened(err, logFile) {132if (err) {133throw err;134}135136self.service.startServer(function () {137try {138//139// TODO: Create a pseudo-daemon to replace this.140//141// daemon.start(logFile);142// daemon.lock(pidFilePath);143self.daemonized = true;144return callback && callback();145}146catch (err) {147console.error(err);148return callback && callback(err);149}150});151});152};153154SystemVAdapter.prototype.add = function add(file, options, callback) {155forever.config.set('root', path.join('/var', 'local', 'foreverd'));156//157// Add descriptor to our service list158// this is just a json file in $root/services/*.json159//160var filePath, service = {161file: file,162options: options || {}163};164165options.appendLog = true;166filePath = path.join(forever.config.get('root'), 'services', options.uid + '.json');167168fs.writeFile(filePath, JSON.stringify(service), function (err) {169return callback && callback(err);170});171};172173SystemVAdapter.prototype.list = function list(callback) {174forever.config.set('root', path.join('/var', 'local', 'foreverd'));175176var sockPath = path.join(forever.config.get('root'), 'foreverd.sock'),177client;178179client = dnode.connect(sockPath, function onConnect(remote, client) {180if (callback) {181callback(false, remote.applications);182}183184client.end();185});186187client.on('error', function onError(err) {188if (err.code === 'ECONNREFUSED') {189try {190fs.unlinkSync(fullPath);191}192catch (ex) { }193return callback && callback(false, []);194}195196return callback && callback(err);197});198};199200