Giter Site home page Giter Site logo

code-munchr / rails-long-running-tasks-readme-v-000 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/rails-long-running-tasks-readme-v-000

0.0 1.0 0.0 381 KB

License: Other

Ruby 75.64% JavaScript 2.44% CSS 3.20% HTML 18.72%

rails-long-running-tasks-readme-v-000's Introduction

Long-Running Tasks In Rails

Objectives

  1. Create a long-running task and observe its impact on the user experience.

Lesson

Most of the things we do in Rails apps revolve around simple tasks that affect only one or a few database objects at a time, and are handled in milliseconds, quickly enough that the impact on the request/response time of our pages is minimal.

Sometimes, however, we need to do something that takes longer to do and heavily impacts page load time, like sending a bunch of emails, uploading large files, manipulating a lot of records, or generating a report. Let's look at how that might happen.

Importing Records From CSV

A common task in many systems is importing records from a Comma-Separated Value (CSV) file. You might need to do this for any number of reasons, from seeding a new database to integrating and sharing data from another system.

Attached is a very simple "customer database" app. It has a list of all customers at /customers.

Our sales team has just aquired some hot leads and wants them in the system right away.

put that coffee down

Turns out we'll be getting new leads via a CSV every week, so instead of importing them manually, we'll just build a feature where the sales team can do it themselves.

We'll let them do the import right from the customers index, so let's just add a route to take the file upload:

# config\routes.rb

  resources :customers, only: [:index]
  post 'customers/upload', to: 'customers#upload'

Then let's add the controls to upload a file to our index view:

# views\customers\index.html.erb

<h1>Our Customers</h1>
<%= form_tag customers_upload_path, multipart: true do %>
  <%= file_field_tag :leads %>
  <%= submit_tag "Import Leads" %>
<% end %>
<ul>
<% @customers.each do |customer| %>
# ...

Finally, let's get into our controller and handle this file upload.

To do that, we first need to examine the file. Check out db/customers.csv. The first row defines the fields, and isn't part of the dataset, so that's called a header.

Each row is ordinal, like an array, so position 0 is email, position 1 is first name, and position 2 is last name.

Armed with all that, we can build our controller method:

# controllers\customers_controller.rb

class CustomersController < ApplicationController
  require 'csv'

  def index
    @customers = Customer.all
  end

  def upload
    CSV.foreach(params[:leads].path, headers: true) do |lead|
      Customer.create(email: lead[0], first_name: lead[1], last_name: lead[2])
    end
    redirect_to customers_path
  end

end

Note: We're using the CSV library (require 'csv'), which is a standard Ruby library, to do the processing. You can learn more about it here

Okay, let's reload /customers, select customers.csv from our project's db directory, and click go.

Wait for it.

wait for it

Keep waiting.

Okay, eventually, the page should refresh and we'll see our new customers. Depending on the speed of your computer, it might have taken upwards of a full minute for 25,000 rows. 25,000 might seem like a lot, but systems routinely process millions and millions of rows of data every day.

Summary

We've seen how something as straightforward as creating records from a data file can take almost a minute for your server to process and reload a page. And while a minute might seem pretty fast to create 25K records, consider it from the standpoint of the user. Even as developers who know how long things take, if we have to wait more than a couple seconds for a page to load, we get frustrated, so clearly this isn't a great experience.

But at least you have time for that coffee now.

that's good coffee

View Long Running Tasks in Rails on Learn.co and start learning to code for free.

rails-long-running-tasks-readme-v-000's People

Contributors

scottcreynolds avatar annjohn avatar blake41 avatar bhollan avatar pletcher avatar pajamaw avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.