Idle thoughts and technical knots.

Setup Laravel queue on shared hosting

We specialise in bespoke web and mobile development at Papertank, and we’re proud to use Laravel as our PHP framework of choice on web and API projects.

When it comes to hosting a Laravel application, installing on a shared host is perfectly possible, although we normally use a more powerful VPS server for more flexibility and control. We’ve previously blogged about setting up installing Laravel on shared hosting on papertank.co.uk. If you do go down the shared hosting route, you’ll want to make sure you are using a decent web hosting company. This will save a lot of headache and make deploying custom apps much easier. We recommend Cloud Above in Cornwall for great value and support.

Modern web apps use queues for background job handling of intensive tasks, allow your API or front-end app to return data, redirect the user, or perform other actions without waiting for the ‘job’ to finish. Laravel’s driver based queue system makes this easy to set up with Beanstalkd, Amazon SQS or Redis. Unfortunately shared hosting makes using these services difficult or impossible, since your host will often forbid installing software or logging in as root.

Laravel 5’s new database driver fills in for other tools, storing the jobs on a database table – https://laravel.com/docs/5.2/queues. The next step is running or listening for jobs using the command line ‘artisan’ tool, which can be a little tricky on shared hosting without process monitors. To get around this, you can use a cron job which can usually be setup in cPanel on shared hosting, however cron jobs can only be run once per minute which isn’t ideal for high traffic websites.

As a workaround, we recently customised a Laravel web app using the built in Task Scheduler. By setting up the server’s cron to run the schedule:run command every minute, our application spawns off a new queue:listen process and checks to see if it needs restarted every 5 minutes.

/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
	$path = base_path();
	$schedule->call(function() use($path) {
		if (file_exists($path . '/queue.pid')) {
			$pid = file_get_contents($path . '/queue.pid');
			$result = exec("ps -p $pid --no-heading | awk '{print $1}'");
			$run = $result == '' ? true : false;
		} else {
			$run = true;
		}
		if($run) {
			$command = '/usr/bin/php -c ' . $path .'/php.ini ' . $path . '/artisan queue:listen --tries=3 > /dev/null & echo $!';
			$number = exec($command);
			file_put_contents($path . '/queue.pid', $number);
		}
	})->name('monitor_queue_listener')->everyFiveMinutes();
}

The full code for this setup is on Gist at https://gist.github.com/davidrushton/b7229df4c73372402fc1

If you’re looking for more help with Laravel, you should check out the following websites which we subscribe to and read regularly.

Of course, if you’re looking for a Glasgow based company with experience building advanced web apps with Laravel then please get in touch

Glasgow Life iPad Apps

This year has been a big one for us in terms of App Development. Not only have we been producing a variety of our own projects (Trivia Night and Sharpen) but we’ve had the amazing opportunity to work with some really exciting institutions.

Glasgow Life
We were fortunate enough to get to work with a selection of Glasgow’s finest museums this year, producing both in-house exhibition apps and appstore releases.

Century of Style
This beautiful exhibition of 19th-century clothing comes from Glasgow Museums’ collection of European costume. The app allows you to delve into details of each item and discover its history in greater detail, as well as zoom in on some of the finer details using high definition photography.

Century of Style – App Store Release

Scotland Street
Scotland street has always been a hub of information for the history of local schools and those that attended, but a lot of the information has been difficult to obtain.
A new push has been made to digitise a lot of the old records and photos, with a custom app being displayed on iPads throughout the museum to help suers interact with it.

Visit Scotland Street Museum for a hands on experience with the app.

Archaeology Companion
Kelvingrove Museum has always been home to a hugely impressive collection of archaeological treasures, ranging from stone age to medieval periods.

In order to bring their new exhibition to life, the museum commissioned us to produce an iPad app that would allow visitors to learn and discover interesting details about the individual items on display. Along with the detailed records, the app also allows users to zoom in on high definition imagery and even pinch, zoom and ‘spin’ detailed videos of certain objects for a closer look.

Visit the museum’s Study Centre upstairs to interact with the app in person or download free from the AppStore.

An Archaeology Companion – App Store Release