def job_controller(jobConfig): db = Helper(BD_HOST, DB_PORT, DB_NAME) while True: try: time.sleep(jobConfig.next()) jobConfig.job(db) except KeyboardInterrupt: break
def main(): parser = argparse.ArgumentParser() parser.add_argument('--db-host', type=str, default='localhost', help='DATABASE HOST NAME') parser.add_argument('--db-port', type=int, default=27017, help='DATABASE PORT') parser.add_argument('--db-name', type=str, default='monolith-database', help='DATABASE NAME') args = parser.parse_args() global BD_HOST global DB_PORT global DB_NAME BD_HOST = args.db_host DB_PORT = args.db_port DB_NAME = args.db_name db = Helper(BD_HOST, DB_PORT, DB_NAME) crontab_interval = '* * * * *' process_number = slackbot_settings.process_number file_handler = logging.FileHandler(filename='logs/run.log') stdout_handler = logging.StreamHandler(sys.stdout) logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(process)d - %(levelname)s - %(name)s - *** %(message)s', datefmt='%Y-%m-%d %H:%M:%S', handlers=[file_handler, stdout_handler] ) jobs = [] user_keys = [] for mod_name in slackbot_settings.modules: mod = import_module('.' + mod_name, 'modules') load_module = mod.CustomModule() load_module.set() for key in load_module.user_keys: key['module'] = load_module.name if key['name'] in slackbot_settings.user_keys.keys(): key['value'] = slackbot_settings.user_keys[key['name']] user_keys.append(key) interval = '' if mod_name in slackbot_settings.intervals.keys(): interval = slackbot_settings.intervals[mod_name] else: interval = load_module.run_interval loaded_modules[load_module.name] = load_module db.initModule(load_module.name, load_module.getDefaultQuery()) jobs.append((load_module.name, interval)) logging.info('Module: {name} is successfully loaded!'.format(name=load_module.name)) db.initJob(jobs) jobConfigs = [JobConfig(CronTab(crontab_interval), runSearch, (7 * x ) % 60) for x in range(process_number)] postdata = '---Monolithbot Started. Loaded Modules are as follows.---\n```' postdata += '\n'.join(loaded_modules.keys()) postdata += '```' post_succeeded = postData(slackbot_settings.API_TOKEN, postdata, slackbot_settings.channels[0]) if post_succeeded: db.setGlobalConfig(slackbot_api_token=slackbot_settings.API_TOKEN, channels=slackbot_settings.channels, user_keys=user_keys) else: logging.error('Slack API is invalid. Disable Slackbot.') db.setGlobalConfig(slackbot_api_token='', channels=slackbot_settings.channels, user_keys=user_keys) if slackbot_settings.enable_slackbot and post_succeeded: process_number = len(jobConfigs) + 1 else: process_number = len(jobConfigs) p = Pool(process_number) try: if slackbot_settings.enable_slackbot and post_succeeded: p.apply_async(runBot) p.map(job_controller, jobConfigs) except KeyboardInterrupt: pass
def setDB(BD_HOST, DB_PORT, DB_NAME): global db db = Helper(BD_HOST, DB_PORT, DB_NAME)
writer.writerow(row) res = make_response() res.data = f.getvalue() res.headers['Content-Type'] = 'text/csv' res.headers['Content-Disposition'] = 'attachment; filename=search_results.csv' return res @app.route('/global_config', methods=["POST"]) def global_config(module=None): if request.method == "POST": data = request.json if data['action'] == 'update': if 'channels' in data.keys(): channels = data['channels'] dbControler.setSlackChannels(channels=channels) else: return abort(400) return 'OK' if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--db-host', type=str, default='localhost', help='DATABASE HOST NAME') parser.add_argument('--db-port', type=int, default=27017, help='DATABASE PORT') parser.add_argument('--db-name', type=str, default='monolith-database', help='DATABASE NAME') args = parser.parse_args() global dbControler print(args.db_host) print(args.db_name) dbControler = Helper(args.db_host, args.db_port, args.db_name) app.run(debug=True, host='0.0.0.0')