Scalr\System\Zmq\Cron\Task\CloudPricing::worker PHP Метод

worker() публичный Метод

См. также: Scalr\System\Zmq\Cron\TaskInterface::worker()
public worker ( $request )
    public function worker($request)
    {
        if (!\Scalr::getContainer()->analytics->enabled) {
            $this->log("WARN", "Cannot process the request. Cloud cost analytics is disabled in config.");
            return false;
        }
        $this->log("INFO", "Processing region: %s", $request->region);
        $now = new DateTime('now', new DateTimeZone('UTC'));
        $cadb = \Scalr::getContainer()->cadb;
        $os = strpos($request->url, 'linux') !== false ? PriceEntity::OS_LINUX : PriceEntity::OS_WINDOWS;
        foreach ($request->instanceTypes as $it) {
            if (!isset(self::$mapping[$request->region])) {
                throw new Exception(sprintf("Region %s does not exist in the mapping.", $request->region));
            }
            $region = self::$mapping[$request->region];
            $latest = [];
            //Gets latest prices for all instance types from current region.
            $res = $cadb->Execute("\n                SELECT p.instance_type, ph.applied, p.os, p.name, HEX(p.price_id) `price_id`, p.cost\n                FROM price_history ph\n                JOIN prices p ON p.price_id = ph.price_id\n                LEFT JOIN price_history ph2 ON ph2.platform = ph.platform\n                    AND ph2.cloud_location = ph.cloud_location\n                    AND ph2.account_id = ph.account_id\n                    AND ph2.url = ph.url\n                    AND ph2.applied > ph.applied AND ph2.applied <= ?\n                LEFT JOIN prices p2 ON p2.price_id = ph2.price_id\n                    AND p2.instance_type = p.instance_type\n                    AND p2.os = p.os\n                WHERE ph.account_id = 0 AND p2.price_id IS NULL\n                AND ph.platform = 'ec2'\n                AND ph.cloud_location = ?\n                AND ph.url = ''\n                AND ph.applied <= ?\n            ", [$now->format('Y-m-d'), $region, $now->format('Y-m-d')]);
            while ($rec = $res->FetchRow()) {
                $latest[$rec['instance_type']][$rec['os']] = ['applied' => $rec['applied'], 'price_id' => $rec['price_id'], 'cost' => $rec['cost']];
            }
            $needUpdate = false;
            foreach ($it->sizes as $sz) {
                foreach ($sz->valueColumns as $v) {
                    if (!is_numeric($v->prices->USD) || $v->prices->USD < 1.0E-6) {
                        continue;
                    }
                    if (!isset($latest[$sz->size][$os])) {
                        $needUpdate = true;
                    } else {
                        if (abs(($latest[$sz->size][$os]['cost'] - $v->prices->USD) / $v->prices->USD) > 1.0E-6) {
                            $needUpdate = true;
                            $latest[$sz->size][$os]['cost'] = $v->prices->USD;
                        } else {
                            continue;
                        }
                    }
                    $latest[$sz->size][$os] = ['cost' => $v->prices->USD];
                }
            }
            if ($needUpdate) {
                $priceid = $cadb->GetOne("\n                    SELECT HEX(`price_id`) AS `price_id`\n                    FROM price_history\n                    WHERE platform = 'ec2'\n                    AND url = ''\n                    AND cloud_location = ?\n                    AND applied = ?\n                    AND account_id = 0\n                    LIMIT 1\n                ", [$region, $now->format('Y-m-d')]);
                if (!$priceid) {
                    $priceid = str_replace('-', '', \Scalr::GenerateUID());
                    $cadb->Execute("\n                        INSERT price_history\n                        SET price_id = UNHEX(?),\n                            platform = 'ec2',\n                            url = '',\n                            cloud_location = ?,\n                            account_id = 0,\n                            applied = ?,\n                            deny_override = 0\n                    ", [$priceid, $region, $now->format('Y-m-d')]);
                }
                foreach ($latest as $instanceType => $ld) {
                    foreach ($ld as $os => $v) {
                        $cadb->Execute("\n                            REPLACE prices\n                            SET price_id = UNHEX(?),\n                                instance_type = ?,\n                                name = ?,\n                                os = ?,\n                                cost = ?\n                        ", [$priceid, $instanceType, $instanceType, $os, $v['cost']]);
                    }
                }
            }
        }
        $ret = new stdClass();
        $ret->url = $request->url;
        $ret->region = $request->region;
        return $ret;
    }