-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
210 lines (162 loc) · 5.44 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require 'active_record'
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/string/strip'
require 'erb'
require 'fileutils'
require 'json'
require 'pathname'
require 'pg'
require 'yaml'
require 'zlib' # needed by active record tasks
require_relative 'lib/database_connection'
require_relative 'lib/crawl_domain'
class SeedLoader
def load_seed
load "#{ActiveRecord::Tasks::DatabaseTasks.db_dir}/seeds.rb"
end
end
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["SLITHER_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.seed_loader = SeedLoader.new
config.database_configuration = ActiveRecord::Base.configurations
end
load "active_record/railties/databases.rake"
# db:load_config can be overriden manually
Rake::Task["db:seed"].enhance(["db:load_config"])
Rake::Task["db:load_config"].clear
# define Rails' tasks as no-op
Rake::Task.define_task("db:environment").clear
if Rake::Task.task_defined?("db:test:deprecated")
Rake::Task["db:test:deprecated"].clear
end
namespace :db do
desc "Create a migration (parameters: NAME, VERSION)"
task :create_migration do
unless ENV["NAME"]
puts "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`"
exit
end
name = ENV["NAME"]
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
ActiveRecord::Migrator.migrations_paths.each do |directory|
next unless File.exist?(directory)
migration_files = Pathname(directory).children
if duplicate = migration_files.find { |path| path.basename.to_s.include?(name) }
puts "Another migration is already named \"#{name}\": #{duplicate}."
exit 1
end
end
filename = "#{version}_#{name}.rb"
dirname = ActiveRecord::Migrator.migrations_paths.first
path = File.join(dirname, filename)
ar_maj = ActiveRecord::VERSION::MAJOR
ar_min = ActiveRecord::VERSION::MINOR
base = "ActiveRecord::Migration"
base += "[#{ar_maj}.#{ar_min}]" if ar_maj >= 5
FileUtils.mkdir_p(dirname)
File.write path, <<-MIGRATION.strip_heredoc
class #{name.camelize} < #{base}
def change
end
end
MIGRATION
puts "Created new migration at:\n"
puts " #{path}"
end
end
desc "pull all websites currently in db"
task :pull_all do
puts "Pulling all #{Website.count} sites\n"
require_relative 'lib/pull_website'
start_time = Time.now
PullTime = Struct.new(:site_name, :duration)
errors_thrown = []
threads = []
pull_durations = []
mutex = Mutex.new
num_threads = (ENV['SLITHER_NUM_THREADS'] || 1).to_i
num_threads.times.each do
threads << Thread.new do
until Website.where(pull_count: 0).empty? do
website = nil
mutex.synchronize do
ActiveRecord::Base.transaction do
website = Website.where(pull_count: 0).first
website.update_attributes!(pull_count: 1) if website
end
end
if website
logfile = File.open("log/#{website.url}.log", 'a')
puts "Pulling #{website.url}..."
logfile.write "\nPulling #{website.url}... "
begin
local_start = Time.now
PullWebsite.run(website)
duration = Time.now - local_start
mutex.synchronize do
pull_durations << PullTime.new(website.url, duration)
end
logfile.puts "done."
puts "finished #{website.url} in #{duration.round} seconds."
rescue => e
puts e.message
puts e.backtrace
errors_thrown << {
id: website.id,
url: website.url,
error: e.message,
trace: e.backtrace
}
logfile.puts "error!"
puts "error thrown pulling #{website.url}"
end
logfile.close
end
end
end
end
threads.each(&:join)
dur = Time.now - start_time
pull_durations.sort_by!(&:duration)
puts "\nfinished all in #{dur.round} seconds."
puts "\nmedian time to crawl: #{pull_durations[pull_durations.size / 2].duration}"
puts "5 slowest websites:"
pull_durations
.reverse
.take(5)
.each { |pd| puts " #{pd.site_name}: #{pd.duration} seconds" }
unless errors_thrown.empty?
File.open('errors.json', 'w') { |f| f.write(JSON.generate(errors_thrown)) }
puts "\n\n****************************************************************\n"
puts "#{errors_thrown.size} unhandled exceptions written to errors.json"
puts "\n****************************************************************"
end
end
desc "pull a single website. Example usage: `rake pull_single URL=zerohedge.com` do note include the protocol"
task :pull_single do
url = ENV['URL']
unless url
puts "No URL specified. Example usage: `rake pull_single URL=zerohedge.com` do note include the protocol"
exit 1
end
website = Website.find_by(url: url)
unless website
print "No website found in the database for #{url}, do you want to add it? [y/n] "
unless STDIN.gets.chomp.downcase == "y"
puts "okay... whatever"
exit 0
end
website = Website.create! url: url
end
print "Pulling #{url}... "
require_relative 'lib/pull_website'
error_count = PullWebsite.run(website) || 0
puts "done with #{error_count} errors."
end
desc "Crawl"
task :crawl do
CrawlDomain.run(ENV['DOMAIN'])
end