示例#1
0
def monitoring_background_task():
    generated_alerts = []

    while True:
        try:
            if not generated_alerts:
                generated_alerts = generate_alerts()
                set_data_to_memcache(name="GENERATED_ALERTS",
                                     data=generated_alerts)
                alerts_from_db = wrap_get_ongoing_extended_overdue_events()
                set_data_to_memcache(name="ALERTS_FROM_DB",
                                     data=alerts_from_db)
                candidate_alerts = candidate_alerts_generator.main(
                    generated_alerts_list=generated_alerts, db_alerts_dict=alerts_from_db)
                set_data_to_memcache(name="CANDIDATE_ALERTS",
                                     data=candidate_alerts)
                set_data_to_memcache(name="ISSUES_AND_REMINDERS",
                                     data=wrap_get_issue_reminder())

                rainfall_data = execute_get_all_site_rainfall_data()
                set_data_to_memcache(name="RAINFALL_DATA",
                                     data=rainfall_data)

                emit_data("receive_generated_alerts")
                emit_data("receive_alerts_from_db")
                emit_data("receive_candidate_alerts")
                emit_data("receive_issues_and_reminders")
                emit_data("receive_rainfall_data")

            elif datetime.now().minute % 5 == 1:
                print()
                system_time = datetime.strftime(
                    datetime.now(), "%Y-%m-%d %H:%M:%S")
                print(f"{system_time} | Websocket running...")

                try:
                    generated_alerts = generate_alerts()
                    set_data_to_memcache(
                        name="GENERATED_ALERTS", data=generated_alerts)
                    alerts_from_db = wrap_get_ongoing_extended_overdue_events()
                    set_data_to_memcache(
                        name="ALERTS_FROM_DB", data=alerts_from_db)
                    set_data_to_memcache(name="CANDIDATE_ALERTS",
                                         data=candidate_alerts_generator.main(
                                             generated_alerts_list=generated_alerts,
                                             db_alerts_dict=alerts_from_db)
                                         )
                    print(f"{system_time} | Done processing Candidate Alerts.")
                except Exception as err:
                    print(err)
                    raise

                emit_data("receive_generated_alerts")
                emit_data("receive_candidate_alerts")
                emit_data("receive_alerts_from_db")

            # Update rainfall summary data
            elif datetime.now().minute in [15, 45]:
                rainfall_data = execute_get_all_site_rainfall_data()
                set_data_to_memcache(name="RAINFALL_DATA",
                                     data=rainfall_data)
                emit_data("receive_rainfall_data")

        except Exception as err:
            print("")
            print("Monitoring Thread Exception")
            var_checker("Exception Detail", err, True)
            print(traceback.format_exc())
            DB.session.rollback()

        SOCKETIO.sleep(60)  # Every 60 seconds in production stage
示例#2
0
def communication_background_task():
    global MESSAGES
    global ROOM_MOBILE_IDS
    inbox_messages_arr = MESSAGES["inbox"]
    is_first_run = False
    ground_meas_run = False

    while True:
        try:
            is_first_run, ground_meas_run = process_ground_measurement_reminder(
                is_first_run, ground_meas_run)

            updates = get_sms_user_updates()
            update_process_start = datetime.now()

            for row in updates:
                query_start = datetime.now()

                mobile_id = row.mobile_id
                update_source = row.update_source

                inbox_index = next(
                    (index
                     for (index, row_arr) in enumerate(inbox_messages_arr)
                     if row_arr["mobile_details"]["mobile_id"] == mobile_id),
                    -1)

                if update_source == "inbox":
                    msgs = get_latest_messages(mobile_id)
                    msgs_schema = get_messages_schema_dict(msgs)

                    if inbox_index > -1:
                        message_row = inbox_messages_arr[inbox_index]
                        del inbox_messages_arr[inbox_index]
                    else:
                        message_row = {
                            "mobile_details":
                            get_user_mobile_details(mobile_id)
                        }
                    message_row["messages"] = msgs_schema
                    inbox_messages_arr.insert(0, message_row)

                    MESSAGES["inbox"] = inbox_messages_arr
                elif update_source == "outbox":
                    if inbox_index > -1:
                        msgs = get_latest_messages(mobile_id)
                        msgs_schema = get_messages_schema_dict(msgs)

                        MESSAGES["inbox"][inbox_index][
                            "messages"] = msgs_schema

                    unsent_messages_arr = get_unsent_messages(duration=1)
                    unsent_messages = format_unsent_messages(
                        unsent_messages_arr)

                    MESSAGES["unsent"] = unsent_messages

                    # CHECK FOR UPDATES IN MOBILE ID ROOM
                    if mobile_id in ROOM_MOBILE_IDS.keys():
                        if inbox_index > -1:
                            msgs_schema = MESSAGES["inbox"][inbox_index][
                                "messages"]
                        else:
                            msgs = get_latest_messages(mobile_id)
                            msgs_schema = get_messages_schema_dict(msgs)

                        ROOM_MOBILE_IDS[mobile_id]["details"][
                            "messages"] = msgs_schema

                        SOCKETIO.emit("receive_mobile_id_room_update",
                                      ROOM_MOBILE_IDS[mobile_id]["details"],
                                      room=mobile_id,
                                      namespace="/communications")
                elif update_source == "blocked_numbers":
                    if inbox_index > -1:
                        del MESSAGES["inbox"][inbox_index]
                elif update_source == "inbox_tag":
                    if inbox_index > -1:
                        msgs = get_latest_messages(mobile_id)
                        msgs_schema = get_messages_schema_dict(msgs)
                        inbox_messages_arr[inbox_index][
                            "messages"] = msgs_schema

                query_end = datetime.now()

                delete_sms_user_update(row)

                emit_data("receive_latest_messages")

                print("")
                print("GET MESSAGE ON MEMCACHE (WS)",
                      (query_end - query_start).total_seconds())
                print("")

            update_process_end = datetime.now()

            if updates:
                print("")
                print(f"COMMS UPDATE PROCESS LOOP (WS) {len(updates)} updates",
                      (update_process_end -
                       update_process_start).total_seconds())
                print("")
        except Exception as err:
            print("")
            print("Communication Thread Exception")
            var_checker("Exception Detail", err, True)
            print(traceback.format_exc())
            DB.session.rollback()
            pass

        SOCKETIO.sleep(0.5)