Single version of Jobbar is ready to go.
Single Server, Socket Handler, Worker Registration, Job Requests & Responses features are available with this version. Worker part is distributed but the job server works single.
You can use this single version for authentication, logging, parallel processing, caching, multitasking, etc. All you have to do is to write workers for each case or you can wait till I get distributed version ready. After that, I’ll publish some tutorials for different cases.
By the way it’s written in Java and available under MIT license.
Here, I’m gonna explain how to get your Web.py application works with your Apache web server via mod_wsgi in Ubuntu 10.04. The key is mod_wsgi here.
To install mod_wsgi, please run that command first.
sudo apt-get install libapache2-mod-wsgi
Once you have mod_wsgi installed, you can modify your Apache server configuration to run with your Web.py server.
<VirtualHost *:80>
ServerName your-domain
WSGIScriptAlias / /path-to-your-webpy-file/server.py/
AddType text/html .py
<Directory /path-to-your-webpy-file/>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now, we need to make a little bit modification on our code. Please add this line before importing your own (custom) libraries.
sys.path.append(os.path.dirname(__file__))
For more information you can visit official website.
As many of you already know, views are not actual tables. Views are only snapshots of data scopes which pre-defined by you. Because of that, you cannot create any index on a View. But in PostgreSQL, I have informed about a different type of view by my friend: Materialized View.
At the very first time, it’s relief to know that there is a structure to store dynamic data and use some indexes on it. But, nothing is what it seems. Lets check what’s wrong here:
CREATE OR REPLACE FUNCTION refresh_matview(name) RETURNS VOID
SECURITY DEFINER
LANGUAGE plpgsql AS '
DECLARE
matview ALIAS FOR $1;
entry matviews%ROWTYPE;
BEGIN
SELECT * INTO entry FROM matviews WHERE mv_name = matview;
IF NOT FOUND THEN
RAISE EXCEPTION <nowiki>''Materialized view % does not exist.'', matview;
END IF;
EXECUTE ''DELETE FROM '' || matview;
EXECUTE ''INSERT INTO '' || matview
|| '' SELECT * FROM '' || entry.v_name;
UPDATE matviews
SET last_refresh=CURRENT_TIMESTAMP
WHERE mv_name=matview;
RETURN;
END
';</nowiki>
Materialized Views work on trigger model. If anything happens on your actual tables there are a few functions to call for changing your Materialized Views. Everything is OK at this far. The code block above creates a function on your database server to update your Materialized View. As you can see, on every change this function deletes all data in your mat_view and inserts all data from your original view. This is nothing but deadly cost.
So, what can we do instead of using Materialized Views?
If you have more experience on PostgreSQL than me maybe you can find a better solution but I’m not familiar with PostgreSQL as I’m familier with MySQL. Because of that I handle that kind of works with Gearman Worker as a background job and I put all logic into the worker instead of using any kind of view in PostgreSQL.
GittiGidiyor.com provides an API to make online business. You can find more information at Developer’s Website.
Here, I want to make some modifications on their PHP client to make it suitable with Symfony Framework. I prefer to use version 1.4 but it also works with older versions.
Please, add these settings to your app.yml:
all:
gittigidiyor:
apiKey: APPLICATION-KEY
secretKey: SECRET-KEY
nick: NICK
password: PASSWORD
authUser: AUTHENTICATION-USERNAME
authPass: AUTHENTICATION-PASSWORD
lang: LANGUAGE
Now, we can reach them via sfConfig::get(). This way is better than INI file while you’re working with Symfony Framework.
You can download the code at Github. By the way, I have modified only __construct method. If you want to make this modification yourself please replace the original __construct with the code below:
public function __construct(){
$this->apiKey = sfConfig::get('app_gittigidiyor_apiKey');
$this->secretKey = sfConfig::get('app_gittigidiyor_secretKey');
$this->nick = sfConfig::get('app_gittigidiyor_nick');
$this->password = sfConfig::get('app_gittigidiyor_password');
$this->auth_user = sfConfig::get('app_gittigidiyor_authUser');
$this->auth_pass = sfConfig::get('app_gittigidiyor_authPass');
$this->lang = sfConfig::get('app_gittigidiyor_lang');
list($usec, $sec) = explode(" ", microtime());
$this->time = round(((float)$usec + (float)$sec) * 100).'0';
$this->sign = md5($this->apiKey.$this->secretKey.$this->time);
}
In Ubuntu 10.04, I started with running that command in shell:
$ sudo apt-get install python-webpy
After that, I was ready for importing web library.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
import web.utils
Then, I modified url handling rules for my application. Url handling is based on classes. We need to specify class name for each url.
urls = (
'/(.*)', 'MyClass'
)
Most important thing is that these classes must have method(s) called POST and/or GET. Because these methods will be called automatically by Web.py
class MyClass:
def __init__(self):
pass
def POST(self, query):
pass
def GET(self, query):
pass
Basically, that’s how we implement Web.py but I want to share more useful things.
def __init__(self):
web.config.debug = False
self.cache = memcache.Client(['%s:%s' % (host, port)])
self.db = web.database(dbn='postgres',
driver='psycopg2',
db='%s', user='%s',
host='localhost',
password='%s' %
(dbname, dbuser, dbpass))
You need to install python-memcache library before importing memcache to your application.
If you want to handle parameters, that’s easy too. web.input() returns you an object for parameters.
params = web.input()
print params.name
print params.surname
Handling Query Strings:
import urlparse
params = urlparse.parse_qs(web.ctx.query[1:])
print params.get('param_name')[0]
Some Examples:
self.cache.set(key, val)
val = self.cache.get(key)
resultset = self.db.query("SELECT id FROM test_table
WHERE username='%s';"
% name)
if len(resultset):
id = resultset[0].id
For more information please check official website.