Skip to content

Commit

Permalink
0.3.3 Log periods over 24 days work, refactored file-data conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldrudell committed Feb 2, 2013
1 parent 898548c commit 3be4fb2
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 44 deletions.
1 change: 1 addition & 0 deletions ChangeLog
@@ -1,3 +1,4 @@
2013-02-01 0.3.3 Log periods over 24 days work, refactored file-data conversion
2013-01-28 0.3.2 remove debug
2013-01-25 0.3.1 Node God as four processes, rotates log on size and time, License, ChangeLog
2013-01-20 0.3.0 Node God as four processes, rotates log on size and time, License, ChangeLog
Expand Down
90 changes: 90 additions & 0 deletions lib/anytimer.js
@@ -0,0 +1,90 @@
// anytimer.js
// Provide equivalents for setTimeout and SetInterval facilitating any time period
// © 2013 Harald Rudell <harald@allgoodapps.com> MIT License

exports.AnyTimer = AnyTimer

var TIMEOUT_MAX = 2147483647
var time1day = 24 * 60 * 60 * 1e3

/*
Provide timer and interval
fn(): function
timeval: number: unit ms
isInterval: boolean
JavaScript setTimeout and setInterval can only handle periods of about 24 days
*/
function AnyTimer(fn, timeval, isInterval) {
this.clear = clear
var isClear
if (!(timeval >= 1)) timeval = 1 // ensure number
var baseTime = Date.now()
var nextTargetTime = baseTime + timeval
var timer
var interval

if (isInterval && timeval <= TIMEOUT_MAX) { // this actually works with setInterval
interval = setInterval(fn, timeval)
} else timerFire()

function timerFire() {
var remaining = nextTargetTime - Date.now()
if (remaining <= 0) {
timer = null
fn()
if (isInterval && !isClear) {
nextTargetTime += isInterval
setTimer(nextTargetTime - Date.now())
}
} else setTimer(remaining)
}

function setTimer(remaining) {
if (remaining >= time1day) remaining = time1day
timer = setTimeout(timerFire, remaining)
}

function clear() {
isClear = true
if (timer) {
var t = timer
timer = null
clearTimeout(t)
}
if (interval) {
var i = interval
interval = null
clearInterval(i)
}
}
}

function AnyInterval(fn, timeval) {
this.clearInterval = clearInterval
var baseTime = Date.now()
var nextTargetTime = baseTime + timeval
var timer

timerFire()

function timerFire() {
var now = Date.now()
var remaining = targetTime - now
if (remaining <= 0) {
timer = null
fn()
} else {
if (remaining >= time1day) remaining = time1day
timer = setTimeout(setTimer, remaining)
}
}

function clearInterval() {
if (timer) {
var t = timer
timer = null
t.clearTimeout()
}
}
}
61 changes: 29 additions & 32 deletions lib/apploader.js
Expand Up @@ -124,46 +124,43 @@ function AppLoader(appLoaderOpts, errEmitter) {
id: appId,
name: fileApp.name,
}
var stateNo = states.indexOf(fileApp.state)
o.state = states[stateNo !== -1 ? stateNo : 0]
o.folder = path.resolve(appLoaderOpts.parentFolder, String(fileApp.folder || o.id))
if (Array.isArray(fileApp.start)) {
o.start = []
fileApp.start.forEach(function (v) {
o.start.push(String(v))
})
} else o.start = ['node', o.folder + '/app']
if (Array.isArray(fileApp.debug)) {
o.debug = []
fileApp.debug.forEach(function (v) {
o.debug.push(String(v))
})
} else o.debug = ['node', '--debug-brk', o.folder + '/app']
if (typeof fileApp.watchFiles === 'string' || fileApp.watchFiles instanceof RegExp) o.watchFiles = [fileApp.watchFiles]
else {
o.watchFiles = []
if (Array.isArray(fileApp.watchFiles))
fileApp.watchFiles.forEach(function (v) {
if (v instanceof RegExp) o.watchFiles.push(v)
else o.watchFiles.push(String(v))
})
}
o.watchFileExclude = []
if (Array.isArray(fileApp.watchFileExclude)) {
fileApp.watchFileExclude.forEach(function (v) {
o.watchFileExclude.push(String(v))
})
}

o.state = states[fileApp.state || 0] || states[0]

o.folder = path.resolve(appLoaderOpts.parentFolder, String(fileApp.folder || o.id)) // absolute path

o.start = Array.isArray(fileApp.start) ? fileApp.start.map(makeString) :
['node', path.join(o.folder, 'app')]

o.debug = Array.isArray(fileApp.debug) ? fileApp.debug.map(makeString) :
['node', '--debug-brk', path.join(o.folder, 'app')]

o.watchFiles = typeof fileApp.watchFiles === 'string' || fileApp.watchFiles instanceof RegExp ? [fileApp.watchFiles] :
Array.isArray(fileApp.watchFiles) ? fileApp.watchFiles.map(makeStringOrRegExp) :
[]

o.watchFileExclude = Array.isArray(fileApp.watchFileExclude) ? fileApp.watchFileExclude.map(makeString) :
[]

o.watchCopy = {}
for (var dest in fileApp.watchCopy) {
for (var dest in fileApp.watchCopy)
o.watchCopy[
path.resolve(o.folder, String(dest))] =
path.resolve(o.folder, String(fileApp.watchCopy[dest]))
}

o.signal = fileApp.signal != null ? !!fileApp.signal : appLoaderOpts.signal

if (fileApp.launchBrowser && !appLoaderOpts.launchedBrowser) launchBrowser = fileApp.launchBrowser

return o
}

function makeString(v) {
return String(v)
}

function makeStringOrRegExp(v) {
return v instanceof RegExp ? v : String(v)
}
}
}
10 changes: 5 additions & 5 deletions lib/perioder.js
Expand Up @@ -2,6 +2,7 @@
// Invoke a function on a certain schedule
// © Harald Rudell 2013 MIT License

var anytimer = require('./anytimer')
var linearcalendar = require('./linearcalendar')
// http://nodejs.org/api/util.html
var util = require('util')
Expand Down Expand Up @@ -72,8 +73,7 @@ function TimeEmitter(opts) {
if (timer) {
var t = timer
timer = null
if (isTimeout) clearTimeout(t)
else clearInterval(t)
t.clear()
}
}
this.getState = function getState() {
Expand Down Expand Up @@ -117,7 +117,7 @@ function TimeEmitter(opts) {
var inPeriod = lastTimerSet % periodTimeval // time elapsed in current epoch-based period
var atTime = (opts.at % periodTimeval) * period.atUnit // at time in each period
var timeLeft = atTime - inPeriod // time left to the next at time in this period
if (timeLeft < 0) timeLeft += periodTimeval // it's passed, go to the next period
if (timeLeft <= 0) timeLeft += periodTimeval // it's passed, go to the next period
lastTimeval = timeLeft
} else lastTimeval = periodTimeval
break
Expand All @@ -130,7 +130,7 @@ function TimeEmitter(opts) {
var period1 = linearcalendar.getTimeval(ymPeriodStart + ymPeriod)
var period2 = linearcalendar.getTimeval(ymPeriodStart + 2 * ymPeriod)
var atLeft = period0 + opts.at * period.atUnit - lastTimerSet
if (atLeft < 0) {
if (atLeft <= 0) {
var nextAt = period1 + opts.at * period.atUnit
if (nextAt >= period2) nextAt = period2 - time1day
atLeft = nextAt - lastTimerSet
Expand All @@ -156,7 +156,7 @@ function TimeEmitter(opts) {
break
}
// rig the timer
timer = (isTimeout = !isInterval) ? setTimeout(fire, lastTimeval) : setInterval(fire, lastTimeval)
timer = new anytimer.AnyTimer(fire, lastTimeval, isTimeout = !isInterval)
}
}
util.inherits(TimeEmitter, events.EventEmitter)
Expand Down
24 changes: 18 additions & 6 deletions lib/rotatedstream.js
Expand Up @@ -67,7 +67,7 @@ function RotatedStream(opts) {
if (!e) {
this.writable = true
periodTimer = new perioder.TimeEmitter(opts.rotating || defaults.rotating)
.on('time', rotate)
.on('time', rotateTimer)
} else process.nextTick(function () {
emitError(e)
})
Expand All @@ -94,7 +94,10 @@ function RotatedStream(opts) {
else {
didCb = true
stream.write.apply(stream, args)
if (stream.bytesWritten > maxBytesWritten) rotate()
if (stream.bytesWritten > maxBytesWritten) {
//** stream.write('rotateSize')
rotate()
}
}
}
if (!didCb) {
Expand All @@ -103,12 +106,19 @@ function RotatedStream(opts) {
}
}

function rotateTimer() {
//** if (self.writable) stream.write('rotatedTimer')
rotate()
}
// rotate the log
function rotate(cb) {
var retryCount
var newName

if (self.writable && didWrite && !rotating) { // we wrote something to the log, and are not in process of rotating
rotating = true
var retryCount = 0
var newName = getRotatedName()
retryCount = 0
newName = getRotatedName()
fs.stat(newName, doesNewNameExist) // see if the new filename already exist
} else end()

Expand All @@ -125,6 +135,7 @@ function RotatedStream(opts) {
}

function retry() {
//** stream.write('retry')
if (self.writable) {
newName = getRotatedName()
fs.stat(newName, doesNewNameExist) // see if the new filename already exist
Expand All @@ -146,6 +157,7 @@ function RotatedStream(opts) {
}

function flushBuffer(err) {
//** stream.write('flushBuffer')
buffer = null
rotating = false
end(err)
Expand All @@ -167,7 +179,7 @@ function RotatedStream(opts) {
var p = periodTimer
periodTimer = null
p.cancel()
p.removeListener('time', rotate)
p.removeListener('time', rotateTimer)
}
closeStream(null, cb)
}
Expand Down Expand Up @@ -262,4 +274,4 @@ function getType(path1) {
if (stats.isDirectory()) result = 1
}
return result
}
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"tools",
"operations"
],
"version": "0.3.2",
"version": "0.3.3",
"contributors": [
{
"name": "Harald Rudell",
Expand Down

0 comments on commit 3be4fb2

Please sign in to comment.