Git Product home page Git Product logo

process_csv_with_resque's Introduction

process_csv_with_resque

This is a small example project of processing CSV file with Resque. Checkout this project and you can try it easily.

Setup

(Install Redis before you run this app)
$ git clone [email protected]:johnny-miyake/process_csv_with_resque.git
$ cd process_csv_with_resque/
$ bundle install
$ rake db:migrate
$ bundle exec rake resque:work QUEUE="*"
(open another terminal and go to project dir)
$ rails s

Usage

  1. Open two tabs and access to http://localhost:3000(tab1) and http://localhost:3000/resque(tab2).
  2. Select a CSV file ( http://localhost:3000/clients.csv is available) and upload it.
  3. Reload tab1 and confirm that the data you have just uploaded has not been saved in DB yet.
  4. Reload tab2 and confirm that a background job is being executed.
  5. Wait for 30 seconds.
  6. Reload tab1 again and confirm that the data you have uploaded is shown in the list.
  • Relaunch the Resque worker every time you modify a worker script (e.g. app/workers/import_csv.rb).

Explanation

Followings are explanations of some principal files on this application.

configuration files

  • config/redis/development.conf This is the configuration file for redis. It is used for launching Redis server and also used for initializing Resque client in config/initializers/resque.rb. config/initializers/resque.rb.
daemonize yes
port 6379
logfile ./log/redis_development.log
dbfilename ./db/development.rdb
  • config/initializers/redis.rb This is an initialize
require "redis"
redis_conf_path = Rails.root.join("config/redis", "#{Rails.env}.conf")
redis_conf = File.read(redis_conf_path)
port = /port.(\d+)/.match(redis_conf)[1]
host = "localhost"
`redis-server #{redis_conf_path}`
res = `ps aux | grep redis-server`
unless res.include?("redis-server") && res.include?(redis_conf_path.to_s)
  raise "Couldn't start redis"
end
# You can do below if you use Redis directly in your application.
# $redis = Redis.new(host: host, port: port)
  • config/initializers/resque.rb This script initializes a Resque client.
redis_conf_path = Rails.root.join("config/redis", "#{Rails.env}.conf")
redis_conf = File.read(redis_conf_path)
port = /port.(\d+)/.match(redis_conf)[1]
host = "localhost"
Resque.redis = Redis.new(host: host, port: port)
  • lib/tasks/resque.rake This is a Rake task used by Resque.
require "resque/tasks"
task "resque:setup" => :environment

Worker

app/workers/import_csv.rb This is the worker for processing CSV file. It starts processing CSV after 30 seconds later it is called.

class ImportCsv
  @queue = :default

  def self.perform(file)
    require "csv"
    sleep 30
    CSV.foreach(file, headers: true) do |row|
      Client.create(
        client_name: row[0],
        roman_name: row[1],
        tel: row[2]
      )
    end
  end
end

Controller

app/controllers/clients_controller.rb This is the controller for uploading CSV file. Job for uploading CSV file is enqueued in create_by_csv action.

class ClientsController < ApplicationController
  def index
    @clients = Client.all
  end

  def create_by_csv
    path = "tmp/clients.csv"
    File.open path, "wb" do |f|
      f.puts params[:csv].read
    end
    Resque.enqueue ImportCsv, path
    redirect_to clients_path, notice: "enqueued."
  end

  def destroy
    @client = Client.find(params[:id])
    @client.destroy
    redirect_to clients_path
  end
end

Operation check

This sample project is performed an operation check on ruby 1.9.3p392 and Rails 3.2.13.

process_csv_with_resque's People

Contributors

omiyakejtakuma avatar j-miyake avatar

Watchers

James Cloos avatar Bui Trung Kien avatar

Forkers

khanhhd

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.