give up retrying when max retry count exceeded

Errors like "Filetype was not recognised" causes infinite retry loop.
Add MAX_RETRY and give up retrying when max retry count exceeded.