Guest User

Untitled

a guest
Apr 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. # purpose of this is to show a simplified form of how i would organize routing of events to handlers
  2. # e.g. creating agents or handlers for various pantheon events
  3.  
  4. # the usage is to call route() with an event payload. route will then call all relevant handlers with that payload
  5.  
  6. # sample stubbed handlers.
  7. # in real life, these would be fancy and complicated. this is just to show the logic for choosing what to invoke so they are stubs
  8. def notify(payload):
  9. print "notifying {}".format(payload)
  10.  
  11. def provision(payload):
  12. print "provisioning {}".format(payload)
  13.  
  14.  
  15. # the data - a map that defines which handler(s) are invoked based on the event's payload
  16. # idea is that as the set of handlers grows, can expand the list of data without touching the code
  17. # could make it much fancier:
  18. # - more metadata
  19. # - fancier wildcard scheme / pattern matching
  20. # - could move into a real data store, could put an admin ui over it, etc
  21. WILDCARD = '*'
  22. HANDLER_MAP = [
  23. {
  24. "program": "*",
  25. "model": "task",
  26. "event": "start",
  27. "handler": notify,
  28. },
  29. {
  30. "program": "google-regular",
  31. "model": "task",
  32. "event": "start",
  33. "handler": provision,
  34. },
  35. ]
  36.  
  37. REQUIRED_FIELDS = ["program", "model", "event"]
  38.  
  39. # determine if a handler is a match for the given payload
  40. # current logic is that it must have identical values for all metadata in the payload, or have a wildcard for a value in the payload
  41. def matcher(payload, row):
  42. for key, value in payload.iteritems():
  43. if row[key] != value and row[key] != WILDCARD:
  44. return False
  45. return True
  46.  
  47. # given a payload, call the appropriate handlers for it
  48. def route(payload):
  49. # reject payload if not a dictionary
  50. if type(payload) is not dict or not all(k in payload for k in REQUIRED_FIELDS):
  51. print "invalid payload"
  52. return
  53. # find appropriate routes
  54. matches = filter(lambda r: matcher(payload, r), HANDLER_MAP)
  55.  
  56. # dispatch
  57. for match in matches:
  58. match["handler"](payload)
  59.  
  60.  
  61. # code for demonstrating this stuff
  62. # there is a list of tests, each represents a mock event payload that we'll route to the appropriate dispatchers
  63. # tests include a payload with missing fields, and one that is the wrong data type
  64. tests = [{
  65. "program": "google-regular",
  66. "model": "task",
  67. "event": "start"
  68. },
  69. {
  70. "program": "uber",
  71. "model": "task",
  72. "event": "start"
  73. },
  74. {
  75. "program": "park",
  76. "model": "task",
  77. "event": "start"
  78. },
  79. # a broken payload with missing fields
  80. {
  81. "broken": "payload"
  82. },
  83. # a broken payload of the wrong data type
  84. "invalid data type"
  85. ]
  86.  
  87.  
  88. print "routing test"
  89. print "============="
  90. for test_payload in tests:
  91. print "test payload:"
  92. print test_payload
  93. print "------------"
  94. route(test_payload)
  95. print "======DONE========\n"
Add Comment
Please, Sign In to add comment