Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jobs runner #56

Open
wants to merge 48 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
f9688b0
Add maintenance infastructure
jtwiest May 8, 2017
9459a55
Add vfs update command
jtwiest May 10, 2017
22d9748
Add maintenance request to jobs stored in memory
jtwiest May 11, 2017
c23b02c
Add maintence request to sites PATCH
jtwiest May 11, 2017
73febfe
Add jobs getter to site request
jtwiest May 11, 2017
73b2bca
Fix bug which caused jobs to get written to the disk
jtwiest May 11, 2017
a25d1f5
Remove site key from site root cache deletion
jtwiest May 11, 2017
a273c20
Remove deprecated cache.php file
jtwiest May 11, 2017
f72ecb3
Remove unnecessary phpfpm param
jtwiest May 11, 2017
694a198
Store site object in variable
themightychris May 12, 2017
99edfa5
Remove extra semicolon
jtwiest May 15, 2017
505c259
Add config wrapper around siteData
jtwiest May 16, 2017
d76c515
Clean up jobs references
jtwiest May 16, 2017
7e9a20b
Add npm package uuid
jtwiest May 17, 2017
456e493
Init job as object not array
jtwiest May 17, 2017
938a036
Use uuid package over hand crafted uuids
jtwiest May 17, 2017
b999d4b
Restructure job init
jtwiest May 17, 2017
24018ba
Restructure pruneJobs
jtwiest May 17, 2017
baae228
Restructure jobs to give eacn command a unique uid
jtwiest May 17, 2017
dad9f78
Add async npm package
jtwiest May 17, 2017
099e079
Add async maintenance queue
jtwiest May 17, 2017
dd154d1
Add config reload maintenance action
jtwiest May 17, 2017
ddce233
Add bulk site maintenance request handler
jtwiest May 17, 2017
2c4a0f0
Add /jobs endpoint
jtwiest May 18, 2017
9942199
Use job namespace instead of maintenance
jtwiest May 18, 2017
f783bfa
Handle /jobs get request
jtwiest May 18, 2017
337c31b
Catch job.php parse errors
jtwiest May 18, 2017
07c04dd
Fix site loaded console log
jtwiest May 19, 2017
4f7e490
Fix jobs declaration and return value
jtwiest May 19, 2017
a79472c
Fix typo in output variable
jtwiest May 22, 2017
686bad8
Remove 30 second job execution limit
jtwiest May 22, 2017
6e93349
Fix indentation
jtwiest May 22, 2017
2c9b583
Remove trailing commas
jtwiest May 22, 2017
ebbc169
Update command data being passed in php job processing
jtwiest May 22, 2017
b2a73f1
Add cursor to all get file system requests
jtwiest May 25, 2017
568ef42
Add flag for including deleted file in /emergence request
jtwiest May 26, 2017
2af5197
Merge pull request #47 from JarvusInnovations/master
jtwiest Jun 23, 2017
4c560ef
Improve delete / new file reporting
jtwiest Jun 26, 2017
43b459c
Update logs to only show newly added jobs
jtwiest Jul 3, 2017
70dacb4
Add worker_rlimit_nofile setting to nginx config
jtwiest Aug 7, 2017
da28cec
Set mysql config explicit_defaults_for_timestamp
jtwiest Aug 11, 2017
ec6c6e4
Merge branch 'master' of github.com:JarvusInnovations/Emergence into …
jtwiest Sep 19, 2017
66cc5f3
Increase default mysql max_allowed_packet option
jtwiest Sep 29, 2017
bd86313
Merge branch 'features/maintenance' into master
jtwiest Dec 19, 2017
ab6d970
Merge pull request #53 from JarvusInnovations/master
jtwiest Dec 19, 2017
a0fa4f1
Merge branch 'master' of github.com:JarvusInnovations/Emergence into …
jtwiest Dec 22, 2017
ea715e1
Cherrypick bf26358530cee616ce84e2bc07c5426487f618fc
themightychris Apr 27, 2018
09c0b60
Register emergence-fire-event command
themightychris Apr 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions bin/fire-event
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env node


require('yargs')
.command('$0 <site> <event> <context>', 'Fire event within site', yargs => {
yargs
.positional('site', {
describe: 'Handle of site to fire event within'
})
.positional('event', {
describe: 'Name of event to fire'
})
.positional('context', {
describe: 'Context path to fire event within'
})
}, argv => {
var path = require('path'),
documentRoot = path.resolve(__dirname, '../php-bootstrap'),
PHPFPM = require('node-phpfpm'),
phpClient = new PHPFPM({
sockFile: '/emergence/services/run/php-fpm/php-fpm.sock',
documentRoot: documentRoot + '/'
}),
payload = Object.assign({}, argv);

delete payload._;
delete payload.help;
delete payload.version;
delete payload.site;
delete payload.event;
delete payload.context;
delete payload['$0'];

// execute event via PHP-FPM interface
phpClient.run({
uri: 'event.php',
json: {
site: argv.site,
event: argv.event,
context: argv.context,
payload: payload
}
}, function (err, output, phpErrors) {
if (err == 99) console.error('PHPFPM server error');
console.log(output);
if (phpErrors) console.error(phpErrors);
});

})
.argv;
1 change: 1 addition & 0 deletions bin/kernel
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ var eServices = require('../kernel-lib/services.js').createServices(eSites, CONF
// instantiate management server
var eManagementServer = require('../kernel-lib/server.js').createServer({
sites: eSites,
jobs: eSites.handleJobsRequest.bind(eSites),
services: eServices
}, CONFIG);

Expand Down
3 changes: 2 additions & 1 deletion kernel-lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ exports.Server.prototype.handleRequest = function(request, response) {
}

if (me.paths.hasOwnProperty(request.path[0])) {
var result = me.paths[request.path[0]].handleRequest(request, response, me);
var handler = me.paths[request.path[0]];
var result = (typeof handler == 'function' ? handler(request, response, me) : handler.handleRequest(request, response, me));

if (result===false) {
response.writeHead(404);
Expand Down
3 changes: 2 additions & 1 deletion kernel-lib/services/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ exports.MysqlService.prototype.makeConfig = function() {
'datadir = '+me.options.dataDir,
'skip-external-locking',
'key_buffer_size = 16M',
'max_allowed_packet = 1M',
'max_allowed_packet = 512M',
'sort_buffer_size = 512K',
'net_buffer_length = 8K',
'read_buffer_size = 256K',
'read_rnd_buffer_size = 512K',
'myisam_sort_buffer_size = 8M',
'explicit_defaults_for_timestamp = 1',
// 'lc-messages-dir = /usr/local/share/mysql',

'log-bin = mysqld-bin',
Expand Down
21 changes: 11 additions & 10 deletions kernel-lib/services/nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ exports.NginxService.prototype.makeConfig = function() {
'user '+me.options.user+' '+me.options.group+';',
'worker_processes auto;',
'pid '+me.options.pidPath+';',
'error_log '+me.options.errorLogPath+' info;'
'error_log '+me.options.errorLogPath+' info;',
'worker_rlimit_nofile 100000;'
);


Expand Down Expand Up @@ -256,15 +257,15 @@ exports.NginxService.prototype.makeConfig = function() {
);

_.each(me.controller.sites.sites, function(site, handle) {
var hostnames = site.hostnames.slice(),
var hostnames = site.config.hostnames.slice(),
siteDir = me.controller.sites.options.sitesDir+'/'+handle,
logsDir = siteDir+'/logs',
siteConfig = [],
sslHostnames, sslHostname;

// process hostnames
if (_.indexOf(hostnames, site.primary_hostname) == -1) {
hostnames.unshift(site.primary_hostname);
if (_.indexOf(hostnames, site.config.primary_hostname) == -1) {
hostnames.unshift(site.config.primary_hostname);
}

// process directories
Expand Down Expand Up @@ -301,15 +302,15 @@ exports.NginxService.prototype.makeConfig = function() {
' }'
);

if (site.ssl) {
if (site.ssl.hostnames) {
sslHostnames = site.ssl.hostnames;
if (site.config.ssl) {
if (site.config.ssl.hostnames) {
sslHostnames = site.config.ssl.hostnames;
} else {
sslHostnames = {};
sslHostnames[site.primary_hostname] = site.ssl;
sslHostnames[site.config.primary_hostname] = site.config.ssl;

site.hostnames.forEach(function(hostname) {
sslHostnames[hostname] = site.ssl;
site.config.hostnames.forEach(function(hostname) {
sslHostnames[hostname] = site.config.ssl;
});
}

Expand Down
72 changes: 54 additions & 18 deletions kernel-lib/services/php-fpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var _ = require('underscore'),
path = require('path'),
util = require('util'),
spawn = require('child_process').spawn,
phpfpm = require('node-phpfpm');
phpfpm = require('node-phpfpm'),
async = require('async'),
jobQueue;

exports.createService = function(name, controller, options) {
return new exports.PhpFpmService(name, controller, options);
Expand Down Expand Up @@ -44,8 +46,8 @@ exports.PhpFpmService = function(name, controller, options) {
me.status = 'online';
}

// listen for site updated
controller.sites.on('siteUpdated', _.bind(me.onSiteUpdated, me));
// listen for job requests
controller.sites.on('jobRequested', _.bind(me.onJobRequested, me));
};

util.inherits(exports.PhpFpmService, require('./abstract.js').AbstractService);
Expand Down Expand Up @@ -197,28 +199,62 @@ exports.PhpFpmService.prototype.makeConfig = function() {
return config.join('\n');
};

exports.PhpFpmService.prototype.onSiteUpdated = function(siteData) {
var me = this,
siteRoot = me.controller.sites.options.sitesDir + '/' + siteData.handle,
jobQueue = async.queue(function(data, callback) {
var me = data.scope,
siteRoot = me.controller.sites.options.sitesDir + '/' + data.job.handle,
phpClient;

console.log(me.name+': clearing config cache for '+siteRoot);

// Connect to FPM worker pool
phpClient = new phpfpm({
sockFile: me.options.socketPath,
documentRoot: me.options.bootstrapDir + '/'
});

// Clear cached site.json
// Mark job as started
data.job.started = new Date().getTime();

// Run job request
phpClient.run({
uri: 'cache.php',
json: [
{ action: 'delete', key: siteRoot }
]
}, function(err, output, phpErrors) {
if (err == 99) console.error('PHPFPM server error');
console.log(output);
if (phpErrors) console.error(phpErrors);
uri: 'job.php',
json: {
'job': data.job,
'handle': data.job.handle,
'siteRoot': siteRoot
}
}, function(err, output, stderr) {
if (err == 99) {
data.job.status = 'failed';
data.job.message = 'PHPFPM server error';
console.error(data.job.message);
return callback(err);
}
if (stderr) {
data.job.status = 'failed';
data.job.message = stderr;
console.error(stderr);
return callback(stderr);
}

// Parse job response
try {
var response = JSON.parse(output);

// Update job with response
data.job.command = response.command;
data.job.status = 'completed';
data.job.completed = new Date().getTime();

} catch(e) {
data.job.status = 'failed';
data.job.message = 'PHPFPM server error';
console.error(output);
return callback(output);
}

callback(null, data.job);
});
};
}, 5);

exports.PhpFpmService.prototype.onJobRequested = function(job) {
jobQueue.push({'job': job, 'scope': this});
}
Loading