1
0
Fork 0

more actions cleanup

This commit is contained in:
Frank Celler 2012-03-05 17:35:02 +01:00
parent 3611799e06
commit 73a71fe68e
19 changed files with 317 additions and 199 deletions

View File

@ -44,7 +44,7 @@ using namespace triagens::rest;
using namespace triagens::avocado;
// -----------------------------------------------------------------------------
// --SECTION-- class ActionDisptacherThread
// --SECTION-- class ActionDispatcherThread
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
@ -56,29 +56,23 @@ using namespace triagens::avocado;
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief action path
////////////////////////////////////////////////////////////////////////////////
JSLoader* ActionDisptacherThread::_actionLoader = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief startup path
////////////////////////////////////////////////////////////////////////////////
JSLoader* ActionDisptacherThread::_startupLoader = 0;
JSLoader* ActionDispatcherThread::_startupLoader = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief vocbase
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* ActionDisptacherThread::_vocbase = 0;
TRI_vocbase_t* ActionDispatcherThread::_vocbase = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief modules path
////////////////////////////////////////////////////////////////////////////////
string ActionDisptacherThread::_startupModules;
string ActionDispatcherThread::_startupModules;
////////////////////////////////////////////////////////////////////////////////
/// @}
@ -97,19 +91,22 @@ string ActionDisptacherThread::_startupModules;
/// @brief constructs a new dispatcher thread
////////////////////////////////////////////////////////////////////////////////
ActionDisptacherThread::ActionDisptacherThread (DispatcherQueue* queue, string const& actionQeue)
ActionDispatcherThread::ActionDispatcherThread (DispatcherQueue* queue,
string const& actionQueue,
JSLoader* actionLoader)
: DispatcherThread(queue),
_report(false),
_isolate(0),
_context(),
_actionQeue(actionQeue) {
_actionQueue(actionQueue),
_actionLoader(actionLoader) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destructs a dispatcher thread
////////////////////////////////////////////////////////////////////////////////
ActionDisptacherThread::~ActionDisptacherThread () {
ActionDispatcherThread::~ActionDispatcherThread () {
}
////////////////////////////////////////////////////////////////////////////////
@ -129,7 +126,7 @@ ActionDisptacherThread::~ActionDisptacherThread () {
/// {@inheritDoc}
////////////////////////////////////////////////////////////////////////////////
void ActionDisptacherThread::reportStatus () {
void ActionDispatcherThread::reportStatus () {
_report = true;
}
@ -137,7 +134,7 @@ void ActionDisptacherThread::reportStatus () {
/// {@inheritDoc}
////////////////////////////////////////////////////////////////////////////////
void ActionDisptacherThread::tick () {
void ActionDispatcherThread::tick () {
while(!v8::V8::IdleNotification()) {
}
@ -174,7 +171,7 @@ void ActionDisptacherThread::tick () {
/// {@inheritDoc}
////////////////////////////////////////////////////////////////////////////////
void ActionDisptacherThread::run () {
void ActionDispatcherThread::run () {
initialise();
_isolate->Enter();
@ -206,7 +203,7 @@ void ActionDisptacherThread::run () {
/// returns the action loader
////////////////////////////////////////////////////////////////////////////////
JSLoader* ActionDisptacherThread::actionLoader () {
JSLoader* ActionDispatcherThread::actionLoader () {
return _actionLoader;
}
@ -227,7 +224,7 @@ JSLoader* ActionDisptacherThread::actionLoader () {
/// @brief initialises the isolate and context
////////////////////////////////////////////////////////////////////////////////
void ActionDisptacherThread::initialise () {
void ActionDispatcherThread::initialise () {
bool ok;
char const* files[] = { "bootstrap/modules.js",
"bootstrap/print.js",
@ -254,7 +251,7 @@ void ActionDisptacherThread::initialise () {
_context->Enter();
TRI_InitV8VocBridge(_context, _vocbase);
TRI_InitV8Actions(_context, _actionQeue.c_str());
TRI_InitV8Actions(_context, _actionQueue.c_str());
TRI_InitV8Conversions(_context);
TRI_InitV8Utils(_context, _startupModules);
TRI_InitV8Shell(_context);
@ -279,7 +276,7 @@ void ActionDisptacherThread::initialise () {
LOGGER_WARNING << "no action loader has been defined";
}
else {
ok = actionLoader()->loadAllScripts(_context);
ok = actionLoader()->executeAllScripts(_context);
if (! ok) {
LOGGER_FATAL << "cannot load actions from directory '" << loader->getDirectory() << "'";

View File

@ -36,7 +36,7 @@
#include "VocBase/vocbase.h"
// -----------------------------------------------------------------------------
// --SECTION-- class ActionDisptacherThread
// --SECTION-- class ActionDispatcherThread
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
@ -51,9 +51,10 @@ namespace triagens {
/// @brief dispatcher thread
////////////////////////////////////////////////////////////////////////////////
class ActionDisptacherThread : public rest::DispatcherThread {
ActionDisptacherThread (ActionDisptacherThread const&);
ActionDisptacherThread& operator= (ActionDisptacherThread const&);
class ActionDispatcherThread : public rest::DispatcherThread {
private:
ActionDispatcherThread (ActionDispatcherThread const&);
ActionDispatcherThread& operator= (ActionDispatcherThread const&);
////////////////////////////////////////////////////////////////////////////////
/// @}
@ -70,12 +71,6 @@ namespace triagens {
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief action path
////////////////////////////////////////////////////////////////////////////////
static JSLoader* _actionLoader;
////////////////////////////////////////////////////////////////////////////////
/// @brief startup path
////////////////////////////////////////////////////////////////////////////////
@ -113,13 +108,13 @@ namespace triagens {
/// @brief constructs a new dispatcher thread
////////////////////////////////////////////////////////////////////////////////
ActionDisptacherThread (rest::DispatcherQueue* queue, string const& actionQeue);
ActionDispatcherThread (rest::DispatcherQueue*, string const& actionQueue, JSLoader*);
////////////////////////////////////////////////////////////////////////////////
/// @brief destructs a dispatcher thread
////////////////////////////////////////////////////////////////////////////////
~ActionDisptacherThread ();
~ActionDispatcherThread ();
////////////////////////////////////////////////////////////////////////////////
/// @}
@ -248,7 +243,13 @@ namespace triagens {
/// @brief action context
////////////////////////////////////////////////////////////////////////////////
std::string _actionQeue;
std::string _actionQueue;
////////////////////////////////////////////////////////////////////////////////
/// @brief action path
////////////////////////////////////////////////////////////////////////////////
JSLoader* _actionLoader;
};
}
}

View File

@ -116,16 +116,16 @@ static JSLoader SystemActionLoader;
/// @brief action dispatcher thread creator
////////////////////////////////////////////////////////////////////////////////
static DispatcherThread* ActionDisptacherThreadCreator (DispatcherQueue* queue) {
return new ActionDisptacherThread(queue, "user");
static DispatcherThread* ClientActionDispatcherThreadCreator (DispatcherQueue* queue) {
return new ActionDispatcherThread(queue, "CLIENT", &ActionLoader);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief system action dispatcher thread creator
////////////////////////////////////////////////////////////////////////////////
static DispatcherThread* SystemActionDisptacherThreadCreator (DispatcherQueue* queue) {
return new ActionDisptacherThread(queue, "admin");
static DispatcherThread* SystemActionDispatcherThreadCreator (DispatcherQueue* queue) {
return new ActionDispatcherThread(queue, "SYSTEM", &SystemActionLoader);
}
////////////////////////////////////////////////////////////////////////////////
@ -384,12 +384,15 @@ void AvocadoServer::buildApplicationServer () {
if (_actionPath.empty()) {
char* path = TRI_Concatenate2File(_databasePath.c_str(), "_ACTIONS");
string pathString(path);
if (path) {
// memleak otherwise
TRI_Free(path);
if (path == 0) {
LOGGER_FATAL << "out-of-memory";
exit(EXIT_FAILURE);
}
string pathString(path);
TRI_FreeString(path);
if (! TRI_IsDirectory(pathString.c_str())) {
bool ok = TRI_ExistsFile(pathString.c_str());
@ -483,12 +486,9 @@ int AvocadoServer::startupServer () {
LOGGER_INFO << "using JavaScript modules path '" << _startupModules << "'";
ActionDisptacherThread::_actionLoader = &ActionLoader;
ActionDisptacherThread::_startupLoader = &StartupLoader;
ActionDisptacherThread::_vocbase = _vocbase;
ActionDisptacherThread::_startupModules = _startupModules;
// TODO SystemActionDisptacherThread::_actionLoader = &SystemActionLoader;
ActionDispatcherThread::_startupLoader = &StartupLoader;
ActionDispatcherThread::_vocbase = _vocbase;
ActionDispatcherThread::_startupModules = _startupModules;
// .............................................................................
// create the various parts of the Avocado server
@ -508,8 +508,8 @@ int AvocadoServer::startupServer () {
_actionThreads = 1;
}
safe_cast<DispatcherImpl*>(dispatcher)->addQueue("CLIENT", ActionDisptacherThreadCreator, _actionThreads);
safe_cast<DispatcherImpl*>(dispatcher)->addQueue("SYSTEM", SystemActionDisptacherThreadCreator, 2);
safe_cast<DispatcherImpl*>(dispatcher)->addQueue("CLIENT", ClientActionDispatcherThreadCreator, _actionThreads);
safe_cast<DispatcherImpl*>(dispatcher)->addQueue("SYSTEM", SystemActionDispatcherThreadCreator, 2);
// .............................................................................
// create a http server and http handler factory
@ -547,8 +547,6 @@ int AvocadoServer::startupServer () {
adminFactory->addPrefixHandler(RestVocbaseBaseHandler::DOCUMENT_PATH, RestHandlerCreator<RestCollectionHandler>::createData<TRI_vocbase_t*>, _vocbase);
adminFactory->addPrefixHandler("/", RestHandlerCreator<RestActionHandler>::createData<TRI_vocbase_t*>, _vocbase);
adminFactory->addHandler("/", RedirectHandler::create, (void*) "/admin/index.html");
_adminHttpServer = _applicationHttpServer->buildServer(adminFactory, adminPorts);
}

View File

@ -165,6 +165,42 @@ bool JSLoader::loadAllScripts (v8::Persistent<v8::Context> context) {
return TRI_LoadJavaScriptDirectory(context, _directory.c_str());
}
////////////////////////////////////////////////////////////////////////////////
/// @brief loads a named script
////////////////////////////////////////////////////////////////////////////////
bool JSLoader::executeScript (v8::Persistent<v8::Context> context, string const& name) {
v8::HandleScope scope;
findScript(name);
map<string, string>::iterator i = _scripts.find(name);
if (i == _scripts.end()) {
return false;
}
string content = "(function() { " + i->second + "/* end-of-file '" + name + "' */ })()";
return TRI_ExecuteStringVocBase(context,
v8::String::New(content.c_str()),
v8::String::New(name.c_str()),
false,
true);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes all scripts
////////////////////////////////////////////////////////////////////////////////
bool JSLoader::executeAllScripts (v8::Persistent<v8::Context> context) {
if (_directory.empty()) {
return true;
}
return TRI_ExecuteJavaScriptDirectory(context, _directory.c_str());
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -124,6 +124,18 @@ namespace triagens {
bool loadAllScripts (v8::Persistent<v8::Context>);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a named script
////////////////////////////////////////////////////////////////////////////////
bool executeScript (v8::Persistent<v8::Context>, string const& name);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes all scripts
////////////////////////////////////////////////////////////////////////////////
bool executeAllScripts (v8::Persistent<v8::Context>);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -544,7 +544,7 @@ HttpResponse* TRI_ExecuteActionVocBase (TRI_vocbase_t* vocbase,
/// @brief stores the V8 actions function inside the global variable
////////////////////////////////////////////////////////////////////////////////
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQeue) {
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQueue) {
v8::HandleScope scope;
// check the isolate
@ -561,7 +561,7 @@ void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQeue)
// .............................................................................
context->Global()->Set(v8::String::New("SYS_ACTION_QUEUE"),
v8::String::New(actionQeue),
v8::String::New(actionQueue),
v8::ReadOnly);
// .............................................................................

View File

@ -243,7 +243,7 @@ triagens::rest::HttpResponse* TRI_ExecuteActionVocBase (TRI_vocbase_t* vocbase,
/// @brief stores the V8 actions function inside the global variable
////////////////////////////////////////////////////////////////////////////////
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQeue);
void TRI_InitV8Actions (v8::Handle<v8::Context> context, char const* actionQueue);
////////////////////////////////////////////////////////////////////////////////
/// @}

View File

@ -566,6 +566,122 @@ bool TRI_ExecuteOrderExecutionContext (TRI_js_exec_context_t context, int* r) {
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief reads/execute a file into/in the current context
////////////////////////////////////////////////////////////////////////////////
static bool LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filename, bool execute) {
v8::HandleScope handleScope;
v8::TryCatch tryCatch;
char* content = TRI_SlurpFile(filename);
if (content == 0) {
LOG_TRACE("cannot loaded java script file '%s': %s", filename, TRI_last_error());
return false;
}
if (execute) {
char* contentWrapper = TRI_Concatenate5String("(function() { ",
content,
"/* end-of-file '",
filename,
"' */ })()");
TRI_FreeString(content);
content = contentWrapper;
}
v8::Handle<v8::String> name = v8::String::New(filename);
v8::Handle<v8::String> source = v8::String::New(content);
TRI_FreeString(content);
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
// compilation failed, print errors that happened during compilation
if (script.IsEmpty()) {
LOG_ERROR("cannot compile java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;
}
// execute script
v8::Handle<v8::Value> result = script->Run();
if (result.IsEmpty()) {
assert(tryCatch.HasCaught());
// print errors that happened during execution
LOG_ERROR("cannot execute java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;
}
LOG_TRACE("loaded java script file: '%s'", filename);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief reads all files from a directory into the current context
////////////////////////////////////////////////////////////////////////////////
static bool LoadJavaScriptDirectory (v8::Handle<v8::Context> context, char const* path, bool execute) {
TRI_vector_string_t files;
bool result;
regex_t re;
size_t i;
LOG_TRACE("loading java script directory: '%s'", path);
files = TRI_FilesDirectory(path);
regcomp(&re, "^(.*)\\.js$", REG_ICASE | REG_EXTENDED);
result = true;
for (i = 0; i < files._length; ++i) {
bool ok;
char const* filename;
char* full;
filename = files._buffer[i];
if (! regexec(&re, filename, 0, 0, 0) == 0) {
continue;
}
full = TRI_Concatenate2File(path, filename);
ok = LoadJavaScriptFile(context, full, execute);
TRI_FreeString(full);
result = result && ok;
}
TRI_DestroyVectorString(&files);
regfree(&re);
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- JS functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup V8Utils
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a script
///
@ -1241,10 +1357,10 @@ void TRI_PrintV8Exception (v8::TryCatch* tryCatch) {
// V8 didn't provide any extra information about this error; just print the exception.
if (message.IsEmpty()) {
if (exceptionString == 0) {
printf("JavaScript exception\n");
LOG_ERROR("JavaScript exception");
}
else {
printf("JavaScript exception: %s\n", exceptionString);
LOG_ERROR("JavaScript exception: %s", exceptionString);
}
}
else {
@ -1256,38 +1372,41 @@ void TRI_PrintV8Exception (v8::TryCatch* tryCatch) {
if (filenameString == 0) {
if (exceptionString == 0) {
printf("exception\n");
LOG_ERROR("exception");
}
else {
printf("exception: %s\n", exceptionString);
LOG_ERROR("exception: %s", exceptionString);
}
}
else {
if (exceptionString == 0) {
printf("exception in file '%s' at %d,%d\n", filenameString, linenum, start);
LOG_ERROR("exception in file '%s' at %d,%d", filenameString, linenum, start);
}
else {
printf("exception in file '%s' at %d,%d: %s\n", filenameString, linenum, start, exceptionString);
LOG_ERROR("exception in file '%s' at %d,%d: %s", filenameString, linenum, start, exceptionString);
}
}
v8::String::Utf8Value sourceline(message->GetSourceLine());
if (*sourceline) {
printf("%s\n", *sourceline);
string l = *sourceline;
if (1 < start) {
printf("%*s", start - 1, "");
l += string(start - 1, ' ');
}
string e((size_t)(end - start + 1), '^');
printf("%s\n", e.c_str());
LOG_ERROR("%s", l.c_str());
l = string((size_t)(end - start + 1), '^');
LOG_ERROR("%s", l.c_str());
}
v8::String::Utf8Value stacktrace(tryCatch->StackTrace());
if (*stacktrace && stacktrace.length() > 0) {
printf("stacktrace:\n%s\n", *stacktrace);
LOG_ERROR("stacktrace: %s", *stacktrace);
}
}
}
@ -1297,46 +1416,7 @@ void TRI_PrintV8Exception (v8::TryCatch* tryCatch) {
////////////////////////////////////////////////////////////////////////////////
bool TRI_LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filename) {
v8::HandleScope handleScope;
v8::TryCatch tryCatch;
char* content = TRI_SlurpFile(filename);
if (content == 0) {
LOG_TRACE("cannot loaded java script file '%s': %s", filename, TRI_last_error());
return false;
}
v8::Handle<v8::String> name = v8::String::New(filename);
v8::Handle<v8::String> source = v8::String::New(content);
TRI_FreeString(content);
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
// compilation failed, print errors that happened during compilation
if (script.IsEmpty()) {
LOG_ERROR("cannot compile java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;
}
// execute script
v8::Handle<v8::Value> result = script->Run();
if (result.IsEmpty()) {
assert(tryCatch.HasCaught());
// print errors that happened during execution
LOG_ERROR("cannot execute java script file '%s'", filename);
TRI_ReportV8Exception(&tryCatch);
return false;
}
LOG_TRACE("loaded java script file: '%s'", filename);
return true;
return LoadJavaScriptFile(context, filename, false);
}
////////////////////////////////////////////////////////////////////////////////
@ -1344,43 +1424,23 @@ bool TRI_LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filena
////////////////////////////////////////////////////////////////////////////////
bool TRI_LoadJavaScriptDirectory (v8::Handle<v8::Context> context, char const* path) {
TRI_vector_string_t files;
bool result;
regex_t re;
size_t i;
return LoadJavaScriptDirectory(context, path, false);
}
LOG_TRACE("loading java script directory: '%s'", path);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a file in the current context
////////////////////////////////////////////////////////////////////////////////
files = TRI_FilesDirectory(path);
bool TRI_ExecuteJavaScriptFile (v8::Handle<v8::Context> context, char const* filename) {
return LoadJavaScriptFile(context, filename, true);
}
regcomp(&re, "^(.*)\\.js$", REG_ICASE | REG_EXTENDED);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes all files from a directory in the current context
////////////////////////////////////////////////////////////////////////////////
result = true;
for (i = 0; i < files._length; ++i) {
bool ok;
char const* filename;
char* full;
filename = files._buffer[i];
if (! regexec(&re, filename, 0, 0, 0) == 0) {
continue;
}
full = TRI_Concatenate2File(path, filename);
ok = TRI_LoadJavaScriptFile(context, full);
TRI_FreeString(full);
result = result && ok;
}
TRI_DestroyVectorString(&files);
regfree(&re);
return result;
bool TRI_ExecuteJavaScriptDirectory (v8::Handle<v8::Context> context, char const* path) {
return LoadJavaScriptDirectory(context, path, true);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -76,6 +76,18 @@ bool TRI_LoadJavaScriptFile (v8::Handle<v8::Context> context, char const* filena
bool TRI_LoadJavaScriptDirectory (v8::Handle<v8::Context> context, char const* path);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a file in the current context
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExecuteJavaScriptFile (v8::Handle<v8::Context> context, char const* filename);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes all files from a directory in the current context
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExecuteJavaScriptDirectory (v8::Handle<v8::Context> context, char const* path);
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a string within a V8 context
////////////////////////////////////////////////////////////////////////////////

View File

@ -25,7 +25,7 @@
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
// var actions = require("actions");
var actions = require("actions");
////////////////////////////////////////////////////////////////////////////////
/// @brief geo "near" query

View File

@ -33,7 +33,7 @@ var actions = require("actions");
actions.defineHttp({
url : "hallo-world",
domain : "user",
context : "user",
callback :
function (req, res) {

View File

@ -81,6 +81,7 @@ static string JS_bootstrap_modules =
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"Module.prototype.require = function (path) {\n"
" var raw;\n"
" var content;\n"
" var sandbox;\n"
" var paths;\n"
@ -96,12 +97,12 @@ static string JS_bootstrap_modules =
" }\n"
"\n"
" // locate file and read content\n"
" content = internal.readFile(path);\n"
" raw = internal.readFile(path);\n"
"\n"
" // create a new sandbox and execute\n"
" ModuleCache[path] = module = new Module(path);\n"
"\n"
" content = \"(function (module, exports, require, print) {\" + content + \"\\n});\";\n"
" content = \"(function (module, exports, require, print) {\" + raw.content + \"\\n/* end-of-file '\" + raw.path + \"' */ });\";\n"
"\n"
" try {\n"
" f = SYS_EXECUTE(content, undefined, path);\n"
@ -348,7 +349,7 @@ static string JS_bootstrap_modules =
" }\n"
"\n"
" if (FS_EXISTS(n)) {\n"
" return SYS_READ(n);\n"
" return { path : n, content : SYS_READ(n) };\n"
" }\n"
" }\n"
"\n"

View File

@ -80,6 +80,7 @@ function Module (id) {
////////////////////////////////////////////////////////////////////////////////
Module.prototype.require = function (path) {
var raw;
var content;
var sandbox;
var paths;
@ -95,12 +96,12 @@ Module.prototype.require = function (path) {
}
// locate file and read content
content = internal.readFile(path);
raw = internal.readFile(path);
// create a new sandbox and execute
ModuleCache[path] = module = new Module(path);
content = "(function (module, exports, require, print) {" + content + "\n});";
content = "(function (module, exports, require, print) {" + raw.content + "\n/* end-of-file '" + raw.path + "' */ });";
try {
f = SYS_EXECUTE(content, undefined, path);
@ -347,7 +348,7 @@ internal.readFile = function (path) {
}
if (FS_EXISTS(n)) {
return SYS_READ(n);
return { path : n, content : SYS_READ(n) };
}
}

View File

@ -30,8 +30,8 @@ static string JS_client_client =
"function help () {\n"
" print(\"--------------- HELP -----------------------------------------------\");\n"
" print(\"predefined objects:\");\n"
" print(\" advocado: AdvocadoConnection\");\n"
" print(\" db: AdvocadoDatabase\");\n"
" print(\" avocado: AvocadoConnection\");\n"
" print(\" db: AvocadoDatabase\");\n"
" print(\"examples:\");\n"
" print(\" > db._collections(); list all collections\");\n"
" print(\" > db.<coll_name>.all(); list all documents\");\n"

View File

@ -202,29 +202,48 @@ function defineHttp (options) {
}
if (typeof callback !== "function") {
console.error("callback for '" + url + "' must be a function, got '" + (typeof callback) + "'");
console.error("callback for '%s' must be a function, got '%s'", url + (typeof callback));
return;
}
console.debug("callback ", callback, "\n");
// console.debug("callback: %s", callback);
for (var i = 0; i < contexts.length; ++i) {
var context = contexts[i];
var queue = "CLIENT";
var use = false;
if (context == "admin") {
queue = "SYSTEM";
if (SYS_ACTION_QUEUE == "SYSTEM") {
use = true;
}
}
else if (context == "api") {
if (SYS_ACTION_QUEUE == "SYSTEM" || SYS_ACTION_QUEUE == "CLIENT") {
use = true;
}
}
else if (context == "user") {
if (SYS_ACTION_QUEUE == "SYSTEM" || SYS_ACTION_QUEUE == "CLIENT") {
use = true;
}
}
else if (context == "monitoring") {
queue = "MONITORING";
if (SYS_ACTION_QUEUE == "MONITORING") {
use = true;
}
}
try {
internal.defineAction(url, queue, callback, parameter);
console.debug("defining action '" + url + "' in context " + context + " using queue " + queue);
if (use) {
try {
internal.defineAction(url, SYS_ACTION_QUEUE, callback, parameter);
console.debug("defining action '%s' in context '%s' using queue '%s'", url, context, SYS_ACTION_QUEUE);
}
catch (err) {
console.error("action '%s' encountered error: %s", url, err);
}
}
catch (err) {
console.error("action '" + url + "' encountered error: " + err);
else {
console.debug("ignoring '%s' for context '%s' in queue '%s'", url, context, SYS_ACTION_QUEUE);
}
}
}

View File

@ -297,7 +297,7 @@ function runTest (path) {
content = SYS_READ(path);
}
catch (err) {
console.error("cannot load test file '" + path + "'");
console.error("cannot load test file '%s'", path);
return;
}

View File

@ -116,7 +116,7 @@ static string JS_server_modules =
" require(\"simple-query\");\n"
"}\n"
"catch (err) {\n"
" console.error(\"while loading 'simple-query' module: \" + err);\n"
" console.error(\"while loading 'simple-query' module: %s\", err);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"

View File

@ -115,7 +115,7 @@ try {
require("simple-query");
}
catch (err) {
console.error("while loading 'simple-query' module: " + err);
console.error("while loading 'simple-query' module: %s", err);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -36,33 +36,29 @@ var actions = require("actions");
/// @{
////////////////////////////////////////////////////////////////////////////////
var API = "_api";
var API = "_api/";
var ApiRequests = {};
ApiRequests.cursor = {
"POST /" + API + "/cursor" : "create and execute query. (creates a cursor)",
"PUT /" + API + "/cursor/<cursor-id>" : "get next results",
"DELETE /" + API + "/cursor/<cursor-id>" : "delete cursor"
}
ApiRequests.cursor = {};
ApiRequests.cursor["POST /" + API + "cursor"] = "create and execute query. (creates a cursor)";
ApiRequests.cursor["PUT /" + API + "cursor/<cursor-id>"] = "get next results";
ApiRequests.cursor["DELETE /" + API + "cursor/<cursor-id>"] = "delete cursor";
ApiRequests.collection = {
"GET /" + API + "/collections" : "get list of collections",
"GET /" + API + "/collection/<collection-id>" : "get all elements of collection"
}
ApiRequests.collection = {};
ApiRequests.collection["GET /" + API + "collections"] = "get list of collections";
ApiRequests.collection["GET /" + API + "collection/<collection-id>"] = "get all elements of collection";
ApiRequests.document = {
"POST /" + API + "/document/<collection-id>" : "create new document",
"PUT /" + API + "/document/<collection-id>/<document-id>" : "update document",
"GET /" + API + "/document/<collection-id>/<document-id>" : "get a document",
"DELETE /" + API + "/document/<collection-id>/<document-id>" : "delete a document"
}
ApiRequests.document = {};
ApiRequests.document["POST /" + API + "document/<collection-id>"] = "create new document";
ApiRequests.document["PUT /" + API + "document/<collection-id>/<document-id>"] = "update document";
ApiRequests.document["GET /" + API + "document/<collection-id>/<document-id>"] = "get a document";
ApiRequests.document["DELETE /" + API + "document/<collection-id>/<document-id>"] = "delete a document";
ApiRequests.query = {
"POST /" + API + "/query" : "create a query",
"GET /" + API + "/query/<query-id>" : "get query",
"PUT /" + API + "/query/<query-id>" : "change query",
"DELETE /" + API + "/query/<query-id>" : "delete query"
}
ApiRequests.query = {};
ApiRequests.query["POST /" + API + "query"] = "create a query";
ApiRequests.query["GET /" + API + "query/<query-id>"] = "get query";
ApiRequests.query["PUT /" + API + "query/<query-id>"] = "change query";
ApiRequests.query["DELETE /" + API + "query/<query-id>"] = "delete query";
// -----------------------------------------------------------------------------
// --SECTION-- public functions
@ -86,26 +82,11 @@ actions.defineHttp({
requests : ApiRequests
}
actionResultOK(req, res, 200, result);
actions.actionResultOK(req, res, 200, result);
}
});
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- MODULE EXPORTS
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup AvocadoAPI
/// @{
////////////////////////////////////////////////////////////////////////////////
exports.apiPrefix = API;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////