Module: Cinch::Plugins::Octospy::Job

Included in:
Cinch::Plugins::Octospy
Defined in:
lib/cinch/plugins/octospy/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cinch/plugins/octospy/job.rb', line 7

def self.included(base)
  base.class_eval do
    match('start', method: :start_with_message)
    match('stop', method: :stop_with_message)
    match('restart', method: :restart_with_message)
  end

  Channel.class_eval do
    attr_accessor :job_thread, :last_github_event_id

    define_method(:job_thread_alive?) do
      @job_thread && @job_thread.alive?
    end
  end
end

Instance Method Details

#restart(m) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/cinch/plugins/octospy/job.rb', line 88

def restart(m)
  if m.channel.job_thread_alive?
    restart!(m)
    true
  else
    false
  end
end

#restart!(m) ⇒ Object



97
98
99
100
# File 'lib/cinch/plugins/octospy/job.rb', line 97

def restart!(m)
  stop(m) if m.channel.job_thread_alive?
  start(m)
end

#restart_with_message(m) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/cinch/plugins/octospy/job.rb', line 50

def restart_with_message(m)
  if restart(m)
    m.reply 'restarted'
  else
    m.reply 'not started'
  end
end

#start(m) ⇒ Object



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
# File 'lib/cinch/plugins/octospy/job.rb', line 58

def start(m)
  repos = ::Octospy::Recordable.channel(m.channel.name).repos
  channel = m.channel

  worker = ::Octospy.worker(repos) do |message|
    case message.class.name
    when 'String'
      message.each_char.each_slice(512) do |string|
        channel.notice string.join
        sleep 1
      end
    when 'Array'
      message.each do |line|
        # maximum line length 512
        # http://www.mirc.com/rfc2812.html
        line.each_char.each_slice(512) do |string|
          channel.notice string.join
          sleep 1
        end
      end
    end
  end

  m.channel.job_thread = worker.thread
end

#start_with_message(m) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cinch/plugins/octospy/job.rb', line 23

def start_with_message(m)
  channel = ::Octospy::Recordable.channel(m.channel.name)

  if channel.nil? || channel.repos.nil? || channel.repos.empty?
    m.reply 'no repository watched'
    return
  end

  if m.channel.job_thread_alive?
    m.reply 'already started'
    return
  end

  start(m)
  m.reply 'started'
end

#stop(m) ⇒ Object



84
85
86
# File 'lib/cinch/plugins/octospy/job.rb', line 84

def stop(m)
  m.channel.job_thread.kill
end

#stop_with_message(m) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/cinch/plugins/octospy/job.rb', line 40

def stop_with_message(m)
  unless m.channel.job_thread_alive?
    m.reply 'not started'
    return
  end

  stop(m)
  m.reply 'stopped'
end