Laravel 5: Background processes checklist.
Laravel project involves a bit more than PHP code.
Here is the list, what you need to remember, when setting your app.
Some software must (should) be added to Windows environment Variable Path.
Read more here.
Also, see this sample of ‘variable value’ in this text file.
Setup Cron, or for Windows (e.g. for dev) equivalent pseudo-Cron using task scheduler.
More here.
Difference is in: “Go to TAB: Actions, and click ‘New’ button” #2.
Each Laravel project needs separate cron setup in Windows task.
E.g. in example (link here above) I have: U:\www\la4\artisan.
Above path will make cron run only for project located in /la4 folder.
So, each folder/project needs own cron to work.
Above does not apply to Redis.
Explanation above apply to Windows dev setup.
Also see screenshots with how to setup Windows task: zip-file
You need to install and start Redis server – if you are using it.
And since Redis can help in queues, cache, broadcasting and as database – it is worthwhile to install it.
For Linux, see here.
For Windows, see here and scroll down to: “setup Windows task”.
It uses same methodology as Cron. Anyway, all links are there.
Dependencies:
- ioredis
- predis
Also see screenshots with how to setup Windows task: zip-file
If you are using queues other than ‘sync’, e.g. QUEUE_DRIVER=redis, then you need php artisan queue:listen command running. Eventually, you may use something like php artisan queue:work –sleep=3 –tries=3 .
For quick testing, you can run it in your console/command prompt, but to have it run all the time – also for dev, it is better to have daemon on Linux/Unix (OSX), or Task on Windows.
Linux setup – see here.
Windows setup – see here.
There is some difference thou to “Windows setup” description above:
Difference is in: “Go to TAB: Actions, and click ‘New’ button” #2.
Each Laravel project needs separate cron setup in Windows task.
E.g. in example (link here above) I have: U:\www\la4\artisan.
Above path will make cron run only for project located in /la4 folder.
So, each folder/project needs own cron to work.
Above does not apply to Redis.
Explanation above apply to Windows dev setup.
Queues daemon should be run and overseen by supervisor script.
Also see screenshots with how to setup Windows task: zip-file
Nice read is here. Thanks Gergely.
Also read here.
Dependencies:
- socket.io
- express (possibly)
- node.js
This way will not react to any process dropped. Supervisor will reinstate such process.
Put inside of method schedule() of :
1 2 3 |
app\Console\Kernel.php |
… this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protected function schedule(Schedule $schedule) { $schedule->exec('node socket.js') //do custom cron - use: min/hour/day/month/day-of-week/year (******) //every 19th of January at 20:20 ->cron('20 20 19 1 * *'); //or, daily at 20:03 //->dailyAt('20:03'); //or, every month on 19th, at 20:00 //->monthlyOn(19, '20:00'); } |
Here is socket.js script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var server = require('http').Server(); var io = require('socket.io')(server); var Redis = require('ioredis'); var redis = new Redis(); //redis.subscribe('test-channel'); redis.psubscribe('*'); //multiple channels //redis.on('message', function(channel, message) { redis.on('pmessage', function(subscribed, channel, message) { //multiple channels message = JSON.parse(message); io.emit(channel + ':' + message.event, message.data); }); server.listen(3000); |
Cron must be on for this to work.
First we need Forever script.
It is somewhat like Supervisor, just for node scripts only:
1 2 3 |
npm install forever --save -g |
Now again we use schedule, so cron must be up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
protected function schedule(Schedule $schedule) { $schedule->exec('forever start socket.js') //here is the difference //do custom cron - use: min/hour/day/month/day-of-week/year (******) //every 19th of January at 20:20 ->cron('57 20 19 1 * *'); //daily at 20:03 //->dailyAt('20:03'); //every month on 19th, at 20:00 //->monthlyOn(19, '20:00'); } |
If you do not like Forever, but do not want to go for Supervisor, try PM2.
Use Supervisor.
It will work on Linux server, not on Windows.
For windows, see above.
It is a script written in Python. It allows to control also other processes than node.js.
More here.
Sample Supervisor config might look like this (it should be placed at /etc/supervisor/conf.d/myapi.conf):
1 2 3 4 5 6 7 8 9 10 |
[program:my-api] command=node /home/myuser/myapi/app.js autostart=true autorestart=true environment=NODE_ENV=production stderr_logfile=/var/log/myapi.err.log stdout_logfile=/var/log/myapi.out.log user=myuser |
Make sure all settings in .env file are taken care of, like:
- APP_DEBUG
- APP_LOG_LEVEL
- and the rest
Run:
1 2 3 4 |
node -v //check if you have node npm -v //check if you have npm |
and
1 2 3 |
npm install //install dependencies |
Also read this.
.env constant be set properly:
1 2 3 |
APP_URL=http://www.yourdomain.com //also applies to localhosted dev domains |
1 2 3 |
/tests/Browser |
1 2 3 |
composer require laravel/dusk |
step 2: service provider
Register the provider within the register() method of your app\Providers\AppServiceProvider, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
use Laravel\Dusk\DuskServiceProvider; /** * Register any application services. * * @return void */ public function register() { /** * Laravel 5.4 Dusk browser testing */ if ($this->app->environment('local', 'testing')) { $this->app->register(DuskServiceProvider::class); } } |
install dusk
Next, run the dusk:install Artisan command:
1 2 3 |
php artisan dusk:install |
More about Dusk – try Laravel docs and articles on this page.
Up to Laravel 5.4 browser testing was done using Integrated Package.
Use link just above for detailed info.
Laracast videos (Intermediate series: “2015-03-23-Intuitive Integration Testing“) are here:
- video: Introduction and General Usage
- video: Laravel and Integrated
- video: Database Transactions
- video: Example Workflow and Custom Methods
- video: Selenium
- video: Testing APIs
1 2 3 |
composer require laracasts/integrated --dev |
More about installation etc. – here.
note how slashes go!
command prompt expects backslash, not forward slash (like in most
tutorials done on Linux, or Apple, or VM (like HomeStead)