Welcome

Netmechanics is a small code and webhosting shop based out of Wanaka, New Zealand. We enjoy working in PHP, mainly with the Laravel framework, and with Javascript/Jquery on a Linux/Nginx/Mysql server stack.

~ Gwilym Griffith-Jones (principle)

Moving stuff around the web with Rsync

After marvelling at the wonders of FTP, and the ability to move large and numerous files around the internet, you quickly realise there might be a better way. One which syncs files quickly and efficiently between servers and is a little more intelligent. Well there is - Rsync and it is an excellent tool in the web developers tool box, on Linux of course.

Now, I now there are even better tools than rsync for deploying, Git for instance, that harness the power of version control. And I use those too. It's just for moving a large Wordpress images folder from one server to another, or a large MySQL dump, I've found Rsync to be incredibly useful.

To move from my local up to a certificate SSH enabled server, excluding node and git folders:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --exclude 'node_modules' --exclude '.git' ./ ssh_user_name_here@ip_address_here:/srv/www/etc/etc/here/

:: Created: 11th Oct '14 ~ Last updated: 29th Oct '14

Useful Mysql/SQL commands

Here are some of my favourite MySql commands:

This one takes the value of col2, splits it and inserts the second character into col1:

UPDATE table_name SET col_1 = SUBSTR(col_2, 2);

This is another handy one which inserts from one table into another:

INSERT INTO table2
column1, column2..
SELECT column1, column2, ..
FROM table1;

:: Created: 20th Oct '14 ~ Last updated: 20th Oct '14

Beanstalk with Laravel Queues

Queues in Laravel are really handy if you want to fire a lot of processes and are hitting the limit of what you can reasonably ask PHP to do.

http://fideloper.com/ubuntu-beanstalkd-and-laravel4

  • Requires Beanstalk to be installed and adding pda/pheanstalk to composer
  • I couldn't get Supervisord managing Beanstalk on Ubuntu 14.04, or track down why it wasn't working
  • I'm planing on trying Upstart when I have time

This starts beanstalk:

sudo service beanstalkd start

This command (from your Laravel dir) starts to listen with an extended timeout:

php artisan queue:listen --timeout=600

This one just processes the first in queue:

php artisan queue:work

This is what I'm going to try:

## Instead of supervisord, why not use Ubuntu's native upstart? Just add the following to /etc/init/myqueue.conf:

description "work queue"
start on filesystem
stop on runlevel [!2345]
respawn
respawn limit 5 2
script
exec /usr/bin/php /path/to/laravel/artisan queue:listen
end script

## Then start the queue: "start myqueue". That's it, it will be automatically monitored and restarted in case of crash. No need for external daemons.
## Path to artisan i.e /srv/www.www.website.com/artisan/ ??

:: Created: 22nd Oct '14 ~ Last updated: 22nd Oct '14

Laravel lists method for select lists

I often use the Laravel ->lists() method to build select lists for forms.

Usually I do something simple like the following to build a list of model User email addresses with the User key:

User::lists('email', 'id);

It's handy to be able to prepend an '--all--' option to the list, I've been just adding to the array

 $list = User::lists('email', 'id);
 $list = array('--all--', 0) + $list; 

Another handy thing to do is concatenate two field together for the label part of the option list:

User::select('id',  DB::raw('CONCAT(firstname, " ", lastname) as thename'))->orderBy('lastname', 'asc')->lists('id', 'thename');

I normally then feed this into a Select2 select that I build with a Form helper.

:: Created: 23rd Oct '14 ~ Last updated: 23rd Oct '14

Seeding and insert speed in Laravel

When you start adding rows in the hundreds or thousands with Eloquent in Laravel you quickly realise that there is a difference in inserting line by line or in one big query. It's worth remembering that some versions of mySQL/SQL limit a single insert to 1,000 so chunking an array into inserts of < 1000 is the way to go.

This blog post here has a good summary - http://bensmith.io/speeding-up-laravel-seeders

:: Created: 31st Oct '14 ~ Last updated: 31st Oct '14