Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live config update #4738

Merged
merged 1 commit into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configs/config.json.cluster.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json.optimizer.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
4 changes: 4 additions & 0 deletions configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"websocket_server": false,
"heartbeat_threshold": 10,
"enable_social": true,
"live_config_update": {
"enabled": false,
"tasks_only": false
},
"tasks": [
{
"//NOTE: This task MUST be placed on the top of task list": {},
Expand Down
2 changes: 2 additions & 0 deletions docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Document the configuration options of PokemonGo-Bot.
|`pokemon_bag.show_count` | false | Show amount of each pokemon.
|`pokemon_bag.pokemon_info` | [] | Check any config example file to see available settings.
|`favorite_locations` | [] | Allows you to define a collection of locations and coordinates, allowing rapid switch using a "label" on your location config
| `live_config_update.enabled` | false | Enable live config update
| `live_config_update.tasks_only` | false | True: quick update for Tasks only (without re-login). False: slower update for entire config file.


## Configuring Tasks
Expand Down
46 changes: 38 additions & 8 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def handle_sigint(*args):
raise SIGINTRecieved
signal.signal(signal.SIGINT, handle_sigint)

def initialize_task(bot, config):
tree = TreeConfigBuilder(bot, config.raw_tasks).build()
bot.workers = tree

def initialize(config):
bot = PokemonGoBot(config)
bot.start()
initialize_task(bot,config)
bot.metrics.capture_stats()
bot.health_record = BotEvent(config)
return bot

def get_commit_hash():
try:
hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], stderr=subprocess.STDOUT)[:-1]
Expand All @@ -88,7 +100,7 @@ def get_commit_hash():
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

config = init_config()
config, config_file = init_config()
if not config:
return

Expand All @@ -100,12 +112,8 @@ def get_commit_hash():

while not finished:
try:
bot = PokemonGoBot(config)
bot.start()
tree = TreeConfigBuilder(bot, config.raw_tasks).build()
bot.workers = tree
bot.metrics.capture_stats()
bot.health_record = health_record
bot = initialize(config)
config_changed = check_mod(config_file)

bot.event_manager.emit(
'bot_start',
Expand All @@ -116,6 +124,12 @@ def get_commit_hash():

while True:
bot.tick()
if config.live_config_update_enabled and config_changed():
logger.info('Config changed! Applying new config.')
config, _ = init_config()

if config.live_config_update_tasks_only: initialize_task(bot, config)
else: bot = initialize(config)

except KeyboardInterrupt:
bot.event_manager.emit(
Expand Down Expand Up @@ -212,6 +226,18 @@ def get_commit_hash():
data={'path': cached_forts_path}
)

def check_mod(config_file):
check_mod.mtime = os.path.getmtime(config_file)

def compare_mtime():
mdate = os.path.getmtime(config_file)
if check_mod.mtime == mdate: # mtime didnt change
return False
else:
check_mod.mtime = mdate
return True

return compare_mtime

def report_summary(bot):
if bot.metrics.start_time is None:
Expand Down Expand Up @@ -265,6 +291,7 @@ def _json_loader(filename):

if config_arg and os.path.isfile(config_arg):
_json_loader(config_arg)
config_file = config_arg
elif os.path.isfile(config_file):
logger.info('No config argument specified, checking for /configs/config.json')
_json_loader(config_file)
Expand Down Expand Up @@ -596,6 +623,9 @@ def _json_loader(filename):
config.daily_catch_limit = load.get('daily_catch_limit', 800)
config.vips = load.get('vips', {})
config.sleep_schedule = load.get('sleep_schedule', [])
config.live_config_update = load.get('live_config_update', {})
config.live_config_update_enabled = config.live_config_update.get('enabled', False)
config.live_config_update_tasks_only = config.live_config_update.get('tasks_only', False)

if config.map_object_cache_time < 0.0:
parser.error("--map_object_cache_time is out of range! (should be >= 0.0)")
Expand Down Expand Up @@ -662,7 +692,7 @@ def task_configuration_error(flag_name):
raise

fix_nested_config(config)
return config
return config, config_file

def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs):
if not long_flag:
Expand Down