#!/usr/bin/php
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../include');

require_once 'Net/Gearman/Worker.php';
require_once 'shard-query.php';
require_once 'DAL/simple-dal.php';
ini_set('memory_limit','-1');
try {

    if(file_exists('../include/config.inc')) {
	require_once('../include/shard-query-config.php');

	$db = SimpleDal::factory($config_database);
	$db->my_select_db($config_database['db']);
	$sql = "select * from gearman_job_servers join schemata on schemata.id = schema_id where schema_name = '" . $config_database['default_virtual_schema'] . "'" ;
	$stmt = $db->my_query($sql);
	$gearman_servers = "";
	while($row = $db->my_fetch_assoc()) {
		if($gearman_servers) $gearman_servers .= ",";
		`mysql -uroot -e'select gman_servers_set("{$row['hostname']}:{$row['port']}")'`;
		$gearman_servers .= $row['hostname'] . ":" . $row['port'];
	}
	
    } else {
        $gearman_servers="127.0.0.1:7001";
    }
    $worker = new Net_Gearman_Worker($gearman_servers);
#    $worker->addAbility('store_resultset');
    $worker->addAbility('shard_query_worker');
    $worker->addAbility('custom_function');
 #   $worker->addAbility('loader');
    $worker->beginWork();
} catch (Net_Gearman_Exception $e) {
    echo $e->getMessage() . "\n";
    exit;
}


?>
