def start(mail_credentials_path=__mail_credentials_path,
          transport_string=__transport):
    """Starts the execution of the server.
    
    The first argument is a path to `mail_credentials.json` file and the second
    argument is a ZeroMQ transport string for bind server socket.

    """
    if not Scheduler.is_running():
        Utils.ntpcheck()

        ctx = zmq.Context.instance()
        s = ctx.socket(zmq.PULL)
        s.bind(transport_string)

        Utils.startup_wait()

        __process_message(
            mail_credentials_path, {
                "name": "MailLoggerServer",
                "level": str(__levels.ALERT),
                "schedule": str(__schedules.INSTANTANEOUSLY),
                "text": "Logging service STARTED",
                "datetime": datetime.datetime.now()
            })
        mail_credentials = json.loads(open(mail_credentials_path).read())
        mail_credentials = None

        Scheduler.start()
        Scheduler.repeat_o_clock(3600 * 1000, __queue_handler,
                                 mail_credentials_path, "HOURLY",
                                 __hourly_queue)
        Scheduler.repeat_o_clock(3600 * 24 * 1000, __queue_handler,
                                 mail_credentials_path, "DAILY", __daily_queue)
        Scheduler.repeat_o_clock(3600 * 24 * 7 * 1000, __queue_handler,
                                 mail_credentials_path, "WEEKLY",
                                 __weekly_queue)

        print("Running server at ZMQ transport: " + transport_string)
        try:
            while True:
                msg = s.recv_pyobj()
                __process_message(mail_credentials_path, msg)
            raise Exception("Unexpected error (probably NTP related)")
        except:
            __queue_handler(mail_credentials_path, "HOURLY", __hourly_queue)
            __queue_handler(mail_credentials_path, "DAILY", __daily_queue)
            __queue_handler(mail_credentials_path, "WEEKLY", __weekly_queue)
            __process_message(
                mail_credentials_path, {
                    "name": "MailLoggerServer",
                    "level": "ALERT",
                    "schedule": "INSTANTANEOUSLY",
                    "text": "Logging service STOPPED",
                    "datetime": datetime.datetime.now()
                })
            print("Stopping server")
            Scheduler.stop()
            raise
                        config["when" + suffix] = t
                # check state transition before message is appended
                if state != last_state:
                    state_message = {'timestamp': t, 'data': state}
                    messages.append((topic.format(name, "state",
                                                  mac), state_message))
                    config["state"] = state  # track current state value
            except:
                print "Unexpected error:", traceback.format_exc()
                logger.info("Error happened while processing circles data: %s",
                            traceback.format_exc())
        for top, message in messages:
            client.publish(top, json.dumps(message))
    except:
        print "Unexpected error:", traceback.format_exc()
        logger.error("Error happened while processing circles data")
        raise


if __name__ == "__main__":
    Utils.startup_wait()
    start()
    Scheduler.start()
    Scheduler.repeat_o_clock(config["period"], publish)
    try:
        while True:
            time.sleep(60)
    except:
        Scheduler.stop()
        stop()
    try:
        func(*args)
        return True
    except:
        print "Unexpected error:",traceback.format_exc()
        logger.error("Unexpected error: %s", traceback.format_exc())
        return False

def __replace_vars(x, module):
    if type(x) is int or type(x) is float or not x.startswith("$this."): return x
    method = getattr(module, x.replace("$this.",""))
    return method

if __name__ == "__main__":
    Utils.ntpcheck()
    Utils.startup_wait()
    
    T1_MILISECOND  = 1
    T1_CENTISECOND = 10
    T1_DECISECOND  = 100
    T1_SECOND      = 1000
    T1_MINUTE      = 60000
    T1_HOUR        = 3600000
    T1_DAY         = 24 * T1_HOUR

    # Configure logger.
    logger = LoggerClient.open("MainMonitoringSystem")

    logger.info("Initializing main monitoring system")

    # Configure Scheduler.