Skip to content

Commit

Permalink
Updated references to creatartis-base.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoVal committed Apr 30, 2014
1 parent 5b123ae commit db93eb2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
32 changes: 16 additions & 16 deletions src/capataz_browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** Capataz module to run under a browser's rendering thread.
*/
require(['basis'], function (basis) { "use strict";
window.basis = basis;
require(['creatartis-base'], function (base) { "use strict";
window.base = base;
var APP = window.APP = {},
CONFIG = APP.CONFIG = {
startTime: Date.now(),
Expand All @@ -11,20 +11,20 @@ require(['basis'], function (basis) { "use strict";
minDelay: 100, // 100 milliseconds.
maxDelay: 2 * 60000, // 2 minutes.
},
LOGGER = APP.LOGGER = basis.Logger.ROOT;
LOGGER = APP.LOGGER = base.Logger.ROOT;
LOGGER.appendToHtml('log', 30);
LOGGER.info('Starting '+ CONFIG.workerCount +' workers.');

/** APP.Drudger():
A wrapper for the rendering thread's side of a web worker.
*/
APP.Drudger = basis.declare({ //////////////////////////////////////////////
APP.Drudger = base.declare({ //////////////////////////////////////////////
constructor: function Drudger() {
// Nothing for now.
},

initialize: function initialize() {
var ready = new basis.Future();
var ready = new base.Future();
if (typeof Worker === 'function') {
this.webworker = new Worker('capataz_worker.js');
this.webworker.onmessage = function (msg) {
Expand All @@ -48,9 +48,9 @@ require(['basis'], function (basis) { "use strict";
// Main workflow. //////////////////////////////////////////////////////////

getTask: function getTask() {
return basis.Future.retrying(function () {
return base.Future.retrying(function () {
LOGGER.info('< Requesting jobs.');
return basis.HttpRequest.getJSON(CONFIG.jobURI).then(function (task) {
return base.HttpRequest.getJSON(CONFIG.jobURI).then(function (task) {
if (task.serverStartTime > CONFIG.startTime) {
LOGGER.info('> Definitions are outdated. Reloading...');
window.location.reload();
Expand All @@ -69,19 +69,19 @@ require(['basis'], function (basis) { "use strict";
doJob: function doJob(job) {
var drudger = this;
if (this.webworker) {
var future = new basis.Future();
var future = new base.Future();
this.webworker.onmessage = this.onWorkerMessage.bind(this, future);
this.webworker.postMessage(JSON.stringify(job));
return future;
} else {
// If web workers are not available, execute in the rendering thread.
return basis.Future.invoke(eval, this, job.code);
return base.Future.invoke(eval, this, job.code);
}
},

doWork: function doWork(task) {
var drudger = this;
return basis.Future.sequence(task.jobs, function (job) {
return base.Future.sequence(task.jobs, function (job) {
job.clientPlatform = navigator.platform; // Set job's client properties.
job.startedAt = Date.now();
return drudger.doJob(job).then(function (result) { // Jobs finishes well.
Expand All @@ -101,9 +101,9 @@ require(['basis'], function (basis) { "use strict";
},

postResults: function postResults(task) {
return basis.Future.retrying(function () {
return base.Future.retrying(function () {
LOGGER.info("< Posting results.");
return basis.HttpRequest.postJSON(CONFIG.jobURI, task).fail(function (xhr) {
return base.HttpRequest.postJSON(CONFIG.jobURI, task).fail(function (xhr) {
LOGGER.warn('! Posting failed: ', xhr.status, ' ', xhr.statusText, ' ', xhr.responseText, '.');
});
}, CONFIG.maxRetries, CONFIG.minDelay, 2, CONFIG.maxDelay).fail(function () {
Expand All @@ -113,7 +113,7 @@ require(['basis'], function (basis) { "use strict";

drudge: function drudge() {
var drudger = this;
return basis.Future.doWhile(function () {
return base.Future.doWhile(function () {
return drudger.getTask()
.then(drudger.doWork.bind(drudger))
.then(drudger.postResults.bind(drudger));
Expand All @@ -126,10 +126,10 @@ require(['basis'], function (basis) { "use strict";
}); // declare Drudger.

APP.start = function start() {
APP.drudgers = basis.Iterable.range(CONFIG.workerCount).map(function () {
APP.drudgers = base.Iterable.range(CONFIG.workerCount).map(function () {
return new APP.Drudger();
}).toArray();
basis.Future.sequence(APP.drudgers, function (drudger) {
base.Future.sequence(APP.drudgers, function (drudger) {
return drudger.initialize().done(drudger.drudge.bind(drudger));
});
}; // start.
Expand All @@ -139,4 +139,4 @@ require(['basis'], function (basis) { "use strict";
} else {
window.addEventListener('load', APP.start, false);
}
}); // require basis.
}); // require base.
28 changes: 14 additions & 14 deletions src/capataz_node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** Capataz definition for NodeJS when required as a module.
*/
"use strict";
var basis = require('./static/basis');
var base = require('creatartis-base');

exports.Capataz = basis.declare({
exports.Capataz = base.declare({
constructor: function Capataz(config) {
basis.initialize(this, config)
base.initialize(this, config)
/** Capataz.workerCount=2:
.
*/
Expand Down Expand Up @@ -34,14 +34,14 @@ exports.Capataz = basis.declare({
Maximum amount of jobs per task.
*/
.number('maxTaskSize', { defaultValue: 50, coerce: true })
/** Capataz.statistics=new basis.Statistics():
/** Capataz.statistics=new base.Statistics():
Statistics about the server functioning.
*/
.object('statistics', { defaultValue: new basis.Statistics() })
/** Capataz.logger=basis.Logger.ROOT:
.object('statistics', { defaultValue: new base.Statistics() })
/** Capataz.logger=base.Logger.ROOT:
Logger for the server.
*/
.object('logger', { defaultValue: basis.Logger.ROOT })
.object('logger', { defaultValue: base.Logger.ROOT })
/** Capataz.jobs:
Scheduled jobs by id.
*/
Expand All @@ -56,7 +56,7 @@ exports.Capataz = basis.declare({
*/
wrappedJob: function wrappedJob(imports, fun, args) {
return ('(function(){' // A code template is not used because of minification.
+'return basis.Future.imports.apply(this,'+ JSON.stringify(imports || []) +').then(function(deps){'
+'return base.Future.imports.apply(this,'+ JSON.stringify(imports || []) +').then(function(deps){'
+'return ('+ fun +').apply(this,deps.concat('+ JSON.stringify(args || []) +'));'
+'});'
+'})()');
Expand All @@ -75,7 +75,7 @@ exports.Capataz = basis.declare({
executed by a worker.
*/
schedule: function schedule(params) {
var result = new basis.Future();
var result = new base.Future();
if (this.scheduledJobsCount() >= this.maxScheduled) {
result.reject(new Error("Cannot schedule more jobs: the maximum amount ("+
this.maxScheduled +") has been reached."));
Expand Down Expand Up @@ -114,7 +114,7 @@ exports.Capataz = basis.declare({
capataz = this;
this.statistics.add({key:'task_size'}, ids.length);
return { serverStartTime: this.__startTime__,
jobs: basis.iterable(ids).map(function (id) {
jobs: base.iterable(ids).map(function (id) {
var job = capataz.jobs[id];
return job && {
id: id,
Expand Down Expand Up @@ -245,10 +245,10 @@ exports.Capataz = basis.declare({
// Utilities. //////////////////////////////////////////////////////////////////

scheduleAll: function scheduleAll(jobs, amount, callback) {
var jobs_iter = basis.iterable(jobs).__iter__(),
var jobs_iter = base.iterable(jobs).__iter__(),
amount = isNaN(amount) ? this.maxScheduled : Math.min(+amount | 0, this.maxScheduled),
capataz = this;
return basis.Future.doWhile(function () {
return base.Future.doWhile(function () {
var partition = [],
scheduled;
try {
Expand All @@ -258,10 +258,10 @@ exports.Capataz = basis.declare({
callback && callback(scheduled);
}
} catch (err) {
basis.Iterable.prototype.catchStop(err);
base.Iterable.prototype.catchStop(err);
}
// The Future.all() result is an array, so always casts to true.
return partition.length < 1 ? false : basis.Future.all(partition);
return partition.length < 1 ? false : base.Future.all(partition);
});
}
}); // declare Capataz.
Expand Down
6 changes: 3 additions & 3 deletions src/capataz_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ self.onmessage = function (msg) {
};

importScripts('require.js');
require(['basis'], function (basis) {
self.basis = basis;
require(['creatartis-base'], function (base) {
self.base = base;
self.onmessage = function onmessage(msg) {
var data = JSON.parse(msg.data);
basis.Future.invoke(eval, self, data.code || '').then(function (result) {
base.Future.invoke(eval, self, data.code || '').then(function (result) {
data.result = result;
self.postMessage(JSON.stringify(data));
}, function (error) {
Expand Down
24 changes: 12 additions & 12 deletions tests/pi_estimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
"use strict";
var PORT = 8080,
RADIUS_EXP = 32,
RADIUS_EXP = 28,//32,
RADIUS = Math.pow(2, RADIUS_EXP) - 1,
// imports
basis = require('../static/basis'),
capataz_node = require('../capataz_node'),
base = require('creatartis-base'),
capataz_node = require('../build/capataz_node'),
capataz = new capataz_node.Capataz();

function job_function(from, to, r) {
Expand All @@ -17,9 +17,8 @@ function job_function(from, to, r) {
}
return s / r / r * 4;
}

basis.Future.sequence(
basis.Iterable.range(30).product(basis.Iterable.range(17)),

base.Future.sequence(base.Iterable.range(30).product(base.Iterable.range(17)),
function (pair) {
var repetition = pair[0],
jobCount = Math.pow(2, pair[1]),
Expand All @@ -28,7 +27,7 @@ basis.Future.sequence(
//TODO tag = 'radius=2^'+ RADIUS_EXP +'-1,jobCount=2^'+ jobCountExp;
fulltimeStat = capataz.statistics.stat({key:'fulltime', step: step});
fulltimeStat.startTime();
return capataz.scheduleAll(basis.Iterable.range(0, RADIUS, step).map(function (x) {
return capataz.scheduleAll(base.Iterable.range(0, RADIUS, step).map(function (x) {
return {
fun: job_function,
args: [x, x + step, RADIUS],
Expand All @@ -45,13 +44,14 @@ basis.Future.sequence(
capataz.logger.info('Repetition #'+ repetition +' with step '+ step +' finished. PI = ', pi,
' (error ', pi_error, ').');
});
}).then(function () {
process.exit();
});
}
).then(function () {
process.exit();
});

capataz.logger.appendToConsole();
capataz.configureApp({
staticPath: __dirname +'/../static',
logFile: './tests/logs/capataz-'+ basis.Text.formatDate(new Date(), 'yyyymmdd-hhnnss') +'.log'
staticPath: __dirname +'/../build/static',
logFile: './tests/logs/capataz-'+ base.Text.formatDate(new Date(), 'yyyymmdd-hhnnss') +'.log'
}).listen(PORT);
capataz.logger.info('Server started and listening at port ', PORT, '.');

0 comments on commit db93eb2

Please sign in to comment.