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

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/gcollect.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

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.persiststate import PlugState
14
 
import gc, time
15
 
 
16
 
state = PlugState()
17
 
state.define('wait', 300)
18
 
state.define('enable', 0)
19
 
 
20
 
if state['enable']:
21
 
    gc.enable()
22
 
    rlog(10, 'gcollect', 'garbage collector enabled .. wait is %s' % state['wait'])
23
 
 
24
 
def shutdown():
25
 
    periodical.kill()
26
 
 
27
 
def gcollect():
28
 
    rlog(1, 'gcollect', 'running collector')
29
 
    gc.collect()
30
 
    time.sleep(5)
31
 
    gc.collect()
32
 
    time.sleep(5)
33
 
    gc.collect()
34
 
 
35
 
if state['enable']:
36
 
    pid = periodical.addjob(state['wait'], 0, gcollect)
37
 
else:
38
 
    pid = None
39
 
 
40
 
def handle_gcollectwait(bot, ievent):
41
 
    try:
42
 
        newwait = int(ievent.args[0])
43
 
    except (IndexError, ValueError):
44
 
        ievent.reply('gcollect wait is %s seconds' % state['wait'])
45
 
        return
46
 
    if newwait < 60:
47
 
        ievent.reply('min. number of seconds is 60')
48
 
        return
49
 
    state['wait'] = newwait
50
 
    state.save()
51
 
    if pid:
52
 
        periodical.changeinterval(pid, newwait)
53
 
    ievent.reply('gcollect wait set to %s' % state['wait'])
54
 
 
55
 
cmnds.add('gcollect-wait', handle_gcollectwait, 'OPER')
56
 
examples.add('gcollect-wait', 'set wait of garbage collector', \
57
 
'gcollect-wait 300')
58
 
 
59
 
def handle_gcollect(bot, ievent):
60
 
    gcollect()
61
 
    ievent.reply('collector runned')
62
 
 
63
 
cmnds.add('gcollect', handle_gcollect, 'OPER')
64
 
examples.add('gcollect', 'run garbage collector', 'gcollect')
65
 
 
66
 
def handle_gcollectenable(bot, ievent):
67
 
    global pid
68
 
    state['enable'] = 1
69
 
    state.save()
70
 
    pid = periodical.addjob(state['wait'], 0, gcollect)
71
 
    ievent.reply('gcollect enabled')
72
 
 
73
 
cmnds.add('gcollect-enable', handle_gcollectenable, 'OPER')
74
 
examples.add('gcollect-enable', 'enable the garbage collector', 'gcollect-enable')
75
 
 
76
 
def handle_gcollectdisable(bot, ievent):
77
 
    state['enable'] = 0
78
 
    state.save()
79
 
    periodical.kill()
80
 
    ievent.reply('gcollect disabled')
81
 
 
82
 
cmnds.add('gcollect-disable', handle_gcollectdisable, 'OPER')
83
 
examples.add('gcollect-disable', 'disable the garbage collector', 'gcollect-disable')