Class: Pige::Client::ChunkScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/pige/client/chunk_scheduler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(day = Date.today) ⇒ ChunkScheduler

Returns a new instance of ChunkScheduler.



52
53
54
55
# File 'lib/pige/client/chunk_scheduler.rb', line 52

def initialize(day = Date.today)
  self.day = day
  self.default_attributes = { :format => "wav" }
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



4
5
6
# File 'lib/pige/client/chunk_scheduler.rb', line 4

def day
  @day
end

#default_attributesObject

Returns the value of attribute default_attributes.



4
5
6
# File 'lib/pige/client/chunk_scheduler.rb', line 4

def default_attributes
  @default_attributes
end

#dry_runObject Also known as: dry_run?

Returns the value of attribute dry_run.



4
5
6
# File 'lib/pige/client/chunk_scheduler.rb', line 4

def dry_run
  @dry_run
end

Class Method Details

.define(&block) ⇒ Object



57
58
59
60
61
# File 'lib/pige/client/chunk_scheduler.rb', line 57

def self.define(&block)
  new.tap do |scheduler|
    yield scheduler
  end
end

Instance Method Details

#chunk_attributes(attributes = {}) ⇒ Object



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
# File 'lib/pige/client/chunk_scheduler.rb', line 70

def chunk_attributes(attributes = {})
  attributes = default_attributes.merge(attributes)

  [:begin, :end].each do |endpoint|
    attributes[endpoint] = Time.parse(attributes[endpoint], day) if String === attributes[endpoint]
  end

  if duration = attributes.delete(:duration)
    if attributes[:begin]
      attributes[:end] = attributes[:begin] + duration
    else
      attributes[:begin] = attributes[:end] - duration
    end
  end

  if name = attributes[:begin_label]
    label = Pige::Client::Label.by_name(name, after: attributes[:begin])
    if label
      puts "Found label '#{name}' at #{label.timestamp}"
      attributes[:begin] = label.timestamp
    else
      puts "Can't find' label '#{name}'"
    end
  end

  if name = attributes[:end_label]
    label = Pige::Client::Label.by_name(name, before: attributes[:end])
    if label
      puts "Found label '#{name}' at #{label.timestamp}"
      attributes[:end] = label.timestamp
    else
      puts "Can't find' label '#{name}'"
    end
  end

  if margin = attributes.delete(:margin)
    attributes[:begin] -= margin
    attributes[:end] += margin
  end

  if attributes[:title] =~ /%[dmyHMY]/
    attributes[:title] = day.strftime(attributes[:title])
  end

  attributes
end

#chunksObject



7
8
9
# File 'lib/pige/client/chunk_scheduler.rb', line 7

def chunks
  @chunks ||= []
end

#create(attributes = {}, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pige/client/chunk_scheduler.rb', line 117

def create(attributes = {}, options = {})
  options = { :delay => 1 }.merge(options)

  attributes = chunk_attributes(attributes)
  puts "Create Chunk with #{attributes.inspect}"

  unless dry_run?
    chunk = Pige::Client::Chunk.create(attributes)
    if chunk
      chunks << chunk
    else
      puts "Error: can't create Chunk with #{attributes.inspect}"
    end
    sleep options[:delay].to_i if options[:delay]
  end
end

#download(target) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pige/client/chunk_scheduler.rb', line 37

def download(target)
  if chunks.empty?
    puts "No chunk to download"
    return []
  end

  wait

  FileUtils.mkdir_p target
  chunks.map do |chunk|
    puts "Download '#{chunk.title}' to #{target}/#{chunk.download_file} ..."
    chunk.download target
  end
end

#on(criteria, &block) ⇒ Object



63
64
65
66
67
68
# File 'lib/pige/client/chunk_scheduler.rb', line 63

def on(criteria, &block)
  if day.send("#{criteria}?")
    puts "Scheduling #{criteria}"
    instance_exec &block
  end
end

#wait(timeout = nil) ⇒ Object



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
# File 'lib/pige/client/chunk_scheduler.rb', line 11

def wait(timeout = nil)
  if chunks.empty?
    puts "No chunk to wait"
    return
  end

  timeout ||= chunks.size * 5
  print "Wait for new chunks to be ready : "
  deadline = Time.now + (timeout * 60)

  uncompleted_chunks = chunks.dup

  while Time.now < deadline
    uncompleted_chunks.delete_if do |chunk|
      chunk.completion_rate == 1
    end
    if uncompleted_chunks.empty?
      puts " done\nChunks are ready"
      break
    else
      print "."
      sleep 10
    end
  end
end