liquid_feedback_frontend
diff model/event.lua @ 1309:32cc544d5a5b
Cumulative patch for upcoming frontend version 4
| author | bsw/jbe |
|---|---|
| date | Sun Jul 15 14:07:29 2018 +0200 (2018-07-15) |
| parents | fd50bdd36a4b |
| children |
line diff
1.1 --- a/model/event.lua Thu Jun 23 03:30:57 2016 +0200 1.2 +++ b/model/event.lua Sun Jul 15 14:07:29 2018 +0200 1.3 @@ -3,6 +3,30 @@ 1.4 1.5 Event:add_reference{ 1.6 mode = 'm1', 1.7 + to = "Unit", 1.8 + this_key = 'unit_id', 1.9 + that_key = 'id', 1.10 + ref = 'unit', 1.11 +} 1.12 + 1.13 +Event:add_reference{ 1.14 + mode = 'm1', 1.15 + to = "Area", 1.16 + this_key = 'area_id', 1.17 + that_key = 'id', 1.18 + ref = 'area', 1.19 +} 1.20 + 1.21 +Event:add_reference{ 1.22 + mode = 'm1', 1.23 + to = "Policy", 1.24 + this_key = 'policy_id', 1.25 + that_key = 'id', 1.26 + ref = 'policy', 1.27 +} 1.28 + 1.29 +Event:add_reference{ 1.30 + mode = 'm1', 1.31 to = "Issue", 1.32 this_key = 'issue_id', 1.33 that_key = 'id', 1.34 @@ -33,6 +57,13 @@ 1.35 ref = 'member', 1.36 } 1.37 1.38 +function Event:by_member_id(member_id) 1.39 + return Event:new_selector() 1.40 + :add_where{ "member_id = ?", member_id } 1.41 + :add_order_by("id DESC") 1.42 + :exec() 1.43 +end 1.44 + 1.45 function Event.object_get:event_name() 1.46 return ({ 1.47 issue_state_changed = _"Issue reached next phase", 1.48 @@ -136,40 +167,57 @@ 1.49 1.50 end 1.51 1.52 -function Event:send_next_notification() 1.53 - 1.54 - local notification_event_sent = NotificationEventSent:new_selector() 1.55 - :optional_object_mode() 1.56 - :for_update() 1.57 - :exec() 1.58 - 1.59 - local last_event_id = 0 1.60 - if notification_event_sent then 1.61 - last_event_id = notification_event_sent.event_id 1.62 - end 1.63 +function Event:get_events_after_id(id) 1.64 + return ( 1.65 + Event:new_selector() 1.66 + :add_where{ "event.id > ?", id } 1.67 + :add_order_by("event.id") 1.68 + :exec() 1.69 + ) 1.70 +end 1.71 + 1.72 + 1.73 + 1.74 +Event.handlers = {} 1.75 + 1.76 +function Event:add_handler(func) 1.77 + table.insert(Event.handlers, func) 1.78 +end 1.79 + 1.80 +function Event:process_stream(poll) 1.81 + 1.82 + db:query('LISTEN "event"') 1.83 + 1.84 + local last_event_id = EventProcessed:get_last_id() 1.85 1.86 - local event = Event:new_selector() 1.87 - :add_where{ "event.id > ?", last_event_id } 1.88 - :add_order_by("event.id") 1.89 - :limit(1) 1.90 - :optional_object_mode() 1.91 - :exec() 1.92 + while true do 1.93 + 1.94 + local events = Event:get_events_after_id(last_event_id) 1.95 + 1.96 + for i_event, event in ipairs(events) do 1.97 + last_event_id = event.id 1.98 + EventProcessed:set_last_id(last_event_id) 1.99 + for i_handler, event_handler in ipairs(Event.handlers) do 1.100 + event_handler(event) 1.101 + end 1.102 + event:send_notification() 1.103 + end 1.104 1.105 - if event then 1.106 - if last_event_id == 0 then 1.107 - db:query{ "INSERT INTO notification_event_sent (event_id) VALUES (?)", event.id } 1.108 + if poll then 1.109 + while not db:wait(0) do 1.110 + if not poll({[db.fd]=true}, nil, nil, true) then 1.111 + goto exit 1.112 + end 1.113 + end 1.114 else 1.115 - db:query{ "UPDATE notification_event_sent SET event_id = ?", event.id } 1.116 + db:wait() 1.117 end 1.118 - 1.119 - event:send_notification() 1.120 + while db:wait(0) do end -- discard multiple events 1.121 1.122 - if config.notification_handler_func then 1.123 - config.notification_handler_func(event) 1.124 - end 1.125 - 1.126 - return true 1.127 - 1.128 end 1.129 1.130 + ::exit:: 1.131 + 1.132 + db:query('UNLISTEN "event"') 1.133 + 1.134 end