run.py 3.29 KB
Newer Older
1
2
3
# run.py

import os
4
import time
Mitchell Moore's avatar
Mitchell Moore committed
5
6
import pika
import sys
7
8

from app import create_app
9
from flask_socketio import SocketIO
10
from flask import flash
11
12
13

config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
14
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
15
socketio = SocketIO(app)
16
17


18
19
20
21
22
def messageReceived(methods=['GET', 'POST']):
    print('message was received!!!')


def check_dir(user, interval):
Mitchell Moore's avatar
Mitchell Moore committed
23
24
    # Todo: Make this mothod in a consumer

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    """
    :param user: (string) username to check for in DB.
    :param interval: (int) Frequency to check in seconds.
    :return: (boolean) if account has been registered.
    """
    seconds = 0

    while seconds < 600:
        querystring = "_" + user + ".done"

        for filename in os.listdir("flat_db/"):
            if filename.endswith(querystring):
                return True
        time.sleep(interval)
        seconds = seconds + interval

    return False


@socketio.on('user connect')
def handle_my_custom_event(json, methods=['GET', 'POST']):
    username = json["user"]
Mitchell Moore's avatar
Mitchell Moore committed
47
    print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
48
49
50
51


@socketio.on('user data')
def ingest_data(json, methods=['GET', 'POST']):
Mitchell Moore's avatar
Mitchell Moore committed
52
    print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
53

54
    # Begin RabbitMQ process. 
Mitchell Moore's avatar
Mitchell Moore committed
55
56
57
58
59
60
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()

    channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

61
62
63
64
65
66
    message = "Hey there" # todo: account info goes here

    channel.basic_publish(
        exchange='direct_logs', routing_key="ohpc", body=message)
    print(" [x] Sent %r:%r" % ("ohpc", message))

Mitchell Moore's avatar
Mitchell Moore committed
67
    channel.basic_publish(
68
69
        exchange='direct_logs', routing_key="ood", body=message)
    print(" [x] Sent %r:%r" % ("ood", message))
Mitchell Moore's avatar
Mitchell Moore committed
70
71
    connection.close()

72
    # Todo: Make the remaining portion of this method in a RabbitMQ consumer
Mitchell Moore's avatar
Mitchell Moore committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    # try:
    #     fullname = json["fullname"]
    #     reason = json["reason"]
    #     username = json["username"]
    #
    #     time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
    #     directory = "flat_db/"
    #     complete_file_name = os.path.join(directory, time_stamp + "_" + username + ".txt")
    #
    #     if not os.path.exists(directory):
    #         os.makedirs(directory)
    #
    #     file = open(complete_file_name, "w")  # create time stamped file to be queued
    #
    #     file.write(fullname + "\n")
    #     file.write(reason)
    #
    #     file.close()
    #     print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
    #     socketio.emit("creating account")
    #
    # except Exception as e:
    #     print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in directory creation: ", e)
    #     socketio.emit("Account creation failed")
98
99
100
101


@socketio.on("validate creation")
def creation_confirmation(json, methods=['GET', 'POST']):
Mitchell Moore's avatar
Mitchell Moore committed
102
103
    # Todo: Make this mthod in a RabbitMQ consumer

104
105
106
    username = json["username"]

    if check_dir(username, 10):
Mitchell Moore's avatar
Mitchell Moore committed
107
        print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
108
109
110
111
112
        socketio.emit("Account created")
    else:
        socketio.emit("Account creation failed")


113
if __name__ == '__main__':
114
115
   # app.run()
    socketio.run(app)