Advertisement
Guest User

Untitled

a guest
May 3rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import psycopg2
  2. import pybreaker
  3. import time
  4. from datetime import datetime
  5.  
  6. db_breaker = pybreaker.CircuitBreaker(fail_max=2, reset_timeout=60)
  7.  
  8. insert_sql = """INSERT INTO account(username, password, email, created_on, last_login) VALUES(%s, %s, %s, %s, %s)"""
  9.  
  10. username = 'username'
  11. password = 'password'
  12. email = 'email@email.com'
  13.  
  14. @db_breaker
  15. def insert_row():
  16. print('LOGGER | Current PyBreaker STATE: ' + db_breaker.current_state)
  17.  
  18. db_connection = None
  19. db_cursor = None
  20.  
  21. try:
  22. db_connection = psycopg2.connect(db_url)
  23. db_cursor = db_connection.cursor()
  24. except:
  25. raise Exception('EXCEPTION: Error connecting to database!')
  26.  
  27. print('LOGGER | Successfully connected to database!')
  28.  
  29. created_on = datetime.now()
  30. last_login = datetime.now()
  31.  
  32. print('LOGGER | Current PyBreaker STATE: ' + db_breaker.current_state)
  33.  
  34. try:
  35. db_cursor.execute(insert_sql, (username, password, email, created_on, last_login))
  36. db_connection.commit()
  37. print('LOGGER | Successfully inserted row to database!')
  38. except:
  39. raise Exception("EXCEPTION: Error inserting to database!")
  40.  
  41.  
  42. db_cursor.close()
  43. db_connection.close()
  44. print('LOGGER | Successfully closed connection to database!')
  45.  
  46.  
  47. while True:
  48. try:
  49. insert_row()
  50. except Exception as e:
  51. print('LOGGER | Error occured in insert_row function!')
  52. print(e)
  53. print('LOGGER | Current PyBreaker STATE: ' + db_breaker.current_state)
  54. print('LOGGER | INITIATED 10 seconds WAIT...')
  55. time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement