From 1b830ac11ff3e075fb4e199dde509bda8c0815bd Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 25 Jun 2019 11:22:27 +0800 Subject: [PATCH] =?UTF-8?q?rake=E4=BB=BB=E5=8A=A1=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework_commons_controller.rb | 10 +++---- app/tasks/exercise_publish_task.rb | 6 ++--- .../exercise_close_to_deadline_warn.rake | 26 +++++++++++-------- lib/tasks/graduation_task.rake | 2 +- lib/tasks/homework_endtime.rake | 10 +++---- lib/tasks/homework_evaluation.rake | 4 +-- lib/tasks/homework_publishtime.rake | 4 +-- lib/tasks/poll_publish.rake | 20 ++++++-------- lib/tasks/resource_publish.rake | 4 +-- 9 files changed, 42 insertions(+), 44 deletions(-) diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 6e2e1df40..cf2d6b20e 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -458,9 +458,9 @@ class HomeworkCommonsController < ApplicationController tip_exception("发布时间不能为空") if setting[:publish_time].blank? tip_exception("截止时间不能为空") if setting[:end_time].blank? # 如果该发布规则 没有已发布的分班则需判断发布时间 - tip_exception("发布时间不能早于当前时间") if setting[:publish_time] <= strf_time(Time.now) && group_settings.group_published.count == 0 + tip_exception("发布时间不能早于等于当前时间") if setting[:publish_time] <= strf_time(Time.now) && group_settings.group_published.count == 0 - tip_exception("截止时间不能早于当前时间") if setting[:end_time] <= strf_time(Time.now) + tip_exception("截止时间不能早于等于当前时间") if setting[:end_time] <= strf_time(Time.now) tip_exception("截止时间不能早于发布时间") if setting[:publish_time] > setting[:end_time] tip_exception("截止时间不能早于课堂结束时间") if setting[:end_time] > strf_time(@course.end_date.end_of_day) @@ -585,9 +585,9 @@ class HomeworkCommonsController < ApplicationController # anonymous_comment :true 是启用,false 是不启用 if params[:anonymous_comment] tip_exception("匿评开启时间不能为空") if params[:evaluation_start].blank? - tip_exception("匿评开启时间不能早于截止时间") if params[:evaluation_start] <= strf_time(@homework.end_time) + tip_exception("匿评开启时间不能早于截止时间") if params[:evaluation_start] < strf_time(@homework.end_time) tip_exception("匿评结束时间不能为空") if params[:evaluation_end].blank? - tip_exception("匿评截止时间不能早于匿评开启时间") if params[:evaluation_end] <= params[:evaluation_start] + tip_exception("匿评截止时间必须晚于匿评开启时间") if params[:evaluation_end] <= params[:evaluation_start] tip_exception("匿评截止时间不能晚于课堂结束时间") if @course.end_date.present? && params[:evaluation_end] > strf_time(@course.end_date.end_of_day) tip_exception("匿评数必须为正整数") if params[:evaluation_num].blank? || params[:evaluation_num].to_i < 1 @@ -614,7 +614,7 @@ class HomeworkCommonsController < ApplicationController # 匿评未截止时可以更新匿评结束时间 if @homework_detail_manual.comment_status < 4 tip_exception("匿评结束时间不能为空") if @homework.anonymous_comment && params[:evaluation_end].blank? - tip_exception("匿评截止时间不能早于匿评开启时间") if @homework.anonymous_comment && + tip_exception("匿评截止时间必须晚于匿评开启时间") if @homework.anonymous_comment && params[:evaluation_end] <= params[:evaluation_start] tip_exception("匿评截止时间不能晚于课堂结束时间") if @homework.anonymous_comment && @course.end_date.present? && params[:evaluation_end] > strf_time(@course.end_date.end_of_day) diff --git a/app/tasks/exercise_publish_task.rb b/app/tasks/exercise_publish_task.rb index 282968755..a561aa42e 100644 --- a/app/tasks/exercise_publish_task.rb +++ b/app/tasks/exercise_publish_task.rb @@ -15,9 +15,9 @@ class ExercisePublishTask tid_str += "(#{member.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if exercise.unified_setting - course.student.find_each do |student| + course.students.find_each do |student| tid_str += "," if tid_str != "" - tid_str += "(#{student.student_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + tid_str += "(#{student.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'ExercisePublish', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end end if tid_str != "" @@ -25,7 +25,7 @@ class ExercisePublishTask ActiveRecord::Base.connection.execute tid_sql end - if exercise.exercise_users.count == 0 + if exercise.exercise_users.size == 0 str = "" course.students.find_each do |student| str += "," if str != "" diff --git a/lib/tasks/exercise_close_to_deadline_warn.rake b/lib/tasks/exercise_close_to_deadline_warn.rake index 598755db0..0212e138c 100644 --- a/lib/tasks/exercise_close_to_deadline_warn.rake +++ b/lib/tasks/exercise_close_to_deadline_warn.rake @@ -5,37 +5,41 @@ namespace :exercise_deadline_warn do task :deadline_warn => :environment do puts "--------------------------------exercise_nearly_end start" # 统一设置发布时间的测验 - exercises = Exercise.where("exercise_status = 2 and unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 82800) + exercises = Exercise.where("exercise_status = 2 and unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) exercises.each do |exercise| - if exercise.tidings.where(:parent_container_type => "NearlyEnd").count == 0 + if exercise.tidings.where(parent_container_type: "NearlyEnd").count == 0 course = exercise.course tid_str = "" - exercise.exercise_users.where(:commit_status => 0).find_each do |student| + exercise.exercise_users.where(commit_status: 0).find_each do |student| tid_str += "," if tid_str != "" - tid_str += "(#{student.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'NearlyEnd', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + tid_str += "(#{student.user_id}, #{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'NearlyEnd', + #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if tid_str != "" - tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str + tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, + parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str ActiveRecord::Base.connection.execute tid_sql end end end # 分组设置发布时间的测验 - exercise_group_settings = ExerciseGroupSetting.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 82800) + exercise_group_settings = ExerciseGroupSetting.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) exercise_group_settings.each do |exercise_group| exercise = exercise_group.exercise if exercise.present? course = exercise.course - members = course.course_members.where(:course_group_id => exercise_group.course_group_id) - if exercise.tidings.where(:parent_container_type => "NearlyEnd", :user_id => members.map(&:user_id)).count == 0 + members = course.students.where(course_group_id: exercise_group.course_group_id) + if exercise.tidings.where(parent_container_type: "NearlyEnd", user_id: members.pluck(:user_id)).count == 0 tid_str = "" - exercise.exercise_users.where(:commit_status => 0, :user_id => members.map(&:user_id)).find_each do |member| + exercise.exercise_users.where(commit_status: 0, user_id: members.pluck(:user_id)).find_each do |member| tid_str += "," if tid_str != "" - tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'NearlyEnd', #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" + tid_str += "(#{member.user_id},#{exercise.user_id}, #{exercise.id}, 'Exercise', #{exercise.id}, 'NearlyEnd', + #{course.id}, 'Course', 0, 'Exercise', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')" end if tid_str != "" - tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str + tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, + parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str ActiveRecord::Base.connection.execute tid_sql end end diff --git a/lib/tasks/graduation_task.rake b/lib/tasks/graduation_task.rake index dada0303d..aad84491f 100644 --- a/lib/tasks/graduation_task.rake +++ b/lib/tasks/graduation_task.rake @@ -30,7 +30,7 @@ namespace :graduation_task do end task :nearly_end => :environment do - tasks = GraduationTask.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 82800) + tasks = GraduationTask.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) tasks.each do |task| if task.tidings.where(parent_container_type: "NearlyEnd").count == 0 course = task.course diff --git a/lib/tasks/homework_endtime.rake b/lib/tasks/homework_endtime.rake index feba02670..555903980 100644 --- a/lib/tasks/homework_endtime.rake +++ b/lib/tasks/homework_endtime.rake @@ -4,7 +4,7 @@ namespace :homework_endtime do desc "send a message for Job deadline" task :message => :environment do # 统一设置发布时间的作业 - homeworks = HomeworkCommon.where("unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 82800) + homeworks = HomeworkCommon.includes(:course).where("unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) homeworks.each do |homework| if homework.tidings.where(:parent_container_type => "NearlyEnd").count == 0 course = homework.course @@ -25,7 +25,7 @@ namespace :homework_endtime do end # 分组设置发布时间的作业 - homework_group_settings = HomeworkGroupSetting.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 82800) + homework_group_settings = HomeworkGroupSetting.includes(:homework_common).where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600) homework_group_settings.each do |homework_group| homework = homework_group.homework_common if homework.present? @@ -52,8 +52,8 @@ namespace :homework_endtime do # 匿评截止时间快到了 task :evaluation_nearly_end => :environment do - homework_detail_manuals = HomeworkDetailManual.where("homework_detail_manuals.comment_status = 3 and evaluation_end <=? - and evaluation_end > ? ", Time.now + 86400, Time.now + 82800) + homework_detail_manuals = HomeworkDetailManual.includes(:homework_common).where("homework_detail_manuals.comment_status = 3 and evaluation_end <=? + and evaluation_end > ? ", Time.now + 86400, Time.now + 84600) homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.present? @@ -85,7 +85,7 @@ namespace :homework_endtime do # 匿评申诉截止时间快到了 task :appeal_nearly_end => :environment do homework_detail_manuals = HomeworkDetailManual.where("homework_detail_manuals.comment_status = 4 and appeal_time <=? - and appeal_time > ?", Time.now + 86400, Time.now + 82800) + and appeal_time > ?", Time.now + 86400, Time.now + 84600) homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.present? diff --git a/lib/tasks/homework_evaluation.rake b/lib/tasks/homework_evaluation.rake index 84c7c888f..cb7b51fae 100644 --- a/lib/tasks/homework_evaluation.rake +++ b/lib/tasks/homework_evaluation.rake @@ -12,7 +12,7 @@ namespace :homework_evaluation do task :start_evaluation => :environment do Rails.logger.info("log--------------------------------start_evaluation start") puts "--------------------------------start_evaluation start" - homework_detail_manuals = HomeworkDetailManual.where("evaluation_start <= '#{Time.now}' and + homework_detail_manuals = HomeworkDetailManual.includes(homework_common: :course).where("evaluation_start <= '#{Time.now}' and (homework_detail_manuals.comment_status < 3)") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common @@ -110,7 +110,7 @@ namespace :homework_evaluation do #自动关闭匿评的任务 task :end_evaluation => :environment do - homework_detail_manuals = HomeworkDetailManual.where("evaluation_end <= '#{Time.now}' and homework_detail_manuals.comment_status = 3") + homework_detail_manuals = HomeworkDetailManual.includes(:homework_common).where("evaluation_end <= '#{Time.now}' and homework_detail_manuals.comment_status = 3") homework_detail_manuals.each do |homework_detail_manual| homework_common = homework_detail_manual.homework_common if homework_common.anonymous_comment #开启匿评状态才可关闭匿评 diff --git a/lib/tasks/homework_publishtime.rake b/lib/tasks/homework_publishtime.rake index f6a4325cd..0e45b0224 100644 --- a/lib/tasks/homework_publishtime.rake +++ b/lib/tasks/homework_publishtime.rake @@ -6,8 +6,8 @@ namespace :homework_publishtime do # 作业的分班设置时间 def homework_group_setting homework, group_id setting = nil - if homework.homework_group_settings.where(:course_group_id => group_id).first - setting = homework.homework_group_settings.where(:course_group_id => group_id).first + if homework.homework_group_settings.where(course_group_id: group_id).first + setting = homework.homework_group_settings.where(course_group_id: group_id).first else setting = homework end diff --git a/lib/tasks/poll_publish.rake b/lib/tasks/poll_publish.rake index 48c289050..6ced88908 100644 --- a/lib/tasks/poll_publish.rake +++ b/lib/tasks/poll_publish.rake @@ -116,11 +116,7 @@ namespace :poll_publish do polls = Poll.includes(:poll_users).where("polls_status = 2 AND unified_setting = true AND end_time <=?",Time.now + 900) polls.each do |poll| poll.update_column('polls_status', 3) - poll.poll_users.each do |poll_user| - if poll_user.commit_status == 0 && poll_user.start_at.present? - poll_user.update_attributes(:commit_status => 1, :end_at => Time.now) - end - end + poll.poll_users.where("commit_status = 0 and start_at is not null").update_all(commit_status: 1, end_at: Time.now) end #2.分班设置的截止 @@ -132,13 +128,13 @@ namespace :poll_publish do if poll&.end_time <= Time.now poll.update_column('polls_status', 3) end - users = poll.course.course_members.where(:course_group_id => poll_setting.course_group_id) - poll_users = poll.poll_users.where(:user_id => users.map(&:user_id)) - poll_users.each do |poll_user| - if poll_user.commit_status == 0 && !poll_user.start_at.nil? - poll_user.update_attributes(:commit_status => 1, :end_at => Time.now) - end - end + users = poll.course.course_members.where(course_group_id: poll_setting.course_group_id) + poll.poll_users.where(user_id: users.pluck(:user_id)).where("commit_status = 0 and start_at is not null").update_all(commit_status: 1, end_at: Time.now) + # poll_users.each do |poll_user| + # if poll_user.commit_status == 0 && !poll_user.start_at.nil? + # poll_user.update_attributes(:commit_status => 1, :end_at => Time.now) + # end + # end end # PollGroupSetting.where("end_time < ? and end_time > ?", Time.now + 1800, Time.now - 1800).each do |poll_setting| diff --git a/lib/tasks/resource_publish.rake b/lib/tasks/resource_publish.rake index 7872a6ce4..f90718cfb 100644 --- a/lib/tasks/resource_publish.rake +++ b/lib/tasks/resource_publish.rake @@ -5,9 +5,7 @@ namespace :resource_publish do task :publish => :environment do Rails.logger.info("log--------------------------------resource_publish start") attachments = Attachment.where("publish_time < '#{Time.now}' and is_publish = 0") - attachments.each do |attachment| - attachment.update_attributes(:is_publish => 1) - end + attachments.update_all(is_publish: 1) Rails.logger.info("log--------------------------------resource_publish end") end end \ No newline at end of file