Esempio n. 1
0
    def handle(cls, content):
        for k, d in content.items():
            if k in cls._callbacks:
                for cb in cls._callbacks[k]:
                    cb(d)

        if "metasmoke_state" in content:
            if content["metasmoke_state"] == "down":
                log(
                    'info',
                    "{} says metasmoke is down, switching to active ping monitoring."
                    .format(content["location"]))
                GlobalVars.metasmoke_down = True
                Tasks.later(SocketScience.check_recent_pings, after=90)

            if content["metasmoke_state"] == "up":
                log(
                    'info',
                    '{} says metasmoke is up, disabling ping monitoring.'.
                    format(content["location"]))
                GlobalVars.metasmoke_down = False

        if "ping" in content:
            cls._pings.append({
                "timestamp": content["ping"],
                "location": content["location"]
            })
            if cls._switch_task is not None:
                cls._switch_task.cancel()
Esempio n. 2
0
    def handle(content):
        global _pings
        global _switch_task

        if "metasmoke_state" in content:
            if content["metasmoke_state"] == "down":
                log(
                    'info',
                    "{} says metasmoke is down, switching to active ping monitoring."
                    .format(content["location"]))
                GlobalVars.metasmoke_down = True
                Tasks.later(SocketScience.check_recent_pings, after=90)

            if content["metasmoke_state"] == "up":
                log(
                    'info',
                    '{} says metasmoke is up, disabling ping monitoring.'.
                    format(content["location"]))
                GlobalVars.metasmoke_down = False

        if "ping" in content:
            _pings.append({
                "timestamp": content["ping"],
                "location": content["location"]
            })
            if _switch_task is not None:
                _switch_task.cancel()
Esempio n. 3
0
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if isinstance(prop_has, tuple):
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(
                    map(
                        lambda prop: prop not in _room_roles or room not in
                        _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(
                room.room._client.host, room.room.id, notify_site,
                room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles[
                    "delay"]:

                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(
                            post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0],
                                                      callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    # Go through the list of properties in "has" and add all rooms which have any of those properties
    # to the target_rooms set. _room_roles contains a list of rooms for each property.
    for prop_has in has:
        if isinstance(prop_has, tuple):
            # If the current prop_has is a tuple, then it's assumed to be a descriptor of a specific room.
            # The format is: (_client.host, room.id)
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            # No rooms have this property.
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    # If SD is not already in the room, then join the room.
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles["delay"]:
                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0], callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
    def check_recent_pings(cls):
        recent = cls._pings.sort(key=lambda p: p["timestamp"]).reverse()
        if len(recent) >= 1:
            most_recent = recent[0]["timestamp"]
            now = time.time()

        if now - most_recent >= 90 or len(recent) == 0:
            # No active Smokeys. Wait a random number of seconds, then switch to active.
            sleep = random.randint(0, 30)
            cls._switch_task = Tasks.later(SocketScience.switch_to_active, after=sleep)
def tell_rooms(msg, has, hasnt, notify_site="", report_data=None):
    global _rooms

    msg = msg.rstrip()
    target_rooms = set()

    for prop_has in has:
        if isinstance(prop_has, tuple):
            target_rooms.add(prop_has)

        if prop_has not in _room_roles:
            continue

        for room in _room_roles[prop_has]:
            if all(map(lambda prop: prop not in _room_roles or room not in _room_roles[prop], hasnt)):
                if room not in _rooms:
                    site, roomid = room
                    deletion_watcher = room in _watcher_rooms

                    new_room = _clients[site].get_room(roomid)
                    new_room.join()

                    _rooms[room] = RoomData(new_room, -1, deletion_watcher)

                target_rooms.add(room)

    for room_id in target_rooms:
        room = _rooms[room_id]

        if notify_site:
            pings = datahandling.get_user_names_on_notification_list(room.room._client.host,
                                                                     room.room.id,
                                                                     notify_site,
                                                                     room.room._client)

            msg_pings = datahandling.append_pings(msg, pings)
        else:
            msg_pings = msg

        timestamp = time.time()

        if room.block_time < timestamp and _global_block < timestamp:
            if report_data and "delay" in _room_roles and room_id in _room_roles["delay"]:
                def callback(room=room, msg=msg_pings):
                    post = fetch_post_id_and_site_from_url(report_data[0])[0:2]

                    if not datahandling.is_false_positive(post) and not datahandling.is_ignored_post(post):
                        _msg_queue.put((room, msg, report_data))

                task = Tasks.later(callback, after=300)

                GlobalVars.deletion_watcher.subscribe(report_data[0], callback=task.cancel)
            else:
                _msg_queue.put((room, msg_pings, report_data))
    def handle(cls, content):
        for k, d in content.items():
            if k in cls._callbacks:
                for cb in cls._callbacks[k]:
                    cb(d)

        if "metasmoke_state" in content:
            if content["metasmoke_state"] == "down":
                log(
                    'info',
                    "{} says metasmoke is down, switching to active ping monitoring."
                    .format(content["location"]))
                chatcommunicate.tell_rooms_with(
                    "debug",
                    "{} says metasmoke is down,".format(content["location"]) +
                    " switching to active ping monitoring.")
                # This is an exception, to prevent circular import.
                # Other classes should not do the same. Always use Metasmoke.ms_down(). (20 May 2020)
                GlobalVars.MSStatus.set_down()
                Tasks.later(SocketScience.check_recent_pings, after=90)

            if content["metasmoke_state"] == "up":
                log(
                    'info',
                    '{} says metasmoke is up, disabling ping monitoring.'.
                    format(content["location"]))
                chatcommunicate.tell_rooms_with(
                    "debug",
                    "{} says metasmoke is up,".format(content["location"]) +
                    " disabling ping monitoring.")
                # This is an exception, to prevent circular import.
                # Other classes should not do the same. Always use Metasmoke.ms_up(). (20 May 2020)
                GlobalVars.MSStatus.set_up()

        if "ping" in content:
            cls._pings.append({
                "timestamp": content["ping"],
                "location": content["location"]
            })
            if cls._switch_task is not None:
                cls._switch_task.cancel()
Esempio n. 8
0
    def handle(cls, content):
        for k, d in content.items():
            if k in cls._callbacks:
                for cb in cls._callbacks[k]:
                    cb(d)

        if "metasmoke_state" in content:
            if content["metasmoke_state"] == "down":
                log('info', "{} says metasmoke is down, switching to active ping monitoring."
                            .format(content["location"]))
                GlobalVars.metasmoke_down = True
                Tasks.later(SocketScience.check_recent_pings, after=90)

            if content["metasmoke_state"] == "up":
                log('info', '{} says metasmoke is up, disabling ping monitoring.'.format(content["location"]))
                GlobalVars.metasmoke_down = False

        if "ping" in content:
            cls._pings.append({"timestamp": content["ping"], "location": content["location"]})
            if cls._switch_task is not None:
                cls._switch_task.cancel()
Esempio n. 9
0
# noinspection PyProtectedMember
def check_socket_connections():
    for client in chatcommunicate._clients.values():
        if client.last_activity and (datetime.utcnow() - client.last_activity
                                     ).total_seconds() >= 60:
            os._exit(10)


# noinspection PyProtectedMember
def restart_automatically():
    Metasmoke.send_statistics()
    os._exit(5)


Tasks.periodic(check_socket_connections, interval=90)
Tasks.later(restart_automatically, after=21600)

log('info', '{} active'.format(GlobalVars.location))
log('info', 'MS host: {}'.format(GlobalVars.metasmoke_host))


def setup_websocket(attempt, max_attempts):
    try:
        ws = websocket.create_connection("wss://qa.sockets.stackexchange.com/")
        ws.send("155-questions-active")
        return ws
    except websocket.WebSocketException:
        log(
            'warning',
            'WS failed to create websocket connection. Attempt {} of {}.'.
            format(attempt, max_attempts))
Esempio n. 10
0
# noinspection PyProtectedMember
def check_socket_connections():
    for client in chatcommunicate._clients.values():
        if client.last_activity and (datetime.utcnow() - client.last_activity).total_seconds() >= 60:
            os._exit(10)


# noinspection PyProtectedMember
def restart_automatically():
    Metasmoke.send_statistics()
    os._exit(1)


Tasks.periodic(check_socket_connections, interval=90)
Tasks.later(restart_automatically, after=21600)

log('info', GlobalVars.location)
log('info', GlobalVars.metasmoke_host)

DeletionWatcher.update_site_id_list()

ws = websocket.create_connection("wss://qa.sockets.stackexchange.com/")
ws.send("155-questions-active")

if "first_start" in sys.argv and GlobalVars.on_master:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.s)
elif "first_start" in sys.argv and not GlobalVars.on_master:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.s_reverted)

Tasks.periodic(Metasmoke.send_statistics, interval=600)