Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/datastore-service
blob: 1a62dfc8423cf1a2678ab64f31bce2a6894e55fa (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
import sys, os, signal
import gobject
import dbus.service
import dbus.mainloop.glib
from olpc.datastore import DataStore, backingstore, logger
import logging

# Path handling
profile = os.environ.get('SUGAR_PROFILE', 'default')
base_dir = os.path.join(os.path.expanduser('~'), '.sugar', profile)
repo_dir = os.path.join(base_dir, 'datastore')

# operate from the repo directory
if not os.path.exists(repo_dir): os.makedirs(repo_dir)

log_dir = os.path.join(base_dir, "logs")
if not os.path.exists(log_dir): os.makedirs(log_dir)

#os.chdir(repo_dir)

# setup logger
logger.start('datastore')

# check for old lockfiles, the rules here are that we can't be
# connected to a tty. If we are not then in all likelyhood the process
# was started automatically, which hopefully implies a single instance
if not sys.stdin.isatty():
    lf = os.path.join(repo_dir, 'fulltext', 'flintlock')
    if os.path.exists(lf):
        logging.warning("Old lock file found -- removing.")
        os.unlink(lf)

        
# build the datastore
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
connected = True

ds = DataStore()
ds.registerBackend(backingstore.FileBackingStore)
ds.registerBackend(backingstore.InplaceFileBackingStore)

# and run it 
logging.info("Starting Datastore %s" % (repo_dir))
mainloop = gobject.MainLoop()

def handle_disconnect():
    global connected
    connected = False

def handle_shutdown(signum, frame):
    global connected
    connected = False
    raise SystemExit("Shutting down on signal %s" % signum)

bus.set_exit_on_disconnect(False)
bus.add_signal_receiver(handle_disconnect,
                        signal_name='Disconnected',
                        dbus_interface='org.freedesktop.DBus.Local')

signal.signal(signal.SIGHUP, handle_shutdown)
signal.signal(signal.SIGTERM, handle_shutdown)

def main():
    # XXX: The context/sleep loop is a work around for what seems
    # to be the mainloop blocking in such a way that the indexer
    # thread doesn't run until new dbus messages come in...
    # I intend to track this down post trial-2
    # really awful
    import time
    context = mainloop.get_context()

    if '-m' in sys.argv:
        # mount automatically for local testing
        ds.mount(repo_dir)
        ds.complete_indexing()
        
    try:
        while connected:
            context.iteration(False)
            time.sleep(0.0025)

    except KeyboardInterrupt:
        logging.info("DataStore shutdown by user")
    except:
        logging.debug("Datastore shutdown with error",
                      exc_info=sys.exc_info())
     
main()

ds.stop()

#import hotshot
#p = hotshot.Profile('hs.prof')
#p.run('main()')