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.



42
43
44
45
# File 'lib/pige/client/chunk_scheduler.rb', line 42

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



47
48
49
50
51
# File 'lib/pige/client/chunk_scheduler.rb', line 47

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

Instance Method Details

#chunk_attributes(attributes = {}) ⇒ Object



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

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pige/client/chunk_scheduler.rb', line 107

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

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

  unless dry_run?
    chunks << Pige::Client::Chunk.create(attributes)
    sleep options[:delay].to_i if options[:delay]
  end
end

#download(target) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/pige/client/chunk_scheduler.rb', line 32

def download(target)
  wait

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

#on(criteria, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/pige/client/chunk_scheduler.rb', line 53

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

def wait(timeout = nil)
  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