def process_requests(cls): """ Go through any new requests (since this method last ran) and process them. This will apply the creates, updates and deletes to the public space :return: """ # first, pick up our current state from storage workflow_dao = WorkflowState() wfs = workflow_dao.pull("requests") # if we don't have a current state, make one if wfs is None: wfs = WorkflowState() wfs.id = "requests" # get the oldest page of requests and process them dao = Request() requests = dao.list_all_since(wfs.last_request) # produces a generator for r in requests: try: # if the request was created at the time of the last request processed, it is possible it arrived # before or after the cut-off. As we don't have any more than second-level granularity in the timing, # we also need to check to see whether it was one of the ids processed during that second if r.created_date == wfs.last_request and wfs.is_processed(r.id): # if it was created at that time, and it was one of the ones processed, we can skip it continue # if the request is from a later time, or was not processed during the last run, then do the usual # processing if r.action == "update": PublicApi.publish(r) elif r.action == "delete": PublicApi.remove(r) # now, revisit the timing of this request. If the time is the same as the last request date, this is a # request which came in during that same second, but was not processed at the time because it was at the # wrong end of the second. In that case, we just need to add the id to the list of records from that second # which have now been processed if r.created_date == wfs.last_request: wfs.add_processed(r.id) else: # otherwise, this is a whole new second, and we can forget everything that went before and start afresh. wfs.last_request = r.created_date wfs.already_processed = [r.id] except: wfs.save(blocking=True) raise wfs.save(blocking=True)
def test_13_request_iterator(self): # Check we can iterate successfully over lists of Requests sources = RequestFixtureFactory.request_per_day("2001-01", 10) for s in sources: req = Request(s) req.save() time.sleep(2) dao = Request() gen = dao.list_all_since("2001-01-01T00:00:00Z", page_size=5) # set the page size small, to ensure the iterator has to work results = [x for x in gen] assert len(results) == 10 dates = [r.created_date for r in results] comp = deepcopy(dates) comp.sort() # this puts the dates in ascending order (i.e. oldest first) # the point of this comparison is to show that the results came out in the right order. # that is, oldest first assert dates == comp