-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
151 lines (133 loc) · 4.78 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Rakefile
require 'dotenv/tasks'
require 'sinatra/activerecord/rake'
namespace :db do
task :load_config do
require './switchyard.rb'
end
# Deletes all rows in collections and media_objects
# @example Clear collections and media_objects table
# RACK_ENV=env bundle exec rake db:clear_history
task :clear_history do
require './switchyard.rb'
MediaObject.delete_all
Collection.delete_all
end
end
namespace :setup do
# Copies all files in /config that end with .example.yml to /config as .yml
# Does not replace file if it already exists
task :configs do
directory = 'config/'
extension_to_remove = '.example.yml'
extension_to_add = '.yml'
Dir["#{directory}*#{extension_to_remove}"].each do |filename|
new_filename = filename[0..filename.length - extension_to_remove.length - 1] + extension_to_add
FileUtils.cp(filename, new_filename) unless File.exist?(new_filename)
end
end
end
# Rake tasks for generating and decomissioning tokens
namespace :tokens do
require './switchyard.rb'
# This task generates and displays a new token and prints the result to console
# @param [String] :notes (default: 'none') Any notes to add to the token
# @param [Boolean] :active (default: true) If set to true the API key is usable, false it is not
# @raise [ArgumentError] For a variety of cases when :notes or :active is invalid, exception will contain details
task :create_token, [:notes,:active] do |t,args|
token = ApiToken.new.create_token(active: args[:active] || true, notes: args[:notes] || 'none')
puts token.inspect
end
task :decomission_token, [:token_key] do |t,args|
token = ApiToken.new.decomission_token(args[:token_key])
puts token.inspect
end
end
namespace :switchyard do
task :send_item => :dotenv do
unless Router.new.send_in_progress?
# Find the oldest object
obj = Objects.new.oldest_ready_object
unless obj.nil?
# Lock it so future chron tasks don't run
media_object = MediaObject.find(obj[0])
media_object.locked = true
media_object.last_modified = Time.now.utc.iso8601.to_s
media_object.save!
# Send It
new_object = media_object.avalon_pid.nil?
begin
item = Objects.new(posted_content: media_object.api_hash)
object = item.parse_request_body
item.post_new_media_object(object) if new_object
item.update_media_object(object) unless new_object
rescue Exception => e
media_object.error = true
media_object.status = 'error'
media_object.message = e
media_object.save!
end
media_object.locked = false
media_object.save!
end
end
end
task :send_batch => :dotenv do
unless Router.new.send_in_progress?
# Find the oldest object
objs = MediaObject.where(status: 'received')
unless objs.empty?
objs.each do |obj|
# Lock it so future chron tasks don't run
media_object = MediaObject.find(obj.id)
media_object.locked = true
media_object.last_modified = Time.now.utc.iso8601.to_s
media_object.save!
# Send It
new_object = media_object.avalon_pid.nil?
begin
item = Objects.new(posted_content: media_object.api_hash)
object = item.parse_request_body
item.post_new_media_object(object) if new_object
item.update_media_object(object) unless new_object
rescue Exception => e
unless MediaObject.find(obj.id).error
media_object.error = true
media_object.status = 'error'
media_object.message = e
media_object.save!
end
end
media_object.locked = false
media_object.save!
end
end
end
end
desc "Send a single media object by group name"
task :send_by_group_name, [:group_name] => [:dotenv] do |t, args|
unless Router.new.send_in_progress?
# Lock it so future chron tasks don't run
media_object = MediaObject.find_by_group_name(args[:group_name])
media_object.locked = true
media_object.last_modified = Time.now.utc.iso8601.to_s
media_object.save!
# Send It
new_object = media_object.avalon_pid.nil?
begin
item = Objects.new(posted_content: media_object.api_hash)
object = item.parse_request_body
item.post_new_media_object(object) if new_object
item.update_media_object(object) unless new_object
rescue Exception => e
puts "Error encountered: #{e.message}"
puts e.backtrace
media_object.error = true
media_object.status = 'error'
media_object.message = e
end
media_object.locked = false
media_object.save!
end
end
end