Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /*
  2. The example consists of 3 files:
  3. queue.js => implements the plugin
  4. bar.js => implements a basic service
  5. foo.js => implements another service which calls bar.js
  6. You will need redis running on localhost port 6379, or change the configurations
  7. */
  8.  
  9. //**************** queue.js
  10.  
  11. // Fire and forget rpc for Studio.
  12. var localServices ={};
  13. //Using redis as message broker for simplicity
  14. var Redis = require('ioredis');
  15. // Adding a prefix to avoid conflict with other lists on redis
  16. var QUEUE_PREFIX = 'studio_queue_';
  17.  
  18. /*
  19. Each service will have their own queue, and will wait for messages
  20. You can implement any message broker you want,
  21. doing a naive redis implementation just to keep really simple
  22. */
  23.  
  24. module.exports = function(options,Studio){
  25. "use strict";
  26. var redisSender = new Redis();
  27. var sendMessageToQueue = Studio.promise.promisify(redisSender.lpush).bind(redisSender);
  28. options.onStart(function(serv,ref){
  29. var redisClient = new Redis();
  30. localServices[serv.id] = redisClient;
  31. /*
  32. When you start a new service, makes it listen for a message on his queue, when a message arrives,
  33. parse and call the service
  34. */
  35. redisClient.blpop(QUEUE_PREFIX+serv.id,0,function(error,message){
  36. message = JSON.parse(message[1]);
  37. message = Object.keys(message).map(function(key){
  38. return message[key];
  39. });
  40. /*
  41. Here we call the service, we shouldnt use serv.fn, because this would bypass the
  42. interceptSend of other plugins, so we use ref (which is basically the same thing as Studio(serv.id))
  43. */
  44. ref.apply(serv,message);
  45. });
  46. });
  47. options.onStop(function(serv,ref){
  48. /*
  49. Remove from localServices and disconnect
  50. */
  51. if(localServices[serv.id]){
  52. localServices[serv.id].disconnect();
  53. localServices[serv.id] = null;
  54. }
  55. });
  56. options.interceptSend(function(send,receiver){
  57. // Whenever a service is called, if is local call the local service, if not send message to a queue
  58. return function(){
  59. if(localServices[receiver]){
  60. return send.apply(null,arguments);
  61. }else{
  62. return sendMessageToQueue(QUEUE_PREFIX+receiver,JSON.stringify(arguments));
  63. }
  64. }
  65. });
  66. };
  67.  
  68.  
  69. //**************** foo.js
  70.  
  71. var Studio = require('studio');
  72. var StudioQueue = require('./queue');
  73. Studio.use(StudioQueue);
  74. Studio(function foo() {
  75. return Studio('bar')({hello:'Hello World!!!'});
  76. });
  77.  
  78. Studio('foo')();
  79.  
  80. //**************** bar.js
  81. var Studio = require('studio');
  82. var StudioQueue = require('./queue');
  83. Studio.use(StudioQueue);
  84. Studio(function bar(message) {
  85. console.log(message);
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement