Exemple #1
0
    def _update_nodes_to_cancel(self, client_id: str, action_id: str) -> int:
        # As always with stop-flags, we can face a bunch of race conditions
        zk_node_path = self._get_node_path(client_id, action_id)

        number_of_nodes_updated = 0

        try:
            for child in self._zk.get_children(zk_node_path):
                abs_path = zk_node_path + "/" + child

                logger.info(f"Updating node {abs_path}")

                try:
                    while True:
                        data, zk_stat = self._zk.get(abs_path)

                        result: FetcherResult = FetcherResult.from_binary(data)

                        # The guy is final - it will not take long for us to cancel it.
                        # The job is finished.
                        # So now we are in a race with a zookeeper listener, that will pass the results downstream.
                        if result.status.final:
                            logger.info(f"{abs_path}: not to be canceled - already finished")
                            break
                        result.status = FetcherStatus.CANCELED

                        new_data = result.to_binary()
                        try:
                            self._zk.set(abs_path, new_data, version=zk_stat.version)
                            number_of_nodes_updated = number_of_nodes_updated + 1
                        except BadVersionError:
                            logger.info(f"{abs_path}: the node was updated meanwhile")
                            continue
                        logger.info(f"{abs_path}: canceled")
                        break

                except NoNodeError:
                    logger.info(f"{abs_path}: the node was deleted meanwhile")
                    # The task was just finished - status was repopted to customer and the node got deleted.
                    # OK. It's not our deal anymore
                    continue
        except NoNodeError:
            # Absorb NoNodeError
            logger.info(f"{zk_node_path}: node not found")

        return number_of_nodes_updated
Exemple #2
0
    def __handle_node_state(self, zk_node_path: str, on_done: DownloadOnDone, content: DownloadableContent):
        def _on_zk_changed(evt):
            self.__on_zk_changed(evt, on_done, content)

        data, _ = self._zk.get(zk_node_path, _on_zk_changed)

        result: FetcherResult = FetcherResult.from_binary(data)

        logger.info("Fetch request %s result = %s", content, result)

        if result.status.final:
            result.update(content)

            # We clean up
            self._zk.delete(zk_node_path)

            on_done(content)
Exemple #3
0
def test_deserialize_state_strange():
    with pytest.raises(Exception):
        FetcherResult.from_binary(STATE_STRANGE_BIN)
Exemple #4
0
def test_deserialize_state_not_final():
    result = FetcherResult.from_binary(STATE_RUNNING_BIN)
    assert not result.status.final
Exemple #5
0
def test_deserialize_state_final():
    result = FetcherResult.from_binary(STATE_DONE_BIN)
    assert result.status.final
Exemple #6
0
def test_deserialize_state():
    assert FetcherResult.from_binary(STATE_DONE_BIN) == FETCHER_DONE_RESULT