Skip to content

Commit

Permalink
Merge pull request #54 from masutaka/refactor-thread-logic
Browse files Browse the repository at this point in the history
Refactor Thread logic using `parallel` gem
  • Loading branch information
masutaka committed Mar 30, 2016
2 parents 6e0f6ce + 59bca62 commit d282cb9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
github-nippou (2.0.0.beta5)
activesupport
octokit
parallel
thor

GEM
Expand All @@ -24,6 +25,7 @@ GEM
multipart-post (2.0.0)
octokit (4.3.0)
sawyer (~> 0.7.0, >= 0.5.3)
parallel (1.8.0)
rake (11.1.1)
sawyer (0.7.0)
addressable (>= 2.3.5, < 2.5)
Expand Down
1 change: 1 addition & 0 deletions github-nippou.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'activesupport'
spec.add_dependency 'octokit'
spec.add_dependency 'parallel'
spec.add_dependency 'thor'

spec.add_development_dependency 'bundler'
Expand Down
20 changes: 7 additions & 13 deletions lib/github/nippou/commands.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'parallel'
require 'thor'

module Github
Expand All @@ -17,19 +18,12 @@ class Commands < Thor
desc 'list', "Displays today's GitHub events formatted for Nippou"
def list
lines = []
threads = []
mutex1 = Mutex::new
mutex2 = Mutex::new

thread_num.times do |i|
threads << Thread.start do
while user_event = mutex1.synchronize { user_events.pop } do
line = format_line(user_event, i)
mutex2.synchronize { lines << line }
end
end
mutex = Mutex::new

Parallel.each_with_index(user_events, in_threads: thread_num) do |user_event, i|
line = format_line(user_event, i)
mutex.synchronize { lines << line }
end
threads.each(&:join)

puts sort(lines)
end
Expand All @@ -48,7 +42,7 @@ def user_events
end

def format_line(user_event, i)
puts "#{i} : #{user_event.html_url}" if ENV['GITHUB_NIPPOU_DEBUG']
puts "#{i % thread_num} : #{user_event.html_url}" if ENV['GITHUB_NIPPOU_DEBUG']
issue = issue(user_event)
line = "* [%s - %s](%s) by %s" %
[issue.title.markdown_escape, user_event.repo.name, user_event.html_url, issue.user.login]
Expand Down

0 comments on commit d282cb9

Please sign in to comment.