def init_unit_of_work(q, store, unit_of_work, *args) -> Dict[str, Any]: """ Function used directly in PyTaskIO class. :param q: The client task queue :param unit_of_work: python code to run :param args: Unit of work arguments :return: """ serialized_uow = serialize_unit_of_work(unit_of_work, *args) # The unit of work gets saved to the store in case the task # fails and the client / user wants to retry uow_metadata = _create_store_key( _create_store_index(store), store, serialized_uow ) # Unit of work gets push onto the task queue & metadata gets updated serialized_uow_meta = serialize_store_data(uow_metadata) # LPUSH returns the queue length after the push operation queue_length = q.lpush(_QUEUE_NAME, serialized_uow_meta) uow_metadata["queue_created"] = get_datetime_now() uow_metadata["queue_length"] = queue_length # Returns metadata back to the caller / user. return uow_metadata
def test_worker(event_loop): dumped_uow = serialize_unit_of_work(send_email, ["Hello", 1]) r.lpush("tasks", dumped_uow) r.lpush("tasks", dumped_uow) r.lpush("tasks", dumped_uow) queue = asyncio.Queue() assert {} == event_loop.run_until_complete(worker(queue))
def test_serialize_unit_of_work(): result = serialize_unit_of_work(send_email, "Hello", 1) assert isinstance(result, bytes) assert (send_email, ["Hello", 1]) == dill.loads(result)