REST : Handle website maintenanceAsk Question

问题:

I have a UI (angular), hosted as a Spring boot App, The UI talks to a REST API (Jersey). When the whole application or part of it in maintenance :

1) I am not sure, how to show the maintenance webpage to anybody who visits now (should it be in angular, or spring boot, and separately for the jersey backend, and how?).

2) for users who are already on some part of the website, how do i inform them that they need to refresh.

3) for users who are already on some part of the website, if they don't refresh, and later when the maintenance is over, if they send a POST request with some old invalid data, how do i check that ?

NOTE : The website does not have any user authentication.

As an example for the 3rd point: The UI shows a list of records from the database, and a user is currently making changes to one of the records, meanwhile the backend goes to maintenance. Now this user submits the changed data, when the backend is available again. But this data is invalid now. how can i identify that, and probably inform the user about it.

回答1:


By maintainance I guess you mean that some databases used by your API are either updated or backedup or something similar. In that case, if the API is not able to process the command I'd let it return a 503 Service Unavailable response code and let the frontend display some nicely formatted information to the user.

In order to determine if clients send old data you can make use of ETag or Last-Modified headers. ETags are basically a hash of the current resource state. If the state changes so does the ETag hash as well. Clients trying to update a state of a resource send the previously fetched ETag hash alongside the new state of the resource to the API and the API can then check if there was an in-between change applied to the resource by some other client and hence return an error to the client that it does not act upon the most recent version. Conditional Requests are defined in RFC 7232.

Regarding your 2nd question I'm not fully sure what you are trying to accomplish. Losely speeking I'd say on using AJAX. What exactly do they have to refresh? Is some resource data changing during the maintainance? If not, why should they even care to refresh? If form input data should be reset, the API may respond with a 205 Reset Content response code, though this indicates that the API has fulfilled the request and informs the client to reset the input data. In case the API can't handle the request due to the mainainance I'd also return the 503 Service Unavailable error and let the frontend issue an information via AJAX to the respective client if that's what you are aming for.

回答2:


1) Since the API can't know anything about the front end, we shouldn't put the logic there. Also, Angular can't know anything about the back end without communicating via API. What would happen if you took the API or Spring Boot down for maintenance? Angular won't know unless you make it handle the responses it receives when those services are unavailable.

2) From Angular, you can have some sort of ping on a timeout to your REST API. Then have a function in the index that checks the status of the response from your API. If maintenance, redirect to maintenance page. That would give you the live connection feel it sounds like you want, though it would add networking overhead and hit your API a little harder. Otherwise, if you can wait until a user does an action that touches your API, you can handle whatever http error code comes back when the services are down.

3) How would you know it was old? Is it a security concern? If there is no way to tell from the data normally transmitted, you could add some kind of timestamp, but that is fairly easy to circumvent. There are many topics on timestamps if that's the way you want to go.

标签: rest spring-boot jersey-2.0 maintenance
© 2014 TuiCode, Inc.