env.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var path = require('path');
  18. var os = require('os');
  19. var fs = require('fs');
  20. var child_process = require('child_process');
  21. var memdbConfig = require('../app/config');
  22. var utils = require('../app/utils');
  23. var memdbLogger = require('memdb-logger');
  24. var logger = memdbLogger.getLogger('test', __filename);
  25. var configPath = path.join(__dirname, './memdb.conf.js');
  26. memdbConfig.init(configPath);
  27. var config = memdbConfig.clusterConfig();
  28. var memdbClusterPath = path.join(__dirname, '../bin/memdbcluster');
  29. exports.startCluster = function(shardIds, configOverrideFunc){
  30. var newConfigPath = configPath;
  31. if(typeof(configOverrideFunc) === 'function'){
  32. var newConfig = utils.clone(config);
  33. configOverrideFunc(newConfig);
  34. newConfigPath = path.join(os.tmpDir(), 'memdb-test.conf.json');
  35. fs.writeFileSync(newConfigPath, JSON.stringify(newConfig));
  36. }
  37. var args = [memdbClusterPath, 'start', '--conf=' + newConfigPath];
  38. if(shardIds){
  39. if(!Array.isArray(shardIds)){
  40. shardIds = [shardIds];
  41. }
  42. shardIds.forEach(function(shardId){
  43. args.push('--shard=' + shardId);
  44. });
  45. }
  46. var output = child_process.execFileSync(process.execPath, args).toString();
  47. logger.info(output.toString());
  48. };
  49. exports.stopCluster = function(){
  50. var output = child_process.execFileSync(process.execPath, [memdbClusterPath, 'stop', '--conf=' + configPath]);
  51. logger.info(output.toString());
  52. };
  53. exports.flushdb = function(cb){
  54. var output = child_process.execFileSync(process.execPath, [memdbClusterPath, 'drop', '--conf=' + configPath]);
  55. logger.info(output.toString());
  56. if(typeof(cb) === 'function'){
  57. cb();
  58. }
  59. };
  60. exports.shardConfig = function(shardId){
  61. return memdbConfig.shardConfig(shardId);
  62. };
  63. exports.runScript = function(script, args){
  64. var proc = child_process.fork(script, args);
  65. var deferred = P.defer();
  66. proc.on('exit', function(code){
  67. if(code === 0){
  68. deferred.resolve();
  69. }
  70. else{
  71. deferred.reject('script ' + script + ' returned non-zero code');
  72. }
  73. });
  74. return deferred.promise;
  75. };
  76. exports.config = config;
  77. exports.configPath = configPath;