#! /usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
# Copyright 2018 the HERA Collaboration
# Licensed under the 2-clause BSD license.

"""Gather node sensor and power status info and log them into M&C

The values cycle out of the Redis store every minute, so cron can't
sample quickly enough (and starting a new process every minute feels like a
bit much).

"""
from __future__ import absolute_import, division, print_function

import sqlalchemy.exc
import sys
import time
import traceback
import socket

from astropy.time import Time

from hera_mc import mc

MONITORING_INTERVAL = 60  # seconds

parser = mc.get_mc_argument_parser()
args = parser.parse_args()
db = mc.connect_to_mc_db(args)

hostname = socket.gethostname()

# List of commands (methods) to run on each iteration
commands_to_run = ['add_node_sensor_readings_from_nodecontrol',
                   'add_node_power_status_from_nodecontrol']

while True:
    try:
        # Use a single session unless there's an error that isn't fixed by a rollback.
        with db.sessionmaker() as session:
            while True:
                time.sleep(MONITORING_INTERVAL)

                for command in commands_to_run:
                    try:
                        getattr(session, command)()
                        session.commit()
                        session.add_daemon_status('mc_monitor_correlator',
                                                  hostname, Time.now(), 'good')
                        session.commit()
                    except Exception as e:
                        print('{t} -- error calling command {c}'.format(
                            t=time.asctime(), c=command), file=sys.stderr)
                        traceback.print_exc(file=sys.stderr)
                        traceback_str = traceback.format_exc()
                        session.rollback()
                        try:
                            # try to update the daemon_status table and add an
                            # error message to the subsystem_error table
                            session.add_daemon_status('mc_monitor_nodes',
                                                      hostname, Time.now(), 'errored')
                            session.add_subsystem_error(Time.now(),
                                                        'mc_node_monitor',
                                                        2, traceback_str)
                        except Exception:
                            # if we can't log error messages to the session, need a new session
                            reraise_context('error logging to subsystem_error '
                                            'table after command "%r"', command)
                        continue
    except Exception:
        # Try to log an error with a new session
        traceback.print_exc(file=sys.stderr)
        with db.sessionmaker() as new_session:
            try:
                # try to update the daemon_status table and add an
                # error message to the subsystem_error table
                session.add_daemon_status('mc_monitor_nodes',
                                          hostname, Time.now(), 'errored')
                session.add_subsystem_error(Time.now(),
                                            'mc_node_monitor',
                                            2, traceback_str)
            except Exception:
                # if we can't log error messages to the new session we're in real trouble
                reraise_context('error logging to subsystem_error with a new session')
        # logging with a new session worked, so restart to get a new session
        continue

@@ -1365,18 +1365,13 @@
 tor_
-correlator
+nodes
 ',%0A 

fix copy/paste bug that makes the node monitor report wrong