Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1.  
  2. var _ = require('lodash'),
  3. Waterline = require('waterline'),
  4. path = require('path'),
  5. url = require('url'),
  6. kue = require('kue'),
  7. redis = require('../../node_modules/sails/node_modules/socket.io/node_modules/redis'),
  8. q = require('q')
  9.  
  10. ////////////////////////////////////////////////////////////////////
  11. //// SAILS ENV
  12. ////////////////////////////////////////////////////////////////////
  13. //resolve the sails config files required.
  14. var config_path = path.resolve(__dirname,'../..', 'config/')
  15. var nconf_required = require(config_path+'/nconf.js');
  16. global.sails = {
  17. config: nconf_required
  18. };
  19. var filefog_required = require(config_path+'/filefog.js');
  20. sails.config.filefog = filefog_required.filefog;
  21.  
  22. var constants_required = require(config_path+'/constants.js');
  23. sails.config.constants = constants_required.constants;
  24.  
  25. sails.log = require(config_path+'/log.js').log.custom
  26.  
  27. // Instantiate a new instance of the ORM
  28. var connection = url.parse(sails.config.nconf.get('DATABASE_URL'))
  29.  
  30. ////////////////////////////////////////////////////////////////////
  31. //// REDIS CONFIG
  32. ////////////////////////////////////////////////////////////////////
  33. global.redis_client = redis.createClient(sails.config.nconf.get("REDIS_CONNECTION:PORT"), sails.config.nconf.get("REDIS_CONNECTION:HOST"),{auth_pass: sails.config.nconf.get("REDIS_CONNECTION:AUTH") || null});
  34.  
  35. function generate_model_message(model_name,model_id,action, verb,data){
  36. var message = {
  37. "name":model_name,
  38. "args":[{
  39. "verb" : verb,
  40. "data" : data.toJSON(),
  41. "id" : model_id
  42. }]
  43. };
  44.  
  45.  
  46. var wrapper = {};
  47. wrapper.nodeId = 648745922; //this could be randomly chosen if we cant determine the client id.
  48. wrapper.args = [
  49. "/sails_model_"+model_name+"_"+model_id + ":"+action,
  50. "5:::"+JSON.stringify(message),
  51. null,
  52. []
  53. ]
  54. return JSON.stringify(wrapper);
  55. }
  56.  
  57.  
  58. function generate_association_message(model_name,model_id,attribute, id_associated, action, verb, verbId){
  59. var item ={
  60. "verb" : verb,
  61. "attribute" : attribute,
  62. "id" : model_id
  63. }
  64. item[verbId] = id_associated;
  65.  
  66.  
  67. var message = {
  68. "name":model_name,
  69. "args":[item]
  70. };
  71.  
  72. var wrapper = {};
  73. wrapper.nodeId = 648745922; //this could be randomly chosen if we cant determine the client id.
  74. wrapper.args = [
  75. "/sails_model_"+model_name+"_"+model_id + ":"+action+":"+attribute,
  76. "5:::"+JSON.stringify(message),
  77. null,
  78. []
  79. ]
  80. return JSON.stringify(wrapper);
  81. }
  82.  
  83.  
  84.  
  85.  
  86. ////////////////////////////////////////////////////////////////////
  87. //// WATERLINE CONFIG
  88. ////////////////////////////////////////////////////////////////////
  89. var orm = new Waterline();
  90. // Require any waterline compatible adapters here
  91. var postgresqlAdapter = require('sails-postgresql');
  92.  
  93.  
  94. // Build A Config Object
  95. var config = {
  96.  
  97. // Setup Adapters
  98. // Creates named adapters that have have been required
  99. adapters: {
  100. 'sails-postgresql': postgresqlAdapter
  101. },
  102.  
  103. // Build Connections Config
  104. // Setup connections using the named adapter configs
  105. connections: {
  106. qtPostgresqlServer: {
  107. adapter: 'sails-postgresql',
  108. host: connection.hostname,
  109. port: connection.port || 5432,
  110. user: connection.auth.split(':')[0],
  111. password: connection.auth.split(':')[1],
  112. database: connection.path.substring(1)
  113. }
  114. },
  115.  
  116. defaults: {
  117. migrate: 'alter'
  118. }
  119.  
  120. };
  121.  
  122.  
  123. //////////////////////////////////////////////////////////////////
  124. // WATERLINE SERVICES
  125. //////////////////////////////////////////////////////////////////
  126. var api_dir = path.resolve(__dirname,'../..', 'api/')
  127.  
  128. // load services
  129. var services = require('include-all')({
  130. dirname : api_dir +'/services',
  131. filter : /(.+)\.js$/,
  132. excludeDirs : /^\.(git|svn)$/,
  133. optional : true
  134. });
  135.  
  136. _.forEach(services, function(service,key){
  137. sails.log.info("Loading service: "+key)
  138. global[key] = service;
  139. });
  140.  
  141. //////////////////////////////////////////////////////////////////
  142. // WATERLINE MODELS
  143. //////////////////////////////////////////////////////////////////
  144.  
  145. // load models
  146. var models = require('include-all')({
  147. dirname : api_dir +'/models',
  148. filter : /(.+)\.js$/,
  149. excludeDirs : /^\.(git|svn)$/,
  150. optional : true
  151. });
  152.  
  153. _.forEach(models, function(model,key){
  154. sails.log.info("Register model: "+key)
  155. model.identity = key.toLowerCase();
  156. model.connection = 'qtPostgresqlServer';
  157.  
  158. //add publish methods
  159. model.publishCreate = function(id, data){
  160. redis_client.publish("dispatch", generate_model_message(model.identity,id,"update","updated",data))
  161. };
  162. model.publishUpdate = function(id, data){
  163. redis_client.publish("dispatch", generate_model_message(model.identity,id,"create","created",data))
  164. };
  165. model.publishAdd = function(id,attribute, idAdded){
  166. redis_client.publish("dispatch", generate_association_message(model.identity,id,attribute, idAdded, "add", "addedTo", "addedId"))
  167. };
  168. model.publishRemove = function(id,attribute, idRemoved){
  169. redis_client.publish("dispatch", generate_association_message(model.identity,id,attribute, idRemoved, "remove", "removedFrom", "removedId"))
  170. };
  171.  
  172.  
  173. var waterline_model = Waterline.Collection.extend(model);
  174. orm.loadCollection(waterline_model);
  175. });
  176.  
  177.  
  178. //////////////////////////////////////////////////////////////////
  179. // Initialization
  180. //////////////////////////////////////////////////////////////////
  181. function init_redis(){
  182. var deferred = q.defer();
  183. redis_client.on("ready", function () {
  184. sails.log.info("Redis ready")
  185. return deferred.resolve(redis_client);
  186. });
  187.  
  188. return deferred.promise;
  189. }
  190.  
  191. function init_waterline(){
  192. var deferred = q.defer();
  193. // Start Waterline passing adapters in
  194. orm.initialize(config, function(err, models) {
  195. if (err) {
  196. return deferred.reject(err)
  197. }
  198. else{
  199. sails.log.info("Waterline ready")
  200.  
  201. return deferred.resolve(models);
  202. }
  203. });
  204.  
  205. return deferred.promise;
  206. }
  207.  
  208. q.spread([init_redis(),init_waterline()],function(redis_client,waterline_models){
  209.  
  210. sails.models = waterline_models.collections;
  211. sails.connections = waterline_models.connections;
  212.  
  213. _.forEach(sails.models, function(model, name){
  214. name = name.charAt(0).toUpperCase() + name.slice(1);
  215. global[name] = model;
  216. })
  217.  
  218. sails.log.info("Starting kue")
  219. var kue_engine = kue.createQueue({
  220. prefix: 'kue',
  221. redis: {
  222. port: sails.config.nconf.get('REDIS_CONNECTION:PORT'),
  223. host: sails.config.nconf.get('REDIS_CONNECTION:HOST'),
  224. auth: sails.config.nconf.get('REDIS_CONNECTION:AUTH') || null
  225. }
  226. });
  227.  
  228. //register jobs.
  229. var jobs = require('include-all')({
  230. dirname : __dirname +'/jobs',
  231. filter : /(.+)\.js$/,
  232. excludeDirs : /^\.(git|svn)$/,
  233. optional : true
  234. });
  235. _.forEach(jobs, function(job, name){
  236. sails.log.info("Registering kue handler: "+name)
  237. kue_engine.process(name, job);
  238. })
  239.  
  240. process.once('SIGTERM', function (sig) {
  241. kue_engine.shutdown(function (err) {
  242. sails.log.error("Shutting down kue")
  243. process.exit(0);
  244. }, 5000);
  245. });
  246.  
  247.  
  248. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement