Browse Source

Parallel approximate regex

Dmitry Yu Okunev 3 years ago
parent
commit
d899bb3e01
4 changed files with 51 additions and 9 deletions
  1. 3 0
      Gemfile
  2. 2 0
      Gemfile.lock
  3. 45 8
      app/controllers/people_controller.rb
  4. 1 1
      app/models/person.rb

+ 3 - 0
Gemfile

@@ -53,3 +53,6 @@ gem 'rack-cas'
 #gem 'rack-cas', github: 'mephi-ut/rack-cas'
 
 gem 'mysql2'
+
+gem 'atomic'
+

+ 2 - 0
Gemfile.lock

@@ -40,6 +40,7 @@ GEM
       tzinfo (~> 1.1)
     addressable (2.4.0)
     arel (7.1.4)
+    atomic (1.1.99)
     builder (3.2.2)
     byebug (9.0.6)
     coffee-rails (4.2.1)
@@ -165,6 +166,7 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
+  atomic
   byebug
   coffee-rails (~> 4.2)
   jbuilder (~> 2.5)

+ 45 - 8
app/controllers/people_controller.rb

@@ -84,20 +84,57 @@ class PeopleController < ApplicationController
 			end
 		}
 
+		require 'thread'
 
+		require 'atomic'
+		process_count = Atomic.new(0)
+
+		mutex = Mutex.new
 		employees.each do |key, curEmployees|
-			next if curEmployees.size != 1
-			employee = curEmployees[0]
+			process_count.update {|v| v += 1}
+
+			Thread.new do
+				if curEmployees.size != 1
+					process_count.update {|v| v -= 1}
+					next
+				end
+				employee = curEmployees[0]
 
-			ofz_key = key
-			ofz_key = "#{employee["lastname"]}#{employee["firstname"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip if ofz_links[ofz_key].nil?
-			next if ofz_links[ofz_key].nil?
-			next if ofz_links[ofz_key].size != 1
 
-			#puts "#{key}: #{employees[key]}: #{employees}"
-			employees[key][0]["UserID"] = ofz_links[ofz_key][0]
+				ofz_key = key
+				ofz_key = `/var/www/fixok.ut.mephi.ru/root/3rdparty/approximate_regex/approximate_regex /tmp/cps_keys.csv '#{ofz_key}' | tr -d "\n"` if ofz_links[ofz_key].nil?
+				ofz_key = "#{employee["lastname"]}#{employee["firstname"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip if ofz_key == ""
+				if ofz_links[ofz_key].nil? or ofz_links[ofz_key].size != 1
+					process_count.update {|v| v -= 1}
+					next
+				end
+
+				#puts "#{key}: #{employees[key]}: #{employees}"
+				mutex.lock
+				employees[key][0]["UserID"] = ofz_links[ofz_key][0]
+				mutex.unlock
+
+				process_count.update {|v| v -= 1}
+			end
+
+			begin
+				_process_count = process_count.value
+
+				break unless _process_count > 24
+
+				sleep 0.1
+			end while _process_count > 24
 		end
 
+		begin
+			_process_count = process_count.value
+
+			break unless _process_count > 0
+
+			puts "F: process_count == #{_process_count}"
+			sleep 0.11
+		end while _process_count > 0
+
 		employees.each do |key, curEmployees|
 			curEmployees.each do |employee|
 				#puts "#{employee[0]}"

+ 1 - 1
app/models/person.rb

@@ -13,7 +13,7 @@ class Person < ApplicationRecord
 		self.Lastname    = employee["lastname"].strip
 		self.Firstname   = employee["firstname"].strip
 		self.Patronymic  = employee["middlename"].strip unless employee["middlename"].nil?
-		self.PassId      = employee["UserID"] unless self.PassIdIsManual
+		self.PassId      = employee["UserID"] unless self.PassIdIsManual or employee["UserID"] == false
 		self.DateOfBirth = employee["dob"]
 
 		unless employee["WORKPOSITIONS"].nil?