Scalr\System\Zmq\Cron\Task\Scaling::enqueue PHP Method

enqueue() public method

See also: Scalr\System\Zmq\Cron\TaskInterface::enqueue()
public enqueue ( )
    public function enqueue()
    {
        $queue = new ArrayObject([]);
        //This is necessary for the next query
        $this->db->Execute("SET @fid := NULL, @num := NULL");
        //Selects one Farm Role from each Farm with synchronous lauhch type and
        //all Farm Roles from each Farm with asynchronous launch type
        $rs = $this->db->Execute("\n            SELECT * FROM (\n                SELECT IF(f.`farm_roles_launch_order` = 1, @num := IF(@fid = f.`id`, @num + 1, 1), 1) `row_number`,\n            \t    @fid := f.`id` `farm_id`,\n            \t    f.`name` `farm_name`,\n            \t    fr.`id` `farm_role_id`,\n            \t    rs.`value` `dt_last_polling`,\n            \t    rs2.`value` `polling_interval`,\n                    f.`farm_roles_launch_order`\n                FROM `farms` f\n                JOIN `clients` c ON c.`id` = f.`clientid`\n                JOIN `client_environments` ce ON ce.`id` = f.`env_id`\n                JOIN `farm_roles` fr ON fr.`farmid` = f.`id`\n                LEFT JOIN `farm_role_settings` rs  ON rs.`farm_roleid` = fr.`id` AND rs.`name` = ?\n                LEFT JOIN `farm_role_settings` rs2 ON rs2.`farm_roleid` = fr.`id` AND rs2.`name` = ?\n                WHERE c.`status` = ? AND ce.`status` = ? AND f.`status` = ?\n                AND (rs.`value` IS NULL OR UNIX_TIMESTAMP() > rs.`value` + IFNULL(rs2.`value`, 1) * 60)\n                ORDER BY f.`id`, fr.`launch_index`\n            ) t WHERE t.`row_number` = 1\n        ", [Entity\FarmRoleSetting::SCALING_LAST_POLLING_TIME, Entity\FarmRoleSetting::SCALING_POLLING_INTERVAL, Scalr_Account::STATUS_ACTIVE, Scalr_Environment::STATUS_ACTIVE, FARM_STATUS::RUNNING]);
        while ($row = $rs->FetchRow()) {
            $obj = new stdClass();
            $obj->farmId = $row['farm_id'];
            $obj->farmName = $row['farm_name'];
            if (!$row['farm_roles_launch_order']) {
                //Asynchronous launch order
                $obj->farmRoleId = $row['farm_role_id'];
            }
            $queue->append($obj);
        }
        if ($count = $queue->count()) {
            $this->getLogger()->info("%d running farm roles found.", $count);
        }
        return $queue;
    }