-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from NYULibraries/cron-job-to-clear-search
Cron job to clear search
- Loading branch information
Showing
8 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Use this file to easily define all of your cron jobs. | ||
# | ||
# It's helpful, but not entirely necessary to understand cron before proceeding. | ||
# http://en.wikipedia.org/wiki/Cron | ||
|
||
# Example: | ||
# | ||
# set :output, "/path/to/my/cron_log.log" | ||
# | ||
# every 2.hours do | ||
# command "/usr/bin/some_great_command" | ||
# runner "MyModel.some_method" | ||
# rake "some:great:rake:task" | ||
# end | ||
# | ||
# every 4.days do | ||
# runner "AnotherModel.prune_old_records" | ||
# end | ||
|
||
# Learn more: http://github.com/javan/whenever | ||
|
||
every 1.week do | ||
rake 'vacate_searches:vacate' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace :vacate_searches do | ||
desc 'Vacate week-old searches' | ||
task vacate: :environment do | ||
Search.where('created_at < ?', (Date.today - 7.days)).destroy_all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :search do | ||
query_params { { q: 'test' } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rake' | ||
|
||
RSpec.configure do |config| | ||
config.before(:suite) do | ||
Rails.application.load_tasks | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'timecop' | ||
|
||
RSpec.describe 'Vacate Searches', type: :task do | ||
context 'vacate_searches:vacate' do | ||
before do | ||
# Freeze time as task is time-sensitive | ||
Timecop.freeze Time.now | ||
end | ||
|
||
after { Timecop.return } | ||
|
||
it 'deletes searches older than 1 week' do | ||
create(:search, created_at: 2.week.ago) | ||
expect { invoke_task }.to change { Search.count }.by(-1) | ||
end | ||
|
||
it 'does not delete newer searches' do | ||
create(:search, created_at: 2.days.ago) | ||
expect { invoke_task }.not_to(change { Search.count }) | ||
end | ||
|
||
private | ||
|
||
def invoke_task | ||
task = Rake::Task['vacate_searches:vacate'] | ||
# Ensure task is re-enabled, as rake tasks by default are disabled | ||
# after running once within a process http://pivotallabs.com/how-i-test-rake-tasks/ | ||
task.reenable | ||
task.invoke | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# spec/whenever_spec.rb | ||
require 'spec_helper' | ||
|
||
describe 'Whenever Schedule' do | ||
before do | ||
load 'Rakefile' # Makes sure rake tasks are loaded so you can assert in rake jobs | ||
end | ||
|
||
it 'should have a rake task that runs every week' do | ||
schedule = Whenever::Test::Schedule.new(file: 'config/schedule.rb') | ||
task_name = schedule.jobs[:rake].first[:task] | ||
task_name.should include('vacate_searches:vacate') | ||
end | ||
end |