sync/src/partition/partitionmodule.js

106 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-06-06 21:54:49 -07:00
import { loadFromToml } from 'cytube-common/lib/configuration/configloader';
import { PartitionConfig } from './partitionconfig';
import { PartitionDecider } from './partitiondecider';
import { PartitionClusterClient } from '../io/cluster/partitionclusterclient';
2016-06-09 23:42:30 -07:00
import RedisClientProvider from 'cytube-common/lib/redis/redisclientprovider';
2016-06-06 21:54:49 -07:00
import logger from 'cytube-common/lib/logger';
import LegacyConfig from '../config';
import path from 'path';
2016-07-03 21:28:43 -07:00
import { AnnouncementRefresher } from './announcementrefresher';
import { RedisPartitionMapReloader } from './redispartitionmapreloader';
2016-06-06 21:54:49 -07:00
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
'partitions.toml');
class PartitionModule {
constructor() {
this.initConfig();
}
onReady() {
2016-07-03 21:28:43 -07:00
this.getAnnouncementRefresher();
2016-06-06 21:54:49 -07:00
}
initConfig() {
logger.initialize(null, null, LegacyConfig.get('debug'));
try {
this.partitionConfig = this.loadPartitionConfig();
2016-06-06 21:54:49 -07:00
} catch (error) {
process.exit(1);
}
}
loadPartitionConfig() {
2016-06-06 21:54:49 -07:00
try {
return loadFromToml(PartitionConfig, PARTITION_CONFIG_PATH);
} catch (error) {
if (typeof error.line !== 'undefined') {
logger.error(`Error in ${PARTITION_CONFIG_PATH}: ${error} ` +
`(line ${error.line})`);
} else {
logger.error(`Error loading ${PARTITION_CONFIG_PATH}: ` +
`${error.stack}`);
}
throw error;
}
}
getPartitionMapReloader() {
if (!this.partitionMapReloader) {
const redisProvider = this.getRedisClientProvider();
this.partitionMapReloader = new RedisPartitionMapReloader(
redisProvider.get(), // Client for GET partitionMap
redisProvider.get()); // Subscribe client
}
return this.partitionMapReloader;
}
2016-06-06 21:54:49 -07:00
getPartitionDecider() {
if (!this.partitionDecider) {
const reloader = this.getPartitionMapReloader();
this.partitionDecider = new PartitionDecider(this.partitionConfig,
reloader.getPartitionMap());
reloader.on('partitionMapChange', newMap => {
this.partitionDecider.setPartitionMap(newMap);
require('../server').getServer().handlePartitionMapChange();
});
2016-06-06 21:54:49 -07:00
}
return this.partitionDecider;
}
getClusterClient() {
if (!this.partitionClusterClient) {
this.partitionClusterClient = new PartitionClusterClient(
this.getPartitionDecider());
}
return this.partitionClusterClient;
}
2016-06-09 23:42:30 -07:00
getRedisClientProvider() {
if (!this.redisClientProvider) {
this.redisClientProvider = new RedisClientProvider(
this.partitionConfig.getRedisConfig()
);
}
return this.redisClientProvider;
}
2016-07-03 21:28:43 -07:00
getAnnouncementRefresher() {
if (!this.announcementRefresher) {
const provider = this.getRedisClientProvider();
this.announcementRefresher = new AnnouncementRefresher(
provider.get(),
provider.get()
);
}
return this.announcementRefresher;
}
2016-06-06 21:54:49 -07:00
}
export { PartitionModule };