memdbd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env node
  2. // Copyright 2015 The MemDB Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. // implied. See the License for the specific language governing
  14. // permissions and limitations under the License. See the AUTHORS file
  15. // for names of contributors.
  16. 'use strict';
  17. var helpContent = '\
  18. MemDB - Distributed transactional in memory database\n\n\
  19. Usage: memdbd [options]\n\
  20. Options:\n\
  21. -c, --conf path Config file path\n\
  22. -s, --shard shardId Start specific shard\n\
  23. -d, --daemon Start as daemon\n\
  24. -h, --help Display this help\n';
  25. if (require.main === module) {
  26. var argv = require('minimist')(process.argv.slice(2));
  27. if(process.argv.length <= 2 || argv.help || argv.h){
  28. console.log(helpContent);
  29. process.exit(0);
  30. }
  31. var confPath = argv.conf || argv.c;
  32. var shardId = argv.shard || argv.s;
  33. if(!shardId){
  34. throw new Error('Please specify shardId with --shard');
  35. }
  36. //Become daemon
  37. if((argv.d || argv.daemon) && !process.env.__parentPid){
  38. process.env.__parentPid = process.pid;
  39. var opts = {
  40. stdio : 'ignore',
  41. env : process.env,
  42. cwd : process.cwd(),
  43. detached : true,
  44. };
  45. var args = [].concat(process.argv);
  46. args.shift(); // shift off node
  47. process.stdout.write('starting ' + shardId + '... ');
  48. var child = require('child_process').spawn(process.execPath, args, opts);
  49. child.on('exit', function(code, signal){
  50. if(code !== 0){
  51. process.stdout.write('failed\n');
  52. process.exit(1);
  53. }
  54. else{
  55. process.exit(0);
  56. }
  57. });
  58. child.unref();
  59. process.on('SIGUSR2', function(){
  60. process.stdout.write('success\n');
  61. process.exit(0);
  62. });
  63. // prevent from exit
  64. process.on('SIGTERM', function(){});
  65. process.on('SIGINT', function(){});
  66. setTimeout(function(){}, 5000);
  67. // setTimeout(function(){}, 100000000);
  68. return;
  69. }
  70. var config = require('../app/config');
  71. var conf = config.init(confPath, shardId);
  72. var shardConfig = config.shardConfig(shardId);
  73. require('../app/server').start(shardConfig)
  74. .then(function(){
  75. // send signal to parent process
  76. var parentPid = process.env.__parentPid;
  77. if(parentPid){
  78. // process.kill(parentPid, 'SIGUSR2');
  79. }
  80. }, function(e){
  81. console.error(e.stack ? e.stack : e);
  82. require('memdb-logger').shutdown(function(){
  83. process.exit(1);
  84. });
  85. });
  86. }