CSC/ECE 517 Spring 2015 E1529 GLDS: Difference between revisions
No edit summary |
No edit summary |
||
Line 212: | Line 212: | ||
==Keep log of the scheduled tasks== | ==Keep log of the scheduled tasks== | ||
According to the project [https://github.com/expertiza/expertiza/pull/462 E1478], there is a paper_trail<ref> | According to the project [https://github.com/expertiza/expertiza/pull/462 E1478], there is a paper_trail<ref name = "paper trail"> https://github.com/expertiza/expertiza/pull/462]</ref> gem which can keep log of the models' data. What we need to do is let it also keep track of our scheduled task data.<br> | ||
Since the gem need to work with <code>ActiveRecord 3+</code>, while <code>assignment_form</code> doesn't. So we created a new file named <code>delayed_job.rb</code> in <code>models</code> folder, add <code>has_paper_trail</code> in it. Then it is like: <br> | Since the gem need to work with <code>ActiveRecord 3+</code>, while <code>assignment_form</code> doesn't. So we created a new file named <code>delayed_job.rb</code> in <code>models</code> folder, add <code>has_paper_trail</code> in it. Then it is like: <br> | ||
<pre> | <pre> |
Revision as of 04:43, 22 April 2015
E1529. Extend the Email notification feature to scheduled tasks
Overview
Introduction to Expertiza
Expertiza is a web application developed by Ruby on Rails framework. It serves as a peer review system for professors and students at NC State and some other colleges and universities<ref>Expertiza Github</ref>. Students can submit different assignments and peer-review reusable learning objects (articles, code, web sites, etc). It is also a powerful tool for professor to manage courses and assignments and so on. The latest "Rails 4" branch of Expertiza, although combined with various enhancements from the past two years, is seriously broken, from data migration to key feature implementation. Part of the reason has been the design strategy and code changes by various teams.
Email notification feature to scheduled tasks
This is a feature that has already been partially implemented in Expertiza E1451 implemented both sychronous and asychoronous mailers. Sychronous Emails refer to the Emails sent immediatly after an event (e.g. when student receive a peer-review). Asychoronous Email we implemented by the Gem "delayed job” and when a task (asychronous Email) is added to the delayed queue, a count-down number of minutes needs to be specified.
Our team aims to extend this project. This wiki page documents the problem analysis and provides a guideline for future development and enhancement.
Scope
Project Scope
E1451. Create Mailers for All Email Messages github wiki report (Merged in E1483)
Extending this project, the new system should be capable of
- If one task is added to the delayed job queue, the asychronous Email should be updated or deleted automatically.
- Add UI to visualize for the task in delayed job queue, instructors should be able to view tasks related to the assignments they have created.
- Keep log of the scheduled tasks when they are scheduled and done, record events in the same log as project E1478.
- [optional] support more scheduled task:
- instructors are able to schedule the time to drop all the outstanding reviews (reviews which has not been started)
- instructors are able to schedule a specific time to send Emails to all the assignment participants who are still not in any team to find or form one.
- (new) instructor should be able to schedule a time to drop all the topics which are held by 1 person teams.
- create tests to make sure the test coverage increases.
Files Involved
Mailers:
- delayed_mailer.rb
Models:
- assignment_form.rb
- due_date.rb
- delayed_job.rb(new created)
- scheduled_task.rb(new created)
Views:
- _due_dates.html.erb
- _assignments_actions.html.erb
- scheduled_tasks.erb(new created)
Controllers:
- assignments_controller.rb
Gems Related
- gem 'delayed_job_active_record'
Delayed:Job encapsulates the common pattern of asynchronously executing longer tasks in the background<ref> Delayed_job Gem</ref>. It allows to support multiple backends for storing the job queue. By using 'delayed_job_active_record' gem, we can use delayed_job with Active Record. This Active Record backend requires a job table.
After running rails generate delayed_job:active_record and rake db:migrate, a "delayed_jobs" table is created in our database. This gem integrates well with many RDBMS backend such as MySQL. Using this gem, we can store all scheduled tasks queue into the "delayed_jobs" table.
- gem 'paper_trail'
PaperTrail allows to track changes of models' data, which is good for versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed<ref>paper trail</ref>. Using this gem, we can keep log of the scheduled tasks.
Problem Analysis
Time Issues
Initially, when we want to verify the email function, we found many time issues in this system. That blocks our work about email notification. Some related problems are explained and analyzed in this section.
Wrong displayed time
Firstly, when we set a due time to test the mail features, we found the display of time is incorrect.
In the “Assignment Edit”->“Due dates”, when we modify the “Date & time” of the deadline, the displayed time will be 4 hours ahead of the time we saved.
After several tries, we found that every time when we click "save", the saved time is 4 hours earlier then the last saved time. However, the time in the database is alway correct as the time we save. After we analyze, we found the problem may lay in the line of 68 of views/assignment/edit/_due_dates.html.erb
file.
due_at = new Date(due_at.substr(0, 16)).format('yyyy/mm/dd HH:MM');
When we declare a Data object, this Data() function will perform a time zone conversion automatically. Yet Rails’ default timezone stored in database is UTC. So the displayed time(local time, which is EDT) and time in database is different.
Different time zone
In the previous version, when current time is compared with the time stored in database, they may apply to different time zones so that the result of comparison may be wrong.
When we delay a deadline of an assignment, for example, we set the deadline of metareview from2015/03/28 16:00 to 2015/04/04 20:00 and set the reminder hr to 16 hours.
After click "save", the run time showed database is about 8:00 am. Since 20-16=4, the correct run time is 4:00 am.
The reason why there is a four-hour time difference is generated from the file application_form.rb
, method find_min_from_now(due_at)
, and line 142 time_in_min=((due_at - curr_time).to_i/60)
. due_at
is the system time for Rails (UTC), while curr_time
is the local time which is EDT. The two time zones have four-hour time difference.
New deadline replace the old one
This time, we add a new delay in Review section, we delay the deadline from 2015/03/22 21:00 to 2015/03/30 21:00, and set the remind hr to 8 hours.
However, the delayed_jobs database form is still the same, which means in one assignment there is only one reminder can work.
As a result, we conclude that only one due_time record associated with different period exists in the database:
- In Expertiza, one assignment has several period, such as submission, review, metareview. Administrates are able to set and postpone due dates for each period separately.
- In the previous version, when the instructor postpone the review deadline and then postpone the submit deadline, the submit deadline’s modification will replace the the review deadline’s modification in database.
- In this case, if both of them need Email reminder, there will be no Email about the former modified deadline.
- After our analysis, we found the due time object is identified with assignment_id, instead of deadline type id.
UI issues
In the previous version, there is no UI to display the reminders of assignments. There is no way for instructors to view or delete their delayed tasks related to an assignment. We are going to add a button under Assignment->edit, and add a view to display all delayed jobs derived from the delayed_jobs table in our database.
System Design
Fix Time Issues
Unified time zone
We convert current time to the same time zone as the deadline stored in database. Then the result of comparison will be right so that it can be used for further usage.
In app/models/assignment_form.rb
, we modified curr_time=DateTime.now.to_s(:db)
to curr_time=DateTime.now.in_time_zone(zone='UTC').to_s(:db)
in the find_min_from_now
method, to make sure when calculating the minutes from now, the time zone is right.
Also, to make sure the right display of time, when log in as a administrator, go to Profile and choose the Preferred Time Zone to be "(GMT-05:00) Eastern Time (US&Canada)".
Separate deadline of each topic period
In each Assignment, each deadline type (E.g review, submit, metareview) should has its own Email reminder time in the database. New postponed deadline of one type can only replace the same deadline type in the same assignment id category. This bug has been fixed by TA.
Add UI to visualize for scheduled tasks
All the scheduled tasks are in the delayed_jobs
table in database, so the system will read from that table to show all the tasks information. A new page is created to show all of those scheduled tasks and there is also a delete button for instructor to delete any task. Instructor can view this page via a view delayed jobs
button in assignment->edit
.
The code in app/models/assignment_form.rb
add the scheduled tasks into delayed_jobs
table.
if diff>0 dj=DelayedJob.enqueue(ScheduledTask.new(@assignment.id, deadline_type, due_date.due_at.to_s(:db)), 1, diff.minutes.from_now) change_item_type(dj.id) due_date.update_attribute(:delayed_job_id, dj.id) # If the deadline type is review, add a delayed job to drop outstanding review if deadline_type == "review" dj = DelayedJob.enqueue(ScheduledTask.new(@assignment.id, "drop_outstanding_reviews", due_date.due_at.to_s(:db)), 1, diff.minutes.from_now) change_item_type(dj.id) end # If the deadline type is team_formation, add a delayed job to drop one member team if deadline_type == "team_formation" dj = DelayedJob.enqueue(ScheduledTask.new(@assignment.id, "drop_one_member_topics", due_date.due_at.to_s(:db)), 1, diff.minutes.from_now) change_item_type(dj.id) end end
After adding to the delayed_jobs, the database will store each task's assignment's id, deadline type, due date, task run time etc.
We create a file scheduled_tasks.erb
in the path: /views/assignments
to show the form. In this file, we use a @scheduled_jobs = DelayedJob.all
to read all datas from the "delayed_jobs" table.
.
<% @assignment = Assignment.find(params[:id]) %> <h1>Scheduled tasks for <%= @assignment.name %> assignment</h1> <% if flash[:notice] %> <div class="flash_note"><%= flash_message :notice %></div> <% end %> <% @scheduled_jobs = DelayedJob.all %> <table class="general"> <th width="25%">Deadline type #</th> <th width="25%">Run time</th> <th width="25%">Due date</th> <th width="25 %">Action</th> <th width="25 %">Delete</th> <% i=1 %> <% for delayed_job in @scheduled_jobs %> <tr> <% if delayed_job.handler.include? "assignment_id: #{@assignment.id}"%> <% handler = delayed_job.handler.split()%> <td><%= handler[5] %></td> <td><%= delayed_job.run_at %></td> <td><%= handler[7][1..-1] + ":" + handler[8][0..-2]%></td> <% if handler[5] == "submission" %> <td><%= " "%></td> <%end%> <% if handler[5] == "review" %> <td><%= " "%></td> <%end%> <% if handler[5] == "metareview" %> <td><%= " "%></td> <%end%> <% if handler[5] == "team_formation" %> <td><%= "send email to ask students to form teams"%></td> <%end%> <% if handler[5] == "drop_outstanding_reviews" %> <td><%= "drop outstanding reviews and send emails to those students"%></td> <%end%> <% if handler[5] == "drop_one_member_topics" %> <td><%= "drop one member team topics and send emails to those students"%></td> <%end%> <td><%= link_to image_tag('delete_icon.png', :title => 'Delete'), {:controller => 'assignments', :action => 'delete_scheduled_task', :id => @assignment.id, :delayed_job_id => delayed_job.id}%></td> <% end %> </tr> <% i=i+1 %> <% end %> </table> <% session[:return_to] = request.url %> <br/><br/> <a href="javascript:history.back()">Back</a>
The following two pictures shows our UI works.
Keep log of the scheduled tasks
According to the project E1478, there is a paper_trail<ref name = "paper trail"> https://github.com/expertiza/expertiza/pull/462]</ref> gem which can keep log of the models' data. What we need to do is let it also keep track of our scheduled task data.
Since the gem need to work with ActiveRecord 3+
, while assignment_form
doesn't. So we created a new file named delayed_job.rb
in models
folder, add has_paper_trail
in it. Then it is like:
class DelayedJob < Delayed::Job has_paper_trail end
When the delayed_jobs
table changes, a Delayed::Backend::ActiveRecord::Job
record is automatically created in the log. In assignment_form.rb
, we define a method to change the item type displayed in the log.
def change_item_type(delayed_job_id) log = Version.where(item_type: "Delayed::Backend::ActiveRecord::Job", item_id: delayed_job_id).first log.update_attribute(:item_type, "ScheduledTask") #Change the item type in the log end
Then we call it in add_to_delayed_queue
method. Every time when the delayed_jobs
table has added or deleted or updated data, the log can keep track of them. The following picture shows what the log is after creating scheduled tasks.
support more schedule tasks
For each scheduled task, we have done the followings:
- Create a new deadline type called "drop_outstanding_reviews". When we set the review’s due date, add an item into “delayed job” queue at the same time. When the review is due, it will automatically call a method to find all the outstanding reviews (reviews which have not been started) in the database then delete them.
There are two tables in the database to store the information about the reviews: responses
and response_maps
.
responses
is used to store all the submitted reviews while response_maps
stores all the requested reviews, which means if one review has began it will not stored in responses
table. Thus, we just need to find the review data in response_maps
while not in responses
for one assignment and delete them in database.
We add a method in /models/scheduled_task.rb
named drop_outstanding_reviews
to drop those reviews.
def drop_outstanding_reviews reviews = ResponseMap.where(reviewed_object_id: self.assignment_id) for review in reviews review_has_began = Response.where(map_id: review.id) if review_has_began.size.zero? review_to_drop = ResponseMap.where(id: review.id) review_to_drop.first.destroy end end end
Then, add the following code in perform
method of the same file to drop one specified assignment's outstanding reviews at the due time.
if(self.deadline_type == "drop_one_member_topics") assignment = Assignment.find(self.assignment_id) if(assignment.team_assignment?) drop_one_member_topics end end
- Use a previously existed deadline type called "team_formation" and enable to set a team_formation's due date and a reminder. Then add an item into “delayed job” at the same time. When “delayed job” is triggered, it will automatically call a method to find all the assignment participants who are still not in any team in the database, and send Emails to them to find teammate.
- Create a new deadline type called "drop_one_member_topics". When time is due, the system will call a method to find all the topics which are held by 1 person teams in the database, then the professor can drop those topics.
Diagram description: In our system, we would have four scheduled time: submission, review, metareview and team_up. When due time comes, the system takes corresponding actions described as above. These scheduled actions are called scheduled tasks. And the design to achieve these functions are explained above. This diagram just displays the text description above in "Support more scheduled tasks " section.
1
2
3
Tests
References
<references/>