-
Notifications
You must be signed in to change notification settings - Fork 2
/
rakefile.rb
129 lines (114 loc) · 3.65 KB
/
rakefile.rb
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
require 'figaro'
require 'sequel'
require 'securerandom'
require_relative 'config/environment'
DB = Sequel.connect(ENV['db'])
require "#{ENV['root']}/entities/api_key"
require "#{ENV['root']}/entities/convert_task"
namespace :task_cleaner do
desc 'Clear downloaded files. (Run every 1-10 minutes)'
task :downloaded do
clear_time = Time.now-ENV['downloaded_tasks_store_time_in_seconds'].to_i
cleanable_tasks =
ConvertTask.filter { (downloads_count > 0) & (last_download_time < clear_time) }
count_cleanable = cleanable_tasks.count
cleanable_tasks.each do |task|
FileUtils.rm_f "#{ENV['file_storage']}/#{task.source_file}"
FileUtils.rm_f "#{ENV['file_storage']}/#{task.converted_file}"
task.delete
end
puts "Success. Cleaned #{count_cleanable} tasks."
end
desc 'Clear outdated files. (Run every 3-10 days)'
task :outdated do
clear_time = Time.now-ENV['tasks_store_days'].to_i*24*3600
cleanable_tasks =
ConvertTask.filter { (downloads_count > 0) & (last_download_time < clear_time) }
count_cleanable = cleanable_tasks.count
cleanable_tasks =
ConvertTask.filter { created_at < clear_time }
cleanable_tasks.each do |task|
FileUtils.rm_f "#{ENV['file_storage']}/#{task.source_file}"
FileUtils.rm_f "#{ENV['file_storage']}/#{task.converted_file}"
task.delete
end
puts "Success. Cleaned #{count_cleanable} tasks."
end
end
namespace :db do
# examples
# 1) rake db:migrate - migrate to last version
# 2) rake db:migrate[42] - migrate to concrete version
desc 'Run migrations'
task :migrate, [:version] do |t, args|
Sequel.extension :migration
if args[:version]
puts "Migrating to version #{args[:version]}"
Sequel::Migrator.run(DB, 'migrations', target: args[:version].to_i)
else
puts "Migrating to latest"
Sequel::Migrator.run(DB, 'migrations')
end
end
end
namespace :service do
desc 'Check ConServA'
task :check do
# Environment validations
if RUBY_VERSION >= '2.3'
puts 'Ruby version ..... OK'
else
puts 'Ruby version smaller then 2.3, please upldate ruby ..... FAIL'
end
# DB validations
if db_connected?
puts 'Database connection valid ..... OK'
if tables_created?
puts 'Tables exist ........... OK'
else
puts 'Tables does non exist. Please run migrations ..... FAIL'
end
else
puts 'Connection to database invalid. Check connection settings .... FAIL'
end
# Running daemon
if daemon_work?
puts 'Daemon work ..... OK'
else
puts 'Daemon is not running. Please, run daemon. ..... FALSE'
end
end
private
def db_connected?
DB.test_connection
rescue Sequel::DatabaseConnectionError
false
end
def tables_created?
DB.table_exists?(:convert_tasks) && DB.table_exists?(:api_keys)
end
def daemon_work?
pid = File.read('task_manager.pid').strip
Process.kill(0, pid.to_i)
true
rescue Errno::ENOENT, Errno::ESRCH # No such process
false
end
end
# example: rake create_token['user_token','Token for user User']
desc 'Generate unique token'
task :create_token, [:name, :comment] do |t, args|
args.with_defaults(name: 'default_name', comment: '')
existed_uuid = ApiKey.all.map(&:uuid)
loop do
@uuid = SecureRandom.uuid
break unless existed_uuid.include? @uuid
end
ApiKey.create do |auth_token|
auth_token.uuid = @uuid
auth_token.name = args.name
auth_token.comment = args.comment
end
puts @uuid
end
# @todo добавить задачу на смену токена пользователю, имя пользователя сделать уникальным