2012-06-21 02:25:35 +04:00
|
|
|
require 'rubygems'
|
2012-07-15 22:14:46 +04:00
|
|
|
require 'bundler'
|
|
|
|
|
|
|
|
Bundler.setup
|
|
|
|
Bundler.require
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
|
|
|
|
desc "Load the environment"
|
|
|
|
task :environment do
|
|
|
|
env = ENV["SINATRA_ENV"] || "development"
|
2012-07-14 10:31:05 +04:00
|
|
|
Sinatra::Base.environment = env
|
|
|
|
Mongoid.load!("config/mongoid.yml")
|
|
|
|
Mongoid.logger.level = Logger::INFO
|
2012-07-17 01:24:12 +04:00
|
|
|
module CommentService
|
|
|
|
class << self; attr_accessor :config; end
|
|
|
|
end
|
|
|
|
|
|
|
|
CommentService.config = YAML.load_file("config/application.yml")
|
2012-07-24 00:15:44 +04:00
|
|
|
|
|
|
|
Dir[File.join(File.dirname(__FILE__),'models', '**', '*.rb')].each {|file| require file}
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
2012-07-16 07:40:29 +04:00
|
|
|
namespace :test do
|
2012-07-17 03:50:54 +04:00
|
|
|
task :nested_comments => :environment do
|
2012-07-16 07:40:29 +04:00
|
|
|
puts "checking"
|
|
|
|
50.times do
|
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
|
|
|
User.delete_all
|
2012-07-17 03:35:34 +04:00
|
|
|
Notification.delete_all
|
2012-07-17 07:05:16 +04:00
|
|
|
Subscription.delete_all
|
2012-07-16 07:40:29 +04:00
|
|
|
|
2012-07-17 03:50:54 +04:00
|
|
|
user = User.create!(external_id: "1")
|
2012-07-16 07:40:29 +04:00
|
|
|
|
2012-07-18 22:00:50 +04:00
|
|
|
comment_thread = CommentThread.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1", commentable_id: "question_1")
|
2012-07-16 07:40:29 +04:00
|
|
|
comment_thread.author = user
|
|
|
|
comment_thread.save!
|
|
|
|
|
2012-07-17 03:50:54 +04:00
|
|
|
comment = comment_thread.comments.new(body: "this problem is so easy", course_id: "1")
|
2012-07-16 07:40:29 +04:00
|
|
|
comment.author = user
|
|
|
|
comment.save!
|
2012-07-17 03:50:54 +04:00
|
|
|
comment1 = comment.children.new(body: "not for me!", course_id: "1")
|
2012-07-16 07:40:29 +04:00
|
|
|
comment1.author = user
|
2012-07-25 22:07:37 +04:00
|
|
|
comment1.comment_thread = comment_thread
|
2012-07-16 07:40:29 +04:00
|
|
|
comment1.save!
|
2012-07-17 03:50:54 +04:00
|
|
|
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
|
2012-07-16 07:40:29 +04:00
|
|
|
comment2.author = user
|
2012-07-25 22:07:37 +04:00
|
|
|
comment2.comment_thread = comment_thread
|
2012-07-16 07:40:29 +04:00
|
|
|
comment2.save!
|
|
|
|
|
2012-07-25 22:07:37 +04:00
|
|
|
children = comment_thread.root_comments.first.to_hash(recursive: true)["children"]
|
2012-07-16 07:40:29 +04:00
|
|
|
if children.length == 2
|
|
|
|
pp comment_thread.to_hash(recursive: true)
|
2012-07-25 22:07:37 +04:00
|
|
|
pp comment_thread.root_comments.first.descendants_and_self.to_a
|
2012-07-16 07:40:29 +04:00
|
|
|
puts "error!"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
puts "passed once"
|
|
|
|
end
|
|
|
|
puts "passed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 02:22:10 +04:00
|
|
|
task :console => :environment do
|
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
namespace :db do
|
2012-07-15 22:14:46 +04:00
|
|
|
task :init => :environment do
|
|
|
|
puts "creating indexes..."
|
|
|
|
Comment.create_indexes
|
|
|
|
CommentThread.create_indexes
|
|
|
|
User.create_indexes
|
2012-07-17 03:35:34 +04:00
|
|
|
Notification.create_indexes
|
2012-07-17 07:05:16 +04:00
|
|
|
Subscription.create_indexes
|
2012-07-15 22:14:46 +04:00
|
|
|
Delayed::Backend::Mongoid::Job.create_indexes
|
|
|
|
puts "finished"
|
|
|
|
end
|
2012-07-14 10:31:05 +04:00
|
|
|
|
2012-07-18 22:00:50 +04:00
|
|
|
task :clean => :environment do
|
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
|
|
|
User.delete_all
|
|
|
|
Notification.delete_all
|
|
|
|
Subscription.delete_all
|
|
|
|
end
|
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
def generate_comments_for(commentable_id)
|
2012-06-27 02:59:08 +04:00
|
|
|
level_limit = YAML.load_file("config/application.yml")["level_limit"]
|
2012-06-28 00:54:31 +04:00
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
thread_seeds = [
|
2012-07-19 02:22:10 +04:00
|
|
|
{title: "This is really interesting", body: "best I've ever seen!"},
|
|
|
|
{title: "We can probably make this better", body: "Let's do it"},
|
|
|
|
{title: "I don't know where to start", body: "Can anyone help me?"},
|
|
|
|
{title: "I'm here!", body: "Haha I'm the first one who discovered this"},
|
2012-07-24 00:15:44 +04:00
|
|
|
{title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"},
|
2012-07-19 02:22:10 +04:00
|
|
|
]
|
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
comment_body_seeds = [
|
2012-07-19 02:22:10 +04:00
|
|
|
"dude I don't know what you're talking about",
|
|
|
|
"hi I'm Jack",
|
|
|
|
"hi just sent you a message",
|
|
|
|
"let's discuss this further",
|
|
|
|
"can't agree more",
|
|
|
|
"haha",
|
|
|
|
"lol",
|
|
|
|
]
|
2012-07-24 05:03:24 +04:00
|
|
|
|
|
|
|
users = User.all.to_a
|
|
|
|
|
|
|
|
puts "Generating threads and comments for #{commentable_id}..."
|
|
|
|
|
|
|
|
threads = []
|
|
|
|
comments = []
|
|
|
|
|
|
|
|
thread_seeds.each do |thread_seed|
|
|
|
|
comment_thread = CommentThread.new(commentable_id: commentable_id, body: thread_seed[:body], title: thread_seed[:title], course_id: "1")
|
|
|
|
comment_thread.author = users.sample
|
|
|
|
comment_thread.save!
|
|
|
|
threads << comment_thread
|
|
|
|
3.times do
|
|
|
|
comment = comment_thread.comments.new(body: comment_body_seeds.sample, course_id: "1")
|
|
|
|
comment.author = users.sample
|
|
|
|
comment.endorsed = [true, false].sample
|
2012-07-25 22:07:37 +04:00
|
|
|
comment.comment_thread = comment_thread
|
2012-07-24 05:03:24 +04:00
|
|
|
comment.save!
|
|
|
|
comments << comment
|
|
|
|
end
|
|
|
|
10.times do
|
|
|
|
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
|
|
|
|
sub_comment = comment.children.new(body: comment_body_seeds.sample, course_id: "1")
|
|
|
|
sub_comment.author = users.sample
|
|
|
|
sub_comment.endorsed = [true, false].sample
|
2012-07-25 22:07:37 +04:00
|
|
|
sub_comment.comment_thread = comment_thread
|
2012-07-24 05:03:24 +04:00
|
|
|
sub_comment.save!
|
|
|
|
comments << sub_comment
|
2012-06-24 04:13:00 +04:00
|
|
|
end
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
2012-06-28 00:54:31 +04:00
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
puts "voting for these threads & comments.."
|
2012-07-14 10:31:05 +04:00
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
threads.each do |c|
|
|
|
|
users.each do |user|
|
|
|
|
user.vote(c, [:up, :down].sample)
|
2012-07-15 02:09:10 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
comments.each do |c|
|
|
|
|
users.each do |user|
|
|
|
|
user.vote(c, [:up, :down].sample)
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
end
|
2012-07-14 10:31:05 +04:00
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
puts "finished"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
task :generate_comments, [:commentable_id] => :environment do |t, args|
|
|
|
|
|
|
|
|
generate_comments_for(args[:commentable_id])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
task :seed => :environment do
|
|
|
|
|
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
|
|
|
User.delete_all
|
|
|
|
Notification.delete_all
|
|
|
|
Subscription.delete_all
|
|
|
|
|
|
|
|
beginning_time = Time.now
|
|
|
|
|
|
|
|
users = (1..10).map {|id| User.find_or_create_by(external_id: id.to_s)}
|
|
|
|
|
2012-07-25 22:58:33 +04:00
|
|
|
3.times do
|
|
|
|
users.first.subscribe(users.sample)
|
|
|
|
end
|
|
|
|
|
2012-07-24 05:03:24 +04:00
|
|
|
10.times do
|
|
|
|
users.sample.subscribe(users.sample)
|
|
|
|
end
|
|
|
|
|
|
|
|
generate_comments_for("video_1")
|
|
|
|
generate_comments_for("lab_1")
|
|
|
|
generate_comments_for("lab_2")
|
|
|
|
|
2012-07-14 10:31:05 +04:00
|
|
|
end_time = Time.now
|
|
|
|
|
|
|
|
puts "Number of comments generated: #{Comment.count}"
|
|
|
|
puts "Number of comment threads generated: #{CommentThread.count}"
|
|
|
|
|
|
|
|
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
end
|
2012-07-24 00:15:44 +04:00
|
|
|
|
|
|
|
# copied from https://github.com/sunspot/sunspot/blob/master/sunspot_solr/lib/sunspot/solr/tasks.rb
|
|
|
|
namespace :sunspot do
|
|
|
|
namespace :solr do
|
|
|
|
desc 'Start the Solr instance'
|
|
|
|
task :start => :environment do
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /w(in)?32$/, /java$/
|
|
|
|
abort("This command is not supported on #{RUBY_PLATFORM}. " +
|
|
|
|
"Use rake sunspot:solr:run to run Solr in the foreground.")
|
|
|
|
end
|
|
|
|
|
|
|
|
Sunspot::Solr::Server.new.start
|
|
|
|
|
|
|
|
puts "Successfully started Solr ..."
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Run the Solr instance in the foreground'
|
|
|
|
task :run => :environment do
|
|
|
|
Sunspot::Solr::Server.new.run
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Stop the Solr instance'
|
|
|
|
task :stop => :environment do
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /w(in)?32$/, /java$/
|
|
|
|
abort("This command is not supported on #{RUBY_PLATFORM}. " +
|
|
|
|
"Use rake sunspot:solr:run to run Solr in the foreground.")
|
|
|
|
end
|
|
|
|
|
|
|
|
Sunspot::Solr::Server.new.stop
|
|
|
|
|
|
|
|
puts "Successfully stopped Solr ..."
|
|
|
|
end
|
|
|
|
|
2012-07-25 22:58:33 +04:00
|
|
|
desc 'Restart the Solr instance'
|
|
|
|
task :restart => :environment do
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /w(in)?32$/, /java$/
|
|
|
|
abort("This command is not supported on #{RUBY_PLATFORM}. " +
|
|
|
|
"Use rake sunspot:solr:run to run Solr in the foreground.")
|
|
|
|
end
|
|
|
|
|
|
|
|
Sunspot::Solr::Server.new.stop
|
|
|
|
Sunspot::Solr::Server.new.start
|
|
|
|
|
|
|
|
puts "Successfully restarted Solr ..."
|
|
|
|
end
|
|
|
|
|
2012-07-24 00:15:44 +04:00
|
|
|
end
|
|
|
|
|
2012-07-27 02:12:49 +04:00
|
|
|
task :commit => :environment do
|
|
|
|
Sunspot.commit
|
|
|
|
end
|
|
|
|
|
2012-07-24 00:15:44 +04:00
|
|
|
end
|
2012-07-25 22:58:33 +04:00
|
|
|
|
|
|
|
namespace :jobs do
|
|
|
|
desc "Clear the delayed_job queue."
|
|
|
|
task :clear => :environment do
|
|
|
|
Delayed::Job.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Start a delayed_job worker."
|
|
|
|
task :work => :environment do
|
|
|
|
Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'], :queues => (ENV['QUEUES'] || ENV['QUEUE'] || '').split(','), :quiet => false).start
|
|
|
|
end
|
|
|
|
end
|