Commit 105faa0e by Thanh Hung Pham

fox code and remove thread

parent 7d3f85c2
......@@ -3,29 +3,57 @@ require 'logger'
require 'csv'
class Import::CSVReader
attr_reader :thread_count, :logger
attr_reader :logger
def initialize(file, thread_count)
def initialize(file)
@file = file
@thread_count = thread_count
@mutex = Mutex.new
@logger = Logger.new("#{Rails.root}/log/csv_reader.log")
end
def import
@logger.info('Start read data')
workers = (0...thread_count).map do
Thread.new do
begin
puts '=======Start read data======='
begin
csv_text = File.read(@file)
csv = CSV.parse(csv_text, headers: :true)
csv.each do |row|
# Job type information
job_type = import_job_type(row)
# Contact information
contact = import_contact(row)
# Company information
company = import_company(row)
# Category information
category = import_category(row)
# City information
city = import_city(row)
# Job information
job = import_job(row, city, job_type, contact, company)
# Job Category Information
import_job_category(job, category)
end
rescue StandardError => e
logger.error(e.message)
logger.error(e.backtrace)
end
puts '=======End read data======='
@logger.info('End read data')
end
def import_job_type(row)
job_type_name = row['type'].strip unless row['type'].nil?
job_type = JobType.find_or_create_by(name: job_type_name)
# Contact information
job_type
end
def import_contact(row)
contact_name = row['contact name'].strip unless row['contact name'].nil?
contact_email = row['contact email'].strip unless row['contact email'].nil?
contact_phone = row['contact phone'].strip unless row['contact phone'].nil?
......@@ -36,7 +64,10 @@ class Import::CSVReader
contact.phone = contact_phone
contact.save
# Company information
contact
end
def import_company(row)
company_address = row['company address'].strip unless row['company address'].nil?
company_district = row['company district'].strip unless row['company district'].nil?
company_name = row['company name'].strip unless row['company name'].nil?
......@@ -49,14 +80,20 @@ class Import::CSVReader
company.province = company_province
company.save
# Category information
company
end
def import_category(row)
category_name = row['category'].strip unless row['category'].nil?
category = Category.find_or_create_by(name: category_name)
category.name = category_name
category.save
# City information
category
end
def import_city(row)
city_name = row['work place'].strip unless row['work place'].nil?
city_name = city_name.tr('""', '').tr('[]', '') # Remove '["text"]' -> 'text'
......@@ -65,7 +102,10 @@ class Import::CSVReader
city.area = Area.find_by_name('Viet Nam')
city.save
# Job information
city
end
def import_job(row, city, job_type, contact, company)
job_benefit = row['benefit'].strip unless row['benefit'].nil?
job_description = row['description'].strip unless row['description'].nil?
job_level = row['level'].strip unless row['level'].nil?
......@@ -86,23 +126,13 @@ class Import::CSVReader
job.company = company
job.save
# Job Category Information
job
end
def import_job_category(job, category)
job_category = JobCategory.find_or_create_by(job: job, category: category)
job_category.job = job
job_category.category = category
job_category.save
end
rescue StandardError => e
logger.error(e.message)
logger.error(e.backtrace)
end
puts '=======Thread End======='
rescue ThreadError
end
end
end
workers.map(&:join)
@logger.info('End read data')
end
end
......@@ -3,7 +3,6 @@ require 'rubygems'
namespace :import do
desc 'Import CSV'
task csv: :environment do
thread_count = ENV['THREAD_COUNT'] || 1
path_zip = "#{Rails.root}/lib/tasks/jobs.zip"
Utils::Download.new('192.168.1.156', 'training', 'training', path_zip, '*.zip').download_ftp
......@@ -15,7 +14,7 @@ namespace :import do
end
end
Import::CSVReader.new(path_csv, thread_count).import
Import::CSVReader.new(path_csv).import
File.delete(path_zip)
File.delete(path_csv)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment