Skip to content

Commit

Permalink
[feat] wrap app in function
Browse files Browse the repository at this point in the history
  • Loading branch information
icfr committed Sep 26, 2016
1 parent ef8c10f commit bc601eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
77 changes: 36 additions & 41 deletions bootstrap/app.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
'use strict';
/* global logger,appdir,config,job */
/* eslint no-eval: 0 */

/* global app,appdir */

global.app = {};
require('.');
var Redis = require('ioredis');
var console = logger(config.core.log.prefix + ':app');
global.Queue = require('bee-queue');
var logPrefix = app.config('core.log.prefix');
var console = app.logger(logPrefix + ':app');
var includeAll = require('include-all');
/* istanbul ignore next */
var getJob = function Eval(str) {
//disable jshint error
var evil = eval;
var cleanStr = str.replace(/;|\(|\)|{|}/gi, '');
return evil(cleanStr);

global.Queue = require('bee-queue');

module.exports = function startApp() {
if (app.config('database')) {
app.queueOption = {
prefix: app.config('core.queue.prefix'),
redis: app.config('database.redis')
};
app.job = includeAll({
dirname: appdir + '/Jobs',
filter: /(.+)\.js$/
});
var redis = new Redis(app.config('database.redis'));

redis.subscribe(app.config('broadcasting.channel') || 'laravel-channel');
redis.on('message', function redisMessage(channel, message) {
message = JSON.parse(message);
var jobTodo = app.config('app.job.' + message.event);

if (!jobTodo || !app.job[jobTodo]) {
console.info('no job for ' + message.event);
console.info(message.data);
return;
}
console.log('add job for ' + message.event);
app.job[jobTodo].add(message.data);
});
} else {
throw new Error('did you have run ./artisan laravel:config ?');
}
};
/* istanbul ignore if */
if (config.database) {
global.queueOption = {
prefix: config.core.queue.prefix,
redis: config.database.redis
};
global.job = includeAll({
dirname: appdir + '/Jobs',
filter: /(.+)\.js$/
});
var redis = new Redis(config.database.redis);
redis.subscribe(config.broadcasting.channel || 'laravel-channel');
redis.on('message', function redisMessage(channel, message) {
message = JSON.parse(message);
var jobTodo;
try {
jobTodo = getJob('config.app.job.' + message.event);
} catch (err) {
jobTodo = null;
}
if (!jobTodo || !job[jobTodo]) {
console.info('no job for ' + message.event);
console.info(message.data);
return;
}
console.log('add job for ' + message.event);
job[jobTodo].add(message.data);
});
} else {
throw new Error('did you have run ./artisan laravel-config ?');
}

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict';
require('./bootstrap/app');
require('./bootstrap/app')();

0 comments on commit bc601eb

Please sign in to comment.