public function add($hook, $args = array(), $priority = 'normal') { $jobdata = array(); $jobdata['hook'] = $hook; $jobdata['args'] = $args; $jobdata['blog_id'] = function_exists('is_multisite') && is_multisite() ? get_current_blog_id() : null; switch ($priority) { case 'high': return $this->_client->doHighBackground($this->gearman_function(), json_encode($jobdata)); break; case 'low': return $this->_client->doLowBackground($this->gearman_function(), json_encode($jobdata)); break; case 'normal': default: return $this->_client->doBackground($this->gearman_function(), json_encode($jobdata)); break; } }
protected function execute(InputInterface $input, OutputInterface $output) { /* @var $em EntityManager */ $em = $this->getContainer()->get('doctrine')->getManager(); $waktuSekarang = new \DateTime(); $perulangan = JadwalKehadiran::getDaftarPerulangan(); $workload['waktu_sekarang'] = $waktuSekarang; $workload['paksa'] = $input->getOption('paksa'); $semuaSekolah = $em->getRepository('LanggasSisdikBundle:Sekolah')->findAll(); foreach ($semuaSekolah as $sekolah) { if (!(is_object($sekolah) && $sekolah instanceof Sekolah)) { continue; } $workload['sekolah'] = $sekolah->getId(); if (!$input->getOption('paksa')) { $kalenderPendidikan = $em->getRepository('LanggasSisdikBundle:KalenderPendidikan')->findOneBy(['sekolah' => $sekolah, 'tanggal' => $waktuSekarang, 'kbm' => true]); if (!(is_object($kalenderPendidikan) && $kalenderPendidikan instanceof KalenderPendidikan)) { continue; } } $mesinKehadiran = $em->getRepository('LanggasSisdikBundle:MesinKehadiran')->findBy(['sekolah' => $sekolah, 'aktif' => true]); foreach ($perulangan as $key => $value) { $workload['perulangan'] = $key; $logDirectory = $this->getContainer()->get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . 'fingerprintlogs' . DIRECTORY_SEPARATOR . $sekolah->getId() . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $waktuSekarang->format('Y-m-d'); if (!is_dir($logDirectory)) { continue; } foreach ($mesinKehadiran as $mesin) { if (!(is_object($mesin) && $mesin instanceof MesinKehadiran)) { continue; } if ($mesin->getAlamatIp() == '') { continue; } $logFiles = []; exec("cd {$logDirectory} && ls -1t {$mesin->getAlamatIp()}*", $logFiles); $logFile = ''; foreach ($logFiles as $logFile) { if ($logFile == '') { continue; } if (!$input->getOption('paksa')) { $prosesLog = $em->createQueryBuilder()->select('COUNT(prosesLog.id)')->from('LanggasSisdikBundle:ProsesLogKehadiran', 'prosesLog')->where('prosesLog.sekolah = :sekolah')->andWhere('prosesLog.namaFile = :namaFile')->setParameter('sekolah', $sekolah)->setParameter('namaFile', $logFile)->getQuery()->getSingleScalarResult(); if ($prosesLog > 0) { continue; } } $workload['log_file'] = $logDirectory . DIRECTORY_SEPARATOR . $logFile; /* @var $logger Logger */ $logger = $this->getContainer()->get('monolog.logger.attendance'); $gearman = new \GearmanClient(); $gearman->addServer(); $jobFunction = "LanggasSisdikBundleWorkerPembaruanKehadiranWorker~pembaruan"; $proses = new ProsesLogKehadiran(); $proses->setAwalProses($waktuSekarang); $proses->setNamaFile($logFile); $proses->setSekolah($sekolah); $proses->setStatusAntrian('a-masuk-antrian'); $proses->setPrioritas($input->getOption('prioritas')); $em->persist($proses); $em->flush(); $workload['proses_log'] = $proses->getId(); switch ($input->getOption('prioritas')) { case "tinggi": $gearman->doHighBackground($jobFunction, serialize($workload)); $logger->addInfo($sekolah->getId() . ' | ' . $sekolah->getNama() . ' | kehadiran | prioritas-tinggi | ' . $workload['log_file']); break; case "normal": $gearman->doBackground($jobFunction, serialize($workload)); $logger->addInfo($sekolah->getId() . ' | ' . $sekolah->getNama() . ' | kehadiran | prioritas-normal | ' . $workload['log_file']); break; case "rendah": $gearman->doLowBackground($jobFunction, serialize($workload)); $logger->addInfo($sekolah->getId() . ' | ' . $sekolah->getNama() . ' | kehadiran | prioritas-rendah | ' . $workload['log_file']); break; } } } } } }
public function doHighBackground($task, $data) { return $this->_gearmanClient->doHighBackground($task, $data); }
* * We'll use the same script as both the client and the worker for * convenience. */ // Create our worker $worker = new \GearmanWorker(); $worker->addServer('192.168.133.72', 4730); // Create our client $client = new \GearmanClient(); $client->addServer('192.168.133.72', 4730); // Service "foo" jobs by displaying "foo" $worker->addFunction('foo', function (\GearmanJob $job) { echo "foo\n"; }); // Service "bar" jobs by displaying "bar" $worker->addFunction('bar', function (\GearmanJob $job) { echo "bar\n"; }); // Add 25 LOW priority "foo" jobs to the queue. for ($i = 0; $i < 25; $i++) { $client->doLowBackground('foo', '{}'); } // Add 1 HIGH priority "bar" job to the queue. $client->doHighBackground('bar', '{}'); /** * Running ->work() will service the pending jobs. * * What would you expect the output order to be? */ while ($worker->work()) { }
/** * This script demonstrates how to work around Gearman's unintuitive behavior * when it comes to servicing jobs of different priorities. * * We'll use the same script as both the client and the worker for * convenience. */ // Create our worker $worker = new \GearmanWorker(); $worker->addServer('192.168.133.72', 4730); // Create our client $client = new \GearmanClient(); $client->addServer('192.168.133.72', 4730); // Use the same "Function" (i.e. queue) name and distinguish jobs by workload. $worker->addFunction('foobar', function (\GearmanJob $job) { $workload = json_decode($job->workload(), true); echo $workload['function'] . "\n"; }); // Add 25 LOW priority "foo" jobs to the queue. for ($i = 0; $i < 25; $i++) { $client->doLowBackground('foobar', '{"function": "foo"}'); } // Add 1 HIGH priority "bar" job to the queue. $client->doHighBackground('foobar', '{"function": "bar"}'); /** * Running ->work() will service the pending jobs. * * Notice how the HIGH priority job is serviced *FIRST* this time. */ while ($worker->work()) { }
public function doHighBackground($function_name, $workload, $unique = null) { $function_name = $this->_processFunctionName($function_name); return parent::doHighBackground($function_name, $workload, $unique); }
function changeTheme($data) { // Validate id $id = (int) $data['template']; $username = $_SESSION['gh']['username']; $r = $this->db->query("select * from templates where id='{$id}'"); if ($r->num_rows < 1) { $_SESSION['error'] = 'Invalid template selected.'; return false; } $row = $r->fetch_array(MYSQLI_ASSOC); $workload = json_encode(array('token' => $this->access_token, 'email' => $_SESSION['gh']['email'], 'template_id' => $id, 'template_path' => $row['path'], 'repo' => $username . '.github.io', 'user_id' => $_SESSION['gh']['id'], 'username' => $username, 'post_data' => $data)); $client = new GearmanClient(); $client->addServer(); $client->doHighBackground("change_theme", $workload); return true; }