Initiating a long-running batch process from RESTful service might sound odd but this is an actual and very valid use case.
When you are not sure about the use case always start with file or image uploads. — William Shakespeare
Converting MP4 to MP3 by uploading a file capped at 25 MB on our imaginary website www.mp4tomp3bywilliams.com
Just to bring you under this use case, say that you are required to implement this functionality using a RESTful service. Just because you have to be lockstep with your organization where everything is achieved using a Web Service, we will be implementing this functionality as a service too.
You must have definitely seen this in one or more sites you use in your day-to-day. Most of these sites follow this strategy. Let’s get into the recipe.
We will be using the following ingredients,
- Client-UI App
- Server - Our RESTful service — Let's call it Mp4toMp3 service
- HTTP code 202 — ACCEPTED
- HTTP code 303 — SEE OTHER
- HTTP code 200 — OK
The file will be uploaded from the UI and say that we drop this into one of the following S3 buckets, DropBox or Google Drive and get a Webhook that is accessible by our RESTful service.
Using the Link uploaded to a cloud drive we will now call our POST web service. In this case, the call to mp4tomp3 doesn't really abide by the RESTful resource rules. Meaning a good RESTful endpoint will be a valid plural noun like employees or customers but those resources which sound like more of a RPC call is called the Controller Resource . So in our case mp4tomp3 is a controller resource.
-> 202 Accepted
So we trigger a POST to the controller resource, which returns a status code 202 Accepted. This just means that the process is submitted. Using the Location header in the response, the direct URL to infer the status of the submitted job is given to the client. This is again your implementation, you build this URL in the backend which is a link to the extension of this controller resource where 48578 could be the number you assigned or the PID from the OS it's your choice.
You can now call this returned URL that points to the Status of the just submitted job using the following URL and initiate an HTTP GET call. You can now expect 2 different HTTP status codes in return 303 and 200. We will be talking about 303 shortly. Let's talk about 200 for now.
When we call the above URL we are essentially calling to know the status of the job we submitted. So we need to return a resource to the client when this URL is called and we call this as a process resource. Below is an ideal representation of the process resource when everything is going well.
The above message from the GET call can be painted by the UI as follows,
And when something is fishy the process resource can look like this,
"error":"Unable to access the file"
So when we invoke the GET on the provided process resource URL we return 200 for both to sense progress as well as failure.
HTTP 303 is Redirect to New URI . RESTful is all about exploiting all available HTTP methods instead of you implementing it by yourself. So when we call the process status URL and get a 303 it just means that our async process is completed or at least that’s what you need to implement on the server-side. This redirect can just bring you back to a Conversion Successful message on the UI with a button to download the content or you can get more creative here. But you need to end your async call with a grand 303 to give it a sense of completion.
That how you deal with long-running RESTful service calls.