Support the ongoing development of Laravel.io →
posted 11 years ago
Queues

Hello,

I am trying to implement a polling queue called MasterQueue that every 15 minutes this MasterQueue will poll the database to check for jobs that have a status of Failing, Passing, or Uninitialized. Then, foreach job that it picks up, it checks to see if that job is scheduled to execute today. If that job is scheduled to execute today it checks to make sure that that it is set to execute for that time. I give the user the ability to choose between 01 - 12 for the hours and 00, 15, 30, 45 for the minutes. If it is time for the job to run (all conditions are met) then the QueueMaster : 1) Takes the Class Name of the job and pushes it into another queue 2) Calculates the amount of time until the next quarter hour (00, 15, 30, 45) and then with that calculated time I put the time into Queue::later('nextCalculatedTime', 'MasterQueue', array());

My questions are as follows:

  1. Is it possible to use ~~~Queue::later('time', 'class', 'data');~~~ inside of a queue? Specifically the queue is pushing itself into the queue line.

  2. Is there a specific set time limit to the Queue::later() method?

This is my code if any one is interested in possibly spotting an error in my logic :

/**
	 * Will continue to poll for new jobs that an administrator / super user has created
	 * These jobs are stored in the automaticProcesses table in the database.
	 * If a processes is polled from the database with the status of passing, failing, uninitialized 
	 * and it is time for it to run the fire method will Push that job into a seperate queue to be completed.
	 * Keep in mind *EVERY* job will have a time to run, and the repitition to run. Whether it be
	 * every day at 12:00 PM or every Saturday at 9:45 AM
	 * All minute intervals are in 15 Minute time slices
	 *
	 * @return void
	 */
	public function fire($job, $data)
	{
		if (! $this->checkMaster())
		{
			$job->delete();
		}

		//Get all jobs where status is uninitialized, passing or failing and name is not Master Process
		$jobs = \DB::table('automaticProcesses')
				->select('userID', 'processName', 'executionTime', 'days', 'scriptLocation', 'repeat')
				->where('processName', '!=', 'Master Process')
				->whereIn('status', array('Uninitialized', 'Passing', 'Failing'))
				->get();

		foreach ($jobs as $v)
		{
			// If the job is meant to run today 
			if (strpos($v->days, date('D')) !== false)
			{
				// If the HH:MM (AM/PM) of now == the HH::MM (AM/PM) stored in the database
				if (date('h:i A') == $v->executionTime)
				{
					// Then push the job into the queue 
					// $v->scriptLocation is just the class name. Nothing more.
					\Queue::push($v->scriptLocation, array($v->userID, $v->processName, $v->repeat));					
				}
			}
		}
		
		\Queue::later($this->nextInterval(), 'QueueMaster', array());
		$this->updateCount(1, 'Master Process');
		$job->delete();
	}

now the job in particular that I am pushing is just a very basic job that texts me (for now) to make sure that it is working. (it isn't because the job never runs)

/**
	 * Reads the files and uploads the banner data into scholarship interface database.
	 * 
	 * @return void 
	 */
	public function fire($job, $data)
	{	
		$sms = new \Text($loggedIn = '');
		$sms->textRix('It works!!');
		$this->updateCount($data[0], $data[1]);

		// Check if the boolean in DB called repeat is set to 1
		if ($data[2] == 1)
		{	
			// If its set to one, then we must give the job a passing sttus and let it run again when it is needed to run
			$this->passJob($data[0], $data[1]);
		}

		else
		{
			// If it is 0 then just finish the job and we are done - never runs agains
			$this->finishJob($data[0], $data[1]);
		}

		$job->delete();	
	}

Thanks in advance. -Rixhers

Last updated 3 years ago.
0

Also the

$this->nextInterval()

method just returns an integer with the amount of time in seconds until the next quarter hour.

/**
	 * The amount of seconds until the next interval currently set to each 15 minutes
	 * This will round up so if it is is 12:05 it will round the next interval to 12:15
	 * if it is 12:32 it will be 12:45 and so on.
	 *
	 * @return int
	 */
	private function nextInterval()
	{
		$now    = strtotime(date('h:i A'));
		$next15 = ceil ($now / (15 * 60)) * (15 * 60);
		return (int) $next15 - $now;
	}
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.