Permalink
Browse files

Merge branch 'dev' of https://github.com/mschwartz/SilkJS into dev

Conflicts:
	src/Makefile.osx
  • Loading branch information...
1 parent 1214d59 commit 5a11f027bdf6020cfb13ba185f83b16ee570b6af @mschwartz committed Jul 30, 2012
View
@@ -1,10 +1,16 @@
UNAME := $(shell uname -s)
+MAKEFILE=Makefile
+RELEASE = 0
ifeq ($(UNAME),Darwin)
MAKEFILE=Makefile.osx
else
- MAKEFILE=Makefile
+ RELEASE := $(shell lsb_release -sr)
endif
+ifeq ($(RELEASE),11)
+ MAKEFILE = Makefile.sles
+endif
+
all:
cd src && make -f$(MAKEFILE)
View
@@ -1,4 +1,4 @@
-/*
+/*
* CommonJS require 1.1 implementation
*/
@@ -48,7 +48,7 @@
return false;
}
if (module.substr(0,1) == '/' || module.substr(0,2) == './' || module.substr(0,3) == '../') {
- return tryFile(module) || tryFile(module + '.js' || tryFile(module + '.coffee'));
+ return tryFile(module) || tryFile(module + '.js') || tryFile(module + '.coffee') || tryFile(module + '.so');
}
else {
var paths = require.path;
@@ -58,7 +58,7 @@
path += '/';
}
path += module;
- var found = tryFile(path) || tryFile(path+'.js') || tryFile(path + '.coffee');
+ var found = tryFile(path) || tryFile(path+'.js') || tryFile(path + '.coffee') || tryFile(path + '.so');
if (found) {
return found;
}
@@ -67,12 +67,12 @@
throw 'Could not locate require file ' + module;
}
// coffeescript support
- var suffix = '.coffee',
- suffixLen = suffix.length;
+ var coffeeRegEx = /\.coffee$/,
+ soRegEx = /\.so/;
function loadFile(modulePath) {
var contents = fs.readFile(modulePath);
- if (modulePath.indexOf(suffix, modulePath.length - suffixLen) !== -1) {
+ if (coffeeRegEx.test(modulePath)) {
contents = CoffeeScript.compile(contents, { bare: true });
}
return contents;
@@ -86,6 +86,10 @@
if (require.cache[modulePath]) {
return require.cache[modulePath];
}
+ if (soRegEx.test(modulePath)) {
+ require.cache[modulePath] = loadDll(modulePath);
+ return require.cache[modulePath];
+ }
var content = loadFile(modulePath);
require.dirStack.push(require.fsPath);
var fsPath = modulePath.split('/');
@@ -126,8 +130,8 @@
'./',
'modules',
'/usr/local/silkjs',
- '/usr/local/silkjs/modules',
'/usr/local/silkjs/contrib',
+ '/usr/local/silkjs/modules',
'/usr/share/silkjs/modules'
];
}());
View
@@ -0,0 +1,124 @@
+/** @ignore */
+/**
+ * @fileoverview
+ * <p>This class provides interfaces to Halma's built-in onStart, onStop, and onCodeUpdate functionality, as well
+ * as endRequest and abortRequest methods.</p>
+ */
+/**
+ * @namespace
+ *
+ * <p>This class provides interfaces to Halma's built-in onStart, onStop, and onCodeUpdate functionality, as well
+ * as endRequest and abortRequest methods.</p>
+ *
+ * <p>This class provides interfaces to Halma's built-in onStart and onStop functionality </p>
+ *
+ * <p>For onStart and onStop, arrays of functions are maintained. An included (repository) class/file
+ * can add onStart/onStop handlers by calling Server.addOnStart() and Server.addOnStop(). This is
+ * useful, for example, for MsUserManager to load all the user records from the DB when Helma starts
+ * up, and storing all the user records back to the DB when Helma exits.</p>
+ *
+ */
+Server = (function() {
+ var mimeTypes = require('MimeTypes');
+
+ var started = false;
+ var onStartFuncs = [];
+ var onStopFuncs = [];
+
+ /** @scope Server */
+ return {
+
+ /**
+ * Add a function to be called when the server first starts.
+ *
+ * @param {mixed} config Config object containing name and func key/values
+ * @returns {void} nothing
+ */
+ addOnStart: function(config) {
+ config = Util.isFunction(config) ? {
+ name: 'unnamed',
+ func: config
+ } : config;
+ if (started) {
+ config.func();
+ }
+ else {
+ onStartFuncs.push(config);
+ }
+ },
+
+ /**
+ * Call all registered onStart functions.
+ *
+ * @returns {void} nothing
+ */
+ onStart: function() {
+ var onstart_time = Util.currentTimeMillis();
+ onStartFuncs.each(function(config) {
+ console.log('Starting ' + config.name);
+ var start_time = Util.currentTimeMillis();
+ var ret = config.func();
+ var elapsed = Util.currentTimeMillis() - start_time;
+ if (ret) {
+ console.log(['--> ', ret, ' in ', elapsed, 'ms'].join(''));
+ }
+ });
+ var onstart_elapsed = Util.currentTimeMillis() - onstart_time;
+ started = true;
+ },
+
+ /**
+ * Add a function to be called when Helma shuts down.
+ *
+ * @param {function} function to be called when Helma shuts down.
+ * @returns {void} nothing
+ */
+ addOnStop: function(func) {
+ onStopFuncs.push(func);
+ },
+
+ /**
+ * Call all registered onStop functions.
+ *
+ * @returns {void} nothing
+ */
+ onStop: function() {
+ var onstop_time = Util.currentTimeMillis();
+ onStopFuncs.each(function(func) {
+ func();
+ });
+ var onstop_elapsed = Util.currentTimeMillis() - onstop_time;
+ },
+
+ /**
+ * <p>Successfully end the current request.</p>
+ *
+ * <p>NOTE: all requests should call this upon successful completion to assure
+ * any SQL transactions are committed and any dangling Sempahore locks are unlocked.</p>
+ */
+ endRequest: function() {
+ // Cache.unlockAll();
+ throw 'RES.STOP';
+ },
+
+ /**
+ * <p>Abort the current request.</p>
+ *
+ * <p>NOTE: all requests should call this upon error to assure
+ * any SQL transactions are rolled back and any dangling Sempahore locks are unlocked.</p>
+ */
+ abortRequest: function() {
+ // Cache.unlockAll();
+ res.stop();
+ },
+
+ sendFile: function(path) {
+ res.status = 200;
+ var parts = path.split('.');
+ var extension = parts.pop().toLowerCase();
+ res.contentType = mimeTypes[extension] || 'text/plain';
+ res.sendFile(path);
+ Server.endRequest();
+ }
+ };
+})();
View
@@ -3,9 +3,11 @@
HttpChild = (function() {
var requestsHandled;
- var mimeTypes = require('MimeTypes');
+ var mimeTypes = require('MimeTypes'),
+ time = require('builtin/time');
function errorHandler(e) {
+ console.log('errorHandler');
console.dir(e);
console.log(e.stack);
var spaces = ' ';
@@ -320,6 +322,7 @@ HttpChild = (function() {
});
return found;
}
+ var realDocumentRoot = fs.realpath(Config.documentRoot);
function handleRequest() {
req.script_path = req.uri;
delete req.path_info;
@@ -377,7 +380,7 @@ HttpChild = (function() {
notFound();
}
- req.script_path = fn.replace(/index\..*$/, '').replace(fs.realpath(Config.documentRoot), '');
+ req.script_path = fn.replace(/index\..*$/, '').replace(realDocumentRoot, '');
res.status = 200;
req.path = fn;
parts = fn.split('.');
@@ -394,19 +397,14 @@ HttpChild = (function() {
}
}
- // semaphore for locking around accept()
- var USE_FLOCK = true;
- var lock = USE_FLOCK ? function(lockfd) { fs.flock(lockfd, fs.LOCK_EX); } : function(lockfd) { fs.lockf(lockfd, fs.F_LOCK); };
- var unlock = USE_FLOCK ? function(lockfd) { fs.flock(lockfd, fs.LOCK_UN); } : function(lockfd) { fs.lockf(lockfd, fs.F_ULOCK); };
-
return {
requestHandler: null, // called at start of each request
endRequest: null, // called at end of each request
onStart: null,
getCoffeeScript: function(fn) {
return coffee_cache[fn];
},
- run: function(serverSocket, pid) {
+ run: function(serverSocket, pid, control) {
// randomize the random number generator, just in case.
var bits = pid % Config.numChildren;
for (var b=0; b<bits; b++) {
@@ -425,21 +423,21 @@ HttpChild = (function() {
var requestHandler = HttpChild.requestHandler;
var endRequest = HttpChild.endRequest;
requestsHandled = 0;
- var lockfd = fs.open(Config.lockFile, fs.O_RDONLY);
while (requestsHandled < REQUESTS_PER_CHILD) {
- lock(lockfd);
+ async.write(control, 'r', 1);
+ async.read(control, 1);
var sock = net.accept(serverSocket);
- unlock(lockfd);
var keepAlive = true;
while (keepAlive) {
if (++requestsHandled > REQUESTS_PER_CHILD) {
keepAlive = false;
}
- var rstart = process.rusage();
+ var start_time = time.getrusage();
try {
if (!req.init(sock)) {
break;
}
+ // console.log(time.getrusage() - start_time);
keepAlive = res.init(sock, keepAlive, requestsHandled);
// execute a pure JavaScript handler, if provided.
if (requestHandler) {
@@ -460,15 +458,16 @@ HttpChild = (function() {
res.data = {};
res.flush();
res.reset();
- // this logfile.write() reduces # requests/sec by 5000!
- var elapsed = ('' + (process.rusage().time - rstart.time)).substr(0,8);
+ var end_time = time.getrusage();
+ var elapsed = end_time - start_time;
+ elapsed = '' + elapsed;
+ elapsed = elapsed.substr(0, 8);
logfile.write(req.remote_addr + ' ' + req.method + ' ' + req.uri + ' completed in ' + elapsed + 's\n');
}
- req.close();
net.close(sock);
+ req.close();
v8.gc();
}
- fs.close(lockfd);
res.close();
}
};
Oops, something went wrong.

0 comments on commit 5a11f02

Please sign in to comment.