![]() ![]() Sweet ASCII art, right? Load up your app again, enter your phone number and wait for the call. We’ll complete this example using Sidekiq, but I encourage you to read up on the options and choose the one that is right for your application. I like Sidekiq, it uses Redis as a store for the jobs (Delayed Job integrates well with Active Record, but this can be slow) and thread based workers (Resque uses process based workers, which can take up a lot more memory). There’s a list of all the adapters available in the Active Job documentation as well as their features. Active Job supports a number of popular Ruby job queues including Sidekiq, Resque and Delayed Job. In order to move the work into the background we need to supply a different back end. However, out of the box Active Job only supplies a default implementation that immediately executes jobs inline. What is happening? Choosing a queueĪctive Job is great because it gives us a very simple interface to queue up jobs. But the log is still showing more than a second to process this. Start up the server again, enter your phone number and wait for the call. Run the tests again and you’ll see success. First, let’s take a look at the whole action we’re dealing with: We’re going to do that now using Active Job. Like I said in my introduction, the solution to requests that contain long running, blocking tasks like this is to move the task into the background and perform it outside of the web request. Kill the server with Ctrl-C and let’s sort this out. ![]() Whilst this is reasonable when you are starting up a project or not dealing with a lot of traffic, over time this is going to hurt. That’s right, loading up the home page took just 38ms and starting the call took just over a second. This all looks great, right? “What’s Phil’s problem?”, you might be asking. Enter your number and submit the form, you’ll see a success message and then receive a phone call. You’ll see a form asking for your phone number. Open up the application in your browser using the public URL from your tunnel. Now we’re ready to test this application. In a separate console tab, set up your tunnel.Migrate the database (there’s no migrations for now, but this keeps the application happy):.$ git clone https : ///TwilioDevEd/clicktocall-rails To run the application, grab your Twilio credentials from your account dashboard and the phone number, then: Redis (check out the quick start guide for redis to get it up and running).A way to tunnel from a public URL to your localhost (I really like ngrok).A Twilio number that can make phone calls.A Twilio account (sign up for a free Twilio account if you don’t have one already).To complete this you’ll need a few things: You can build the application up using the tutorial or grab the application from GitHub. Let’s take a look at Click to Call, a simple form on your website that takes a user’s phone number and calls them back. Rather than build one up right now, we’re going to use one of the example applications available in the Twilio tutorials. In order to show how we can speed up an application’s response times by queueing tasks to happen in the background, we’re going to need an application to test against. In this post we will explore how we can Active Job to speed up our application. Since version 4.2 Rails has included Active Job, a library which makes it easy to delay long running tasks and perform them in a background queue. This allows your application server to respond to requests swiftly and get the job done without affecting performance for your site’s users or tying up web server processes. The best thing to do with long running tasks like this is move them away from the request itself and perform them in the background. If you’re using Twilio within an application the last point might stick out at you. ![]() All of these things take a relatively long time compared to other actions normally performed during the course of a request. I’m talking things like sending emails, generating PDF or CSV files or making HTTP requests to 3rd party APIs. Performing long running, blocking tasks during the course of a request is a top way to slow down responses for any web application. There are many ways to improve the performance of a web application in Rails and I want to look at one of those today. A swift application response causes less stress on servers trying to respond to many users too. Snappy websites make for better user experiences, higher conversion rates and better user retention.
0 Comments
Leave a Reply. |