Package skytools :: Module scripting :: Class DBScript
[frames] | no frames]

Class DBScript

source code

object --+
         |
        DBScript
Known Subclasses:
pgq.ticker.SmartTicker, pgq.consumer.Consumer, pgq.maint.MaintenanceJob, pgq.status.PGQStatus, londiste.syncer.Syncer, londiste.setup.CommonSetup

Base class for database scripts.

Handles logging, daemonizing, config, errors.

Instance Methods
 
__init__(self, service_name, args)
Script setup.
source code
 
close_database(self, dbname)
Explicitly close a cached connection.
source code
 
get_database(self, dbname, autocommit=0, isolation_level=-1, cache=None, max_age=1200)
Load cached database connection.
source code
 
hook_sighup(self, sig, frame)
Internal SIGHUP handler.
source code
 
hook_sigint(self, sig, frame)
Internal SIGINT handler.
source code
 
init_optparse(self, parser=None)
Initialize a OptionParser() instance that will be used to parse command line arguments.
source code
 
reload(self)
Reload config.
source code
 
reset(self)
Something bad happened, reset all connections.
source code
 
run(self)
Thread main loop.
source code
 
run_once(self)
Run users work function, safely.
source code
 
send_signal(self, sig) source code
 
send_stats(self)
Send statistics to log.
source code
 
set_single_loop(self, do_single_loop)
Changes whether the script will loop or not.
source code
 
start(self)
This will launch main processing thread.
source code
 
startup(self)
Will be called just before entering main loop.
source code
 
stat_add(self, key, value) source code
 
stat_increase(self, key, increase=1)
Increases a stat value.
source code
 
stat_put(self, key, value)
Sets a stat value.
source code
 
stop(self)
Safely stops processing loop.
source code
 
work(self)
Here should user's processing happen.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Class Variables
  cf = None
  job_name = None
  log = None
  service_name = None
Properties

Inherited from object: __class__

Method Details

__init__(self, service_name, args)
(Constructor)

source code 

Script setup.

User class should override work() and optionally __init__(), startup(), reload(), reset() and init_optparse().

NB: in case of daemon, the __init__() and startup()/work() will be run in different processes. So nothing fancy should be done in __init__().
Parameters:
  • service_name - unique name for script. It will be also default job_name, if not specified in config.
  • args - cmdline args (sys.argv[1:]), but can be overrided
Overrides: object.__init__

close_database(self, dbname)

source code 

Explicitly close a cached connection.

Next call to get_database() will reconnect.

get_database(self, dbname, autocommit=0, isolation_level=-1, cache=None, max_age=1200)

source code 

Load cached database connection.

User must not store it permanently somewhere, as all connections will be invalidated on reset.

hook_sighup(self, sig, frame)

source code 
Internal SIGHUP handler. Minimal code here.

hook_sigint(self, sig, frame)

source code 
Internal SIGINT handler. Minimal code here.

init_optparse(self, parser=None)

source code 

Initialize a OptionParser() instance that will be used to parse command line arguments.

Note that it can be overrided both directions - either DBScript will initialize a instance and passes to user code or user can initialize and then pass to DBScript.init_optparse().
Parameters:
  • parser - optional OptionParser() instance, where DBScript should attachs its own arguments.
Returns:
initialized OptionParser() instance.

startup(self)

source code 

Will be called just before entering main loop.

In case of daemon, if will be called in same process as work(), unlike __init__().