File: uptime.rb

package info (click to toggle)
mikutter 3.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,256 kB
  • ctags: 2,165
  • sloc: ruby: 19,079; sh: 205; makefile: 20
file content (44 lines) | stat: -rw-r--r-- 1,103 bytes parent folder | download | duplicates (4)
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
#-*- coding: utf-8 -*-
#
# 起動時間プラグイン
#

# システムの起動・終了などをTweetする

require File.expand_path(File.join(File.dirname(__FILE__),'..', 'utils'))
require 'plugin/plugin'
require_if_exist 'sys/uptime'

Module.new do

  store = ConfigLoader.create("Plugin::Uptime")
  plugin = Plugin::create(:uptime)

  plugin.add_event(:boot){ |service|
    if FileTest.exist?('/tmp/computer_tweet_uptime') then
      false
    else
      open('/tmp/computer_tweet_uptime','w')
      service.post(:message => "おはよー。 #uptime #period")
    end }

  if defined?(Sys::Uptime)
    plugin.add_event(:period){ |service|
      uptime = Sys::Uptime.seconds
      last = store.at(:last, 0)
      store.store(:last, uptime)
      notice "last=#{dayof(last)}, uptime=#{dayof(uptime)}\n"
      service.post(:message => on_nextday(uptime, last)) if(dayof(uptime) > dayof(last)) }
  end

  def self.dayof(s)
    (s / 86400).to_i
  end

  def self.on_nextday(uptime, last)
    unless dayof(uptime) then return false end
    "連続起動#{dayof(uptime)+1}日目。 #uptime"
  end

end