<?php

/**
 * 同步表数据
 * User: ben
 * Date: 2018/10/28
 * Time: 下午2:08
 */

$index = (int) $argv[1];
require_once realpath(dirname(__FILE__)) . '/../common.php';

ini_set("display_errors", "On");
error_reporting(E_ALL & ~E_NOTICE);

$map = [];
$classList = [
    Sync_Redpack::class,
    Sync_RedpackLog::class,
    Sync_LoginLog::class,
];

if ($index == 0) {
    CallLog::setUrl('Sync_Base');

    for ($i = 0; $i < 10000; $i++) {
        $time = microtime(true);
        foreach ($classList as $className) {
            $obj = getObj($className);
            $flag = $obj->isTimeout($time);
            if ($flag) {
                // 时间到了，就通知一下他们拉取数据
                $obj->pubSubscribe(false);
            }
        }
        Tool::sleep(1);
    }
} else {
    CallLog::setUrl($classList[$index - 1]);

    $objSync = getObj($classList[$index - 1]);
    $objSync->syncDaemon();
}

/**
 * Sync_Base 对象初始化
 * @return Sync_Base DB对象
 */
function getObj($className) {
    global $map;
    if (!$map[$className]) {
        $map[$className] = new $className();
    }

    return $map[$className];
}
