.
 *
 * END LICENSE
 *
 * @author Rushikesh Padia padiarushi3012@gmail.com
 * (Reworked so could scale for yioop.com by Chris Pollett)
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2022
 * @filesource
 */

namespace seekquarry\yioop\library\media_jobs;

use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;


/**
 * Refreshes caches by executing queries.
 * Executed queries are cached by search engine.
 * Queries to be cached are specified in files located at WORK_DIRECTORY/cache/cache_queries
 */
class CacheRefreshJob extends MediaJob
{
    /**
     * How long to wait to send next cache query
     */
    const TIME_BETWEEN_REQUEST_IN_SECONDS = 1;

    /**
     * Time in current epoch when cache were last refreshed
     * @var int
     */
    public $update_time;

    /**
     * How long to wait to refresh the cache in seconds
     */
    const CACHE_REFRESH_INTERVAL = C\ONE_DAY;

    /**
     * Sets up the database connection so can access tables related
     * to recommendations. Initialize timing info related to job.
     */
    public function init()
    {
        $this->update_time = 0;
        $this->name_server_does_client_tasks = true;
        $this->name_server_does_client_tasks_only = true;
    }

    /**x
     * Only update if it's been more than a day since the last update
     *
     * @return bool whether it's been a day since the last update
     */
    public function checkPrerequisites()
    {
        $time = time();
        $delta = $time - $this->update_time;
        if ($delta > self::CACHE_REFRESH_INTERVAL) {
            $this->update_time = $time;
            L\crawlLog("Prerequisites for CacheRefresh Media Job met");
            return true;
        }
        L\crawlLog("Time since last update not exceeded, skipping" .
            " CacheRefresh MediaJob $delta");
        return false;
    }

    /**
     * Run the same task as in the distributed setting
     */
    public function nondistributedTasks()
    {
        L\crawlLog("Name server is refreshing cache");
        $this->doTasks(array());
    }

    /**
     * Queries the search engine for each cache query.
     * Executed queries are automatically cached by the search engine.
     *
     * @param $tasks array ignored
     * @return void
     */
    public function doTasks($tasks)
    {
        L\crawlLog("This media updater is refreshing cache");
        $queries = $this->getQueryList();
        $i = 0;
        foreach ($queries as $query) {
            $i++;
            L\crawlLog("Query $i: " . $query);
            $ignore_response = L\FetchUrl::getPage(C\BASE_URL . "?q=" . urlencode($query));
            sleep(self::TIME_BETWEEN_REQUEST_IN_SECONDS);
        }
        L\crawlLog("Cache refresh completed, total queries executed: " . $i);
    }

    /**
     * Collects queries from all the files located in WORK_DIRECTORY/cache/cache_queries directory
     * @return array list of queries to be executed
     */
    private function getQueryList(): array
    {
        $queries = [];
        $dirname = C\WORK_DIRECTORY . "/cache/cache_queries";
        foreach (glob($dirname . "/*") as $cache_file) {
            if (file_exists($cache_file)) {
                $file_content = file_get_contents($cache_file);
                $trimmed_content = trim($file_content);
                $raw_queries = explode("\n", $trimmed_content);
                foreach ($raw_queries as $raw_query) {
                    $query = trim($raw_query);
                    if (!empty($query)) {
                        $queries[] = $query;
                    }
                }
            }
        }
        return $queries;
    }

}