Handle 422 status when uploading artifacts that already exists
Background
In gitlab#36516 (closed) we observed a bug that occurred while the Runner retried to upload an artifacts. It turned out that the Runner had already tried to upload the artifact file but then Rails failed to update project_statistics.build_artifacts_size and returned 500 error. The Runner retried the upload and received further 500 errors repeatedly due to violation of a foreign key constraint, meaning that the artifact already resulted uploaded during the first operation.
The specific bug was then fixed in gitlab!20852 (merged) and we have never seen the same error so far.
Proposal
To avoid similar scenarios from happening we could catching the foreign key constraint violation on the Rails side and return 422 Unprocessable Entity status code. This way the Runner can distinguish it from a 400 Bad Request and stop retrying.
On the Runner side we would need to handle this new status code.