![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Often in our Rails applications we have tasks that may take a lot of time to finish, such as external API requests. This is risky to perform inside our endpoints because it blocks our threads and is not scalable. Here we provide a solution to this problem, using sidekiq to run our heavy work in background
To avoid making the external requests inside our endpoints we dellegate this task to a sidekiq worker and later we have to check periodically the status of the sidekiq job until it is completed.
The flow goes like this:
async_request_id
with "202 Accepted" HTTP status. For security reasons it also returns an async_request_token
, that it's actually the Sidekiq job ID.async_request_id
by parameter and the async_request_token
.First we need to define a class that inherits from AsyncRequest and implements the execute_task
method. Here we will make the request to the external API. We can use parameters set in the
controller endpoint and we need to set the response data using setter methods.
class MyAsyncRequest < AsyncRequest
def execute_task
@response = execute_external_request(params_hash[:param_1], params_hash[:param_2])
if @response.present?
self.some_data = @response['some_data']
self.another_data = @response['another_data']
done
else
failed 'My custom error message'
end
end
end
In the controller endpoint we create an instance of our previously defined class. We can pass parameters that can be used when the external request is made. Then we define two procs to handle both successful and failed state.
class MyController < ActionController::Base
def my_endpoint
@async_request = MyAsyncRequest.init(self, {param_1: 'value_1', param_2: 'value_2'})
done_proc = proc do
render json: { some_data: @async_request.some_data,
another_data: @async_request.another_data }
end
failed_proc = proc do
render json: { error: @async_request.error_message }
end
@async_request.handle(done_proc, failed_proc)
end
end
The only thing left is the javascript code. To start an async request we simply use the
$.getAsyncRequest
method instead of the regular $.get
method.
$.getAsyncRequest(url, optional_timeout_in_milliseconds)
.success(function(response) {
// Do some work
}).error(function(response) {
// Handle error
});
As you can see, the only difference with the regular $.get
method is that you can pass an optional
parameter that is the maximum time to wait for the response. If this time is reached and the request
has not been made the error handler is dispatched.
FAQs
Unknown package
We found that async_endpoint demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.