<commit_before><?php
namespace CvoTechnologies\Gearman;

use Cake\Core\Configure;
use GearmanClient;
use GearmanWorker;

trait JobAwareTrait
{

    /**
     * Setup a GearmanClient object configured
     * with the servers from the configuration.
     * 
     * @return GearmanClient
     */
    public function gearmanClient() {
        $client = new GearmanClient();

        $servers = Configure::read('Gearman.Servers');

        $client->addServers(implode(',', $servers));

        return $client;
    }

    /**
     * Setup a GearmanWorker object configured
     * with the servers from the configuration.
     * 
     * @return GearmanWorker
     */
    public function gearmanWorker() {
        $client = new GearmanWorker();

        $servers = Configure::read('Gearman.Servers');

        $client->addServers(implode(',', $servers));

        return $client;
    }

    /**
     * Execute a job by sending it to the
     * Gearman server using the GearmanClient
     * 
     * Example for a background job with normal priority:
     * $this->execute('sleep', ['seconds' => 60]);
     * 
     * Example for a background job with HIGH priority:
     * $this->execute('sleep', ['seconds' => 60], true, Gearman::PRIORITY_HIGH);
     *
     * Example for a normal job with HIGH priority:
     * $this->execute('sleep', ['seconds' => 60], false, Gearman::PRIORITY_HIGH);
     *      * 
     * @param string $name Name of the job to execute
     * @param mixed $workload Any type of workload is supported (as long as it's serializable)
     * @param bool $background If it's a background job
     * @param int $priority A priority level from Gearman::PRIORTIY_*
     */
    public function execute($name, $workload, $background = true, $priority = Gearman::PRIORITY_NORMAL) {
        $func = 'do';
        switch ($priority) {
            case Gearman::PRIORITY_LOW:
                $func .= 'Low';
                break;
            case Gearman::PRIORITY_HIGH:
                $func .= 'High';
                break;
            case Gearman::PRIORITY_NORMAL:
                if (!$background) {
                    $func .= 'Normal';
                }
                break;
        }
        $func .= ($background) ? 'Background' : '';

        if (is_array($workload) || is_object($workload)) {
            $workload = serialize($workload);
        }

        $response = $this->gearmanClient()->{$func}($name, $workload);

        DebugJob::add($name, $workload, $background, $priority);

        if ($background) {
            return $response;
        }

        $serializedResponse = unserialize($response);

        if ($serializedResponse !== false) {
            $response = $serializedResponse;
        }

        return $response;
    }

}
<commit_msg>Check for required configuration settings<commit_after><?php
namespace CvoTechnologies\Gearman;

use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use GearmanClient;
use GearmanWorker;

trait JobAwareTrait
{

    /**
     * Setup a GearmanClient object configured
     * with the servers from the configuration.
     * 
     * @return GearmanClient
     */
    public function gearmanClient()
    {
        $client = new GearmanClient();

        $servers = self::getServers();

        $client->addServers(implode(',', $servers));

        return $client;
    }

    /**
     * Setup a GearmanWorker object configured
     * with the servers from the configuration.
     * 
     * @return GearmanWorker
     */
    public function gearmanWorker()
    {
        $client = new GearmanWorker();

        $servers = self::getServers();

        $client->addServers(implode(',', $servers));

        return $client;
    }

    /**
     * Execute a job by sending it to the
     * Gearman server using the GearmanClient
     * 
     * Example for a background job with normal priority:
     * $this->execute('sleep', ['seconds' => 60]);
     * 
     * Example for a background job with HIGH priority:
     * $this->execute('sleep', ['seconds' => 60], true, Gearman::PRIORITY_HIGH);
     *
     * Example for a normal job with HIGH priority:
     * $this->execute('sleep', ['seconds' => 60], false, Gearman::PRIORITY_HIGH);
     *      * 
     * @param string $name Name of the job to execute
     * @param mixed $workload Any type of workload is supported (as long as it's serializable)
     * @param bool $background If it's a background job
     * @param int $priority A priority level from Gearman::PRIORTIY_*
     */
    public function execute($name, $workload, $background = true, $priority = Gearman::PRIORITY_NORMAL)
    {
        $func = 'do';
        switch ($priority) {
            case Gearman::PRIORITY_LOW:
                $func .= 'Low';
                break;
            case Gearman::PRIORITY_HIGH:
                $func .= 'High';
                break;
            case Gearman::PRIORITY_NORMAL:
                if (!$background) {
                    $func .= 'Normal';
                }
                break;
        }
        $func .= ($background) ? 'Background' : '';

        if (is_array($workload) || is_object($workload)) {
            $workload = serialize($workload);
        }

        $response = $this->gearmanClient()->{$func}($name, $workload);

        DebugJob::add($name, $workload, $background, $priority);

        if ($background) {
            return $response;
        }

        $serializedResponse = unserialize($response);

        if ($serializedResponse !== false) {
            $response = $serializedResponse;
        }

        return $response;
    }

    /**
     * Returns an array with all configured Gearman servers.
     *
     * @throws \Cake\Core\Exception\Exception
     * @return array Flat array with servers as found in Configure
     */
    protected static function getServers()
    {
        $servers = Configure::readOrFail('Gearman.Servers');

        if (empty($servers)) {
            throw new Exception('Invalid Gearman configuration: you must configure at least one server');
        }

        return $servers;
    }
}

{'fraction_non_alphanumeric': 0.12104743083003952, 'fraction_numerical': 0.001976284584980237, 'mean_word_length': 2.140124095139607, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '197643', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1674, 'n_words': 462}