/docs/repl.md

https://github.com/nibblebot/cluster · Markdown · 165 lines · 119 code · 46 blank · 0 comment · 0 complexity · 6bc0da037b76b4fe27164f7dbc11bdd7 MD5 · raw file

  1. ## REPL
  2. Provides live administration tools for inspecting state, spawning and killing workers, and more. The __REPL__ plugin itself is extensible, for example the `stats()` plugin provides a __REPL__ function named `stats()`.
  3. ### Usage
  4. The `repl([port | path])` accepts a `port` or unix domain socket `path`, after which you may telnet to at any time.
  5. Launch the __REPL__ with a local socket:
  6. cluster(server)
  7. .use(cluster.repl('/var/run/cluster.sock'))
  8. .listen(3000);
  9. Start a telnet session:
  10. $ telnet /var/run/cluster.sock
  11. cluster> help()
  12. Commands
  13. help(): Display help information
  14. spawn(n): Spawn one or more additional workers
  15. pids(): Output process ids
  16. kill(id, signal): Send signal or SIGTERM to the given worker
  17. shutdown(): Gracefully shutdown server
  18. stop(): Hard shutdown
  19. restart(): Gracefully restart all workers
  20. echo(msg): echo the given message
  21. stats(): Display server statistics
  22. __NOTE__: a local socket is recommended, otherwise this may be a secure hole.
  23. ### pids()
  24. Outputs the master / worker process ids.
  25. cluster> pids()
  26. pids
  27. master: 1799
  28. worker #0: 1801
  29. worker #1: 1802
  30. worker #2: 1803
  31. worker #3: 1804
  32. ### spawn()
  33. Spawn an additional worker.
  34. cluster> spawn()
  35. spawning 1 worker
  36. cluster> pids()
  37. pids
  38. master: 1799
  39. worker #0: 1801
  40. worker #1: 1802
  41. worker #2: 1803
  42. worker #3: 1804
  43. worker #4: 1809
  44. ### spawn(n)
  45. Spawn `n` workers:
  46. cluster> spawn(4)
  47. spawning 4 workers
  48. cluster> pids()
  49. pids
  50. master: 1817
  51. worker #0: 1818
  52. worker #1: 1819
  53. worker #2: 1820
  54. worker #3: 1821
  55. worker #4: 1825
  56. worker #5: 1826
  57. worker #6: 1827
  58. worker #7: 1828
  59. ### kill(id[, signal])
  60. Kill worker `id` with the given `signal` or __SIGTERM__. For graceful termination use __SIGQUIT__.
  61. cluster> pids()
  62. pids
  63. master: 1835
  64. worker #0: 1837
  65. worker #1: 1838
  66. worker #2: 1839
  67. worker #3: 1840
  68. cluster> kill(2)
  69. sent SIGTERM to worker #2
  70. cluster> kill(3)
  71. sent SIGTERM to worker #3
  72. cluster> pids()
  73. pids
  74. master: 1835
  75. worker #0: 1837
  76. worker #1: 1838
  77. worker #2: 1843
  78. worker #3: 1844
  79. ### restart()
  80. Gracefully restart all workers.
  81. cluster> pids()
  82. pids
  83. master: 1835
  84. worker #0: 1837
  85. worker #1: 1838
  86. worker #2: 1843
  87. worker #3: 1844
  88. cluster> restart()
  89. restarting 4 workers
  90. cluster> pids()
  91. pids
  92. master: 1835
  93. worker #0: 1845
  94. worker #1: 1849
  95. worker #2: 1848
  96. worker #3: 1847
  97. ### Defining REPL Functions
  98. To define a function accessible to the __REPL__, all we need to do is call `cluster.repl.define()`, passing the function, as well as a description string.
  99. Below we define the `echo()` function, simply printing the input `msg` given. As you can see our function receivers the `Master` instance, the __REPL__ `sock`, and any arguments that were passed. For example `echo("test")` would pass the `msg` as `"test"`, and `echo("foo", "bar")` would pass `msg` as `"foo"`, and `arguments[3]` as `"bar"`.
  100. repl.define('echo', function(master, sock, msg){
  101. sock.write(msg + '\n');
  102. }, 'echo the given message');
  103. Shown below is a more complete example.
  104. var cluster = require('../')
  105. , repl = cluster.repl
  106. , http = require('http');
  107. var server = http.createServer(function(req, res){
  108. var body = 'Hello World';
  109. res.writeHead(200, { 'Content-Length': body.length });
  110. res.end(body);
  111. });
  112. // custom repl function
  113. repl.define('echo', function(master, sock, msg){
  114. sock.write(msg + '\n');
  115. }, 'echo the given message');
  116. // $ telnet localhots 8888
  117. cluster(server)
  118. .use(repl(8888))
  119. .listen(3000);