Skip to content

Commit

Permalink
bug: remove .lock file when abe restart to unlock processes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 20, 2016
1 parent a1e0acf commit 1013c28
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/cli/extend/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import hooks from './hooks'
import plugins from './plugins'
import process from './process'
import * as lock from './lock'

export {
hooks
,plugins
,process
,lock
}
46 changes: 46 additions & 0 deletions src/cli/extend/lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import process from 'child_process'
import path from 'path'
import fs from 'fs'

import {
config
,abeExtend
} from '../'

export function create(name) {
let lockFile = path.join(config.root, `abe-process-${name}.lock`)
try {
var stats = fs.statSync(lockFile)
if (stats.isFile()) {
console.log(`cannot lock process ${name} already running`)
return false
}
}catch(err) {
fs.writeFileSync(lockFile, '', {encoding: 'utf8'})
}

return true
}

export function remove(name) {
let lockFile = path.join(config.root, `abe-process-${name}.lock`)
try {
var stats = fs.statSync(lockFile)
if (stats.isFile()) {
fs.unlinkSync(lockFile)
return true
}
}catch(err) {

}
return false
}

export function deleteAll() {
var files = fs.readdirSync(config.root)
Array.prototype.forEach.call(files, (file) => {
if (file.indexOf('abe-process') > -1 && file.indexOf('.lock') > -1) {
fs.unlinkSync(path.join(config.root, file))
}
})
}
13 changes: 3 additions & 10 deletions src/cli/extend/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ var abeProcess = function(name, args = []) {
args = prepend(`ABE_WEBSITE=${config.root}`, args)
args = prepend(`ABEJS_PATH=${__dirname}/../../../dist`, args)

let lockFile = path.join(config.root, `abe-process-${name}.lock`)
try {
var stats = fse.statSync(lockFile)
if (stats.isFile()) {
console.log(`cannot start process ${name} already running`)
return false
}
}catch(err) {
fs.writeFileSync(lockFile, '', {encoding: 'utf8'})
if (!abeExtend.lock.create(name)) {
return false
}

var proc
Expand All @@ -50,7 +43,7 @@ var abeProcess = function(name, args = []) {

if(typeof proc !== 'undefined' && proc !== null) {
proc.on('message', function( msg ) {
fs.unlinkSync(lockFile)
abeExtend.lock.remove(name)
proc.kill()
});
return true
Expand Down
2 changes: 2 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ if(config.port) abePort = config.port
if(process.env.PORT) abePort = process.env.PORT
config.set({webport: process.env.WEBPORT ? process.env.WEBPORT : 8081})

abeExtend.lock.deleteAll() // delete all process .lock when abe start

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

var html = exphbs.create({
Expand Down

0 comments on commit 1013c28

Please sign in to comment.