Permalink
Please
sign in to comment.
Browse files
Setup Delayed::Job and create job. Resolves #23.
- Sets up Delayed::Job - Create a job to populated wasapi_files table from Archive-It WASAPI endpoint - Modify schema to handle large integers for `size` field - tweak RuboCop config to deal with new code. We should come back around and clean this up
- Loading branch information...
Showing
with
160 additions
and 11 deletions.
- +12 −0 .rubocop.yml
- +2 −0 Gemfile
- +21 −0 Gemfile.lock
- +1 −2 app/controllers/users_controller.rb
- +5 −0 app/controllers/wasapi_files_controller.rb
- +57 −0 app/jobs/wasapi_files_populate_job.rb
- +1 −0 app/models/wasapi_file.rb
- +5 −0 bin/delayed_job
- +1 −0 config/application.rb
- +2 −2 db/migrate/20171128135615_create_wasapi_files.rb
- +22 −0 db/migrate/20180108203303_create_delayed_jobs.rb
- +18 −3 db/schema.rb
- +4 −4 test/fixtures/wasapi_files.yml
- +9 −0 test/jobs/wasapi_files_populate_job_test.rb
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
# WASAPI Files controller methods. | ||
class WasapiFilesController < ApplicationController | ||
end |
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
# Methods for populating Wasapi Files. | ||
class WasapiFilesPopulateJob < ApplicationJob | ||
queue_as :default | ||
|
||
# Constants | ||
WASAPI_BASE_URL = 'https://partner.archive-it.org/wasapi/v1/webdata' | ||
|
||
def perform(user) | ||
wasapi_request = HTTP.basic_auth(user: user.wasapi_username, | ||
pass: user.wasapi_password) | ||
.get(WASAPI_BASE_URL) | ||
wasapi_results = JSON.parse(wasapi_request) | ||
wasapi_files = wasapi_results['files'] | ||
wasapi_files.each do |file| | ||
WasapiFile.create!(filetype: file['filetype'], | ||
size: file['size'], | ||
filename: file['filename'], | ||
crawl_time: file['crawl-time'], | ||
crawl_start: file['crawl-start'], | ||
crawl: file['crawl'], | ||
account: file['account'], | ||
collection_id: file['collection'], | ||
location_archive_it: file['locations'][0], | ||
location_internet_archive: file['locations'][1], | ||
checksum_md5: file['checksums']['md5'], | ||
checksum_sha1: file['checksums']['sha1']) | ||
end | ||
paginate = wasapi_results['next'] | ||
if paginate.present? | ||
loop do | ||
wasapi_paged_request = HTTP.basic_auth(user: user.wasapi_username, | ||
pass: user.wasapi_password) | ||
.get(paginate) | ||
wasapi_paged_results = JSON.parse(wasapi_paged_request) | ||
wasapi_paged_files = wasapi_paged_results['files'] | ||
paginate = wasapi_paged_results['next'] | ||
wasapi_paged_files.each do |file| | ||
WasapiFile.create!(filetype: file['filetype'], | ||
size: file['size'], | ||
filename: file['filename'], | ||
crawl_time: file['crawl-time'], | ||
crawl_start: file['crawl-start'], | ||
crawl: file['crawl'], | ||
account: file['account'], | ||
collection_id: file['collection'], | ||
location_archive_it: file['locations'][0], | ||
location_internet_archive: file['locations'][1], | ||
checksum_md5: file['checksums']['md5'], | ||
checksum_sha1: file['checksums']['sha1']) | ||
end | ||
break if paginate.blank? | ||
end | ||
end | ||
end | ||
end |
@@ -1,4 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
# WASAPI File methods. | ||
class WasapiFile < ApplicationRecord | ||
end |
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) | ||
require 'delayed/command' | ||
Delayed::Command.new(ARGV).daemonize |
@@ -0,0 +1,22 @@ | ||
class CreateDelayedJobs < ActiveRecord::Migration[5.1] | ||
def self.up | ||
create_table :delayed_jobs, force: true do |table| | ||
table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue | ||
table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually. | ||
table.text :handler, null: false # YAML-encoded string of the object that will do work | ||
table.text :last_error # reason for last failure (See Note below) | ||
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. | ||
table.datetime :locked_at # Set when a client is working on this object | ||
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) | ||
table.string :locked_by # Who is working on this object (if locked) | ||
table.string :queue # The name of the queue this job is in | ||
table.timestamps null: true | ||
end | ||
|
||
add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority" | ||
end | ||
|
||
def self.down | ||
drop_table :delayed_jobs | ||
end | ||
end |
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class WasapiFilesPopulateJobTest < ActiveJob::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
0 comments on commit
f5b39f9