|
Example
Project structureWe have one DB backend (named zoidberg) and one machine (bender), with server code running on it. CompilingAt first, download latest version of cexpert to working directory. We need to extract it and prepare for building. moriarty@bender:~/work/ce$ ls . cexpert-0.8.tar.gz moriarty@bender:~/work/ce$ tar -zxvf cexpert-0.8.tar.gz moriarty@bender:~/work/ce$ ls -al total 328 drwxr-xr-x 3 moriarty users 4096 2009-04-21 15:44 . drwxr-xr-x 4 moriarty users 4096 2009-04-21 15:43 .. drwxr-xr-x 8 moriarty users 4096 2009-04-21 15:33 cexpert-0.8 -rw-r--r-- 1 moriarty users 315789 2009-04-21 15:42 cexpert-0.8.tar.gz moriarty@bender:~/work/ce$ cd cexpert-0.8 moriarty@bender:~/work/ce/cexpert-0.8$ mkdir build moriarty@bender:~/work/ce/cexpert-0.8$ cd build moriarty@bender:~/work/ce/cexpert-0.8/build$ It's time to run standard building procedure: moriarty@bender:~/work/ce/cexpert-0.8/build$ ../configure --enable-python=yes moriarty@bender:~/work/ce/cexpert-0.8/build$ make && make check If everything is ok, you should see something like that: ........... make check-TESTS make[2]: Entering directory `/home/moriarty/work/ce/cexpert-0.8/build/tests' shmem check: 0.1.2.3.4.5.6 shmem check: 0.1.2.3.4.5.6 DBChecker check 0 DBChecker check 1 DBChecker check 2 DBChecker check 3 DBChecker check 4 PASS: test_server start client test initiated, check now PASS: test_cexpert_lib ================== All 2 tests passed ================== ............ Now, run "sudo make install", which place libraries and headers to right places. Running server processesThere are two ways of running daemons: using system-wide daemons set (via init.d script), or directly from client code (see Running daemons manually section). Using init.d script: moriarty@bender:~/work/ce/cexpert-0.8/build$/usr/local/bin/cexpert_s mysql://user:pasword@zoidberg:3306/test_test moriarty@bender:~/work/ce/cexpert-0.8/build$ ps ux | grep cexpert_s moriarty 26901 0.0 0.0 13892 1944 ? Sl 15:58 0:00 /usr/local/bin/cexpert_s mysql.user.zoidberg.3306.test_test moriarty 26914 0.0 0.0 2816 748 pts/9 S+ 15:59 0:00 grep cexpert_s You also can use init.d script: moriarty@bender:~/work/ce/cexpert-0.8/build$ cat /etc/cexpert.d/test mysql://user:password@zoidberg:3306/test_test moriarty@bender:~/work/ce/cexpert-0.8/build$ sudo /etc/init.d/cexpertd start moriarty@bender:~/work/ce/cexpert-0.8/build$ ps aux | grep cexpert root 26973 0.0 0.0 13756 1788 ? Sl 16:01 0:00 /usr/local/bin/cexpert_s mysql.user.zoidberg.3306.test_test moriarty 26993 0.0 0.0 2820 756 pts/9 S+ 16:01 0:00 grep cexpert Using client libraryPython bindings is the easiest way to demonstrate usage (C and C++ libraries has similar interface). moriarty@bender:~/work/ce/cexpert-0.8/build$ ipython
In [1]: import libpycexpert
In [2]: libpycexpert.get_is_available("mysql://user:password@zoidberg:3306/test_test")
Out[2]: 1Now, let's broke server-DB connectivity: moriarty@bender:~$ sudo iptables -A OUTPUT -p tcp --dport 3306 -j REJECT --reject-with tcp-reset Check our python process: In [3]: libpycexpert.get_is_available("mysql://user:password@zoidberg:3306/test_test")
Out[3]: 0After restoring connectivity: In [4]: libpycexpert.get_is_available("mysql://user:password@zoidberg:3306/test_test")
Out[4]: 1Extended usageIt's possible to know when was the last time the daemon checks the status of the base: In [17]: libpycexpert.get_is_available_ex("mysql://blogger:feedpass@zoidberg:3306/test3")
Out[17]: (1, 1241111982)
In [18]: libpycexpert.get_is_available_ex("mysql://blogger:feedpass@zoidberg:3306/test3")
Out[18]: (1, 1241112225)Value, returned by get_is_available_ex is tuple (is_base_available, last_check_timestamp). Common way of using this values: def get_db_status(connection_str):
(is_ok, last_check_time) = libpycexpert.get_is_available_ex(connection_str)
if time.time() - last_check_time > 5:
#PANIC!!! Probably, the daemon is stopped. We could try to restart it, make a record in system log, or anything else.
return is_ok #last checked statusRunning daemons manuallyit's frequently use-case to connect by dynamically builded connection string. In such case, it can't be pre-written to /etc/cexpert.d/ files. So, we need capability to run daemons from our client code: In [1]: import libpycexpert
In [2]: libpycexpert.is_registered("mysql://user:password@zoidberg:3306/test3")
Out[2]: 0
In [3]: libpycexpert.do_register("mysql://user:password@zoidberg:3306/test3")
Out[3]: 1Now, the daemon starts: moriarty@bender:~/src/cexpert$ ps ux | grep cexp moriarty 622 0.0 0.0 13500 1676 ? Sl 21:08 0:00 cexpert_s mysql.user.zoidberg.3306.test3 And we can check for DB's status or stop daemon: In [7]: libpycexpert.get_is_available("mysql://user:password@zoidberg:3306/test3")
Out[7]: 1
In [8]: libpycexpert.do_unregister("mysql://user:password@zoidberg:3306/test3")
Out[8]: 1
In [9]: libpycexpert.is_registered("mysql://user:password@zoidberg:3306/test3")
Out[9]: 0
|
Sign in to add a comment