~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gplugs/gcollect.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plugs/gcollect.py
 
2
#
 
3
#
 
4
 
 
5
""" run garbage collector """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from gozerbot.periodical import periodical
 
10
from gozerbot.generic import rlog
 
11
from gozerbot.commands import cmnds
 
12
from gozerbot.examples import examples
 
13
from gozerbot.persist.persiststate import PlugState
 
14
from gozerbot.plughelp import plughelp
 
15
from gozerbot.tests import tests
 
16
 
 
17
import gc, time
 
18
 
 
19
plughelp.add('gcollect', 'help the garbage collector')
 
20
 
 
21
state = PlugState()
 
22
state.define('wait', 300)
 
23
state.define('enable', 0)
 
24
 
 
25
if state['enable']:
 
26
    gc.enable()
 
27
    rlog(10, 'gcollect', 'garbage collector enabled .. wait is %s' % state['wait'])
 
28
 
 
29
def shutdown():
 
30
    periodical.kill()
 
31
 
 
32
def gcollect():
 
33
    rlog(1, 'gcollect', 'running collector')
 
34
    gc.collect()
 
35
    time.sleep(5)
 
36
    gc.collect()
 
37
    time.sleep(5)
 
38
    gc.collect()
 
39
 
 
40
if state['enable']:
 
41
    pid = periodical.addjob(state['wait'], 0, gcollect)
 
42
else:
 
43
    pid = None
 
44
 
 
45
def handle_gcollectwait(bot, ievent):
 
46
    try:
 
47
        newwait = int(ievent.args[0])
 
48
    except (IndexError, ValueError):
 
49
        ievent.reply('gcollect wait is %s seconds' % state['wait'])
 
50
        return
 
51
    if newwait < 60:
 
52
        ievent.reply('min. number of seconds is 60')
 
53
        return
 
54
    state['wait'] = newwait
 
55
    state.save()
 
56
    if pid:
 
57
        periodical.changeinterval(pid, newwait)
 
58
    ievent.reply('gcollect wait set to %s' % state['wait'])
 
59
 
 
60
cmnds.add('gcollect-wait', handle_gcollectwait, 'OPER')
 
61
examples.add('gcollect-wait', 'set wait of garbage collector', \
 
62
'gcollect-wait 300')
 
63
tests.add('gcollect-wait 300')
 
64
 
 
65
def handle_gcollect(bot, ievent):
 
66
    gcollect()
 
67
    ievent.reply('collector runned')
 
68
 
 
69
cmnds.add('gcollect', handle_gcollect, 'OPER', threaded=True)
 
70
examples.add('gcollect', 'run garbage collector', 'gcollect')
 
71
tests.add('gcollect')
 
72
 
 
73
def handle_gcollectenable(bot, ievent):
 
74
    global pid
 
75
    state['enable'] = 1
 
76
    state.save()
 
77
    pid = periodical.addjob(state['wait'], 0, gcollect)
 
78
    ievent.reply('gcollect enabled')
 
79
 
 
80
cmnds.add('gcollect-enable', handle_gcollectenable, 'OPER')
 
81
examples.add('gcollect-enable', 'enable the garbage collector', 'gcollect-enable')
 
82
tests.add('gcollect-enable')
 
83
 
 
84
def handle_gcollectdisable(bot, ievent):
 
85
    state['enable'] = 0
 
86
    state.save()
 
87
    periodical.kill()
 
88
    ievent.reply('gcollect disabled')
 
89
 
 
90
cmnds.add('gcollect-disable', handle_gcollectdisable, 'OPER')
 
91
examples.add('gcollect-disable', 'disable the garbage collector', 'gcollect-disable')
 
92
tests.add('gcollect-disable')