Class: Driftwood::Plugin::Logger

Inherits:
Driftwood::Plugin show all
Defined in:
lib/driftwood/plugin/logger.rb

Instance Attribute Summary

Attributes inherited from Driftwood::Plugin

#about, #name, #usage

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, slack = nil, bigquery = nil) ⇒ Logger

Returns a new instance of Logger.



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
# File 'lib/driftwood/plugin/logger.rb', line 3

def initialize(config=nil, slack=nil, bigquery=nil)
  super(config, slack, bigquery)
  @about   = 'Logs all public messages to BigQuery.'

  # logging handler
  @slack.register_handler('message') do |team_id, event_data|
    next unless event_data['channel_type'] == 'channel'
    $logger.debug "Logged message for #{team_id}: #{event_data['text']}"
    @bigquery.insert_message(@slack.normalize_message(team_id, event_data))
  end

  # register new users
  @slack.register_handler('team_join') do |team_id, event_data|
    user = @slack.normalize_user(event_data['user'])
    $logger.info "Registered new user: #{user['name']} (#{user['real_name']})"
    @bigquery.insert_user(user)
  end

  # Do this in a thread so it doesn't block initialization
  # These methods may take a long time to complete
  Thread.new do
    begin
      $logger.info "Starting data synchronization..."
      sync_messages
      sync_channels
      sync_users
    rescue => e
      $logger.error e.message
      $logger.debug e.backtrace.join("\n")
    end

    loop do
      sleep 86400 # Once a day; 60 * 60 * 24
      begin
        $logger.info "Purging deleted users..."
        @bigquery.purge_deleted_users
      rescue => e
        $logger.error e.message
        $logger.debug e.backtrace.join("\n")
      end
    end
  end
end

Instance Method Details

#sync_channelsObject



47
48
49
50
51
52
# File 'lib/driftwood/plugin/logger.rb', line 47

def sync_channels
  @slack.all_channels.each do |channel|
    @bigquery.insert_channel(channel)
  end
  $logger.info "All channels synced."
end

#sync_messagesObject



62
63
64
65
66
67
68
# File 'lib/driftwood/plugin/logger.rb', line 62

def sync_messages
  starting_from = @bigquery.newest_message_timestamp
  @slack.all_messages(starting_from).each do |message|
    @bigquery.insert_message(message)
  end
  $logger.info "All messages synced."
end

#sync_usersObject



54
55
56
57
58
59
60
# File 'lib/driftwood/plugin/logger.rb', line 54

def sync_users
  @slack.all_users.each do |user|
    @bigquery.insert_user(user)
  end
  @bigquery.reconcile_user_creations
  $logger.info "All users synced."
end