Monthly Archives: November 2018

Lumen Microservice Architecture

I posted a blog on implementing Microservice Architecture. Here is the link. This blog is second part of it. In first post I just explained about Microservices and proposed architecture. In this post I will give practical example of Lumen Microservice.

Microservice Using Laravel

Lumen Microservice

Lumen Microservice

Lumen Microservice

You can either install Lumen in public folder of main Laravel app or you can have different installation all together.

composer global require “laravel/lumen-installer”

After it’s installed, now lets create an API.

$router->post(‘getDetails’,’APIController@getDetails’);
Now in a controller define the API.
public function getDetails(){
      return response()->json(array(
           “key1″=>”value1”,
           “key2″=>”value2”,
           “key3″=>”value3”,
           “key4″=>”value4”,
           “key5″=>”value5”
      ));
}
Now we can call these API either through our JavaScript app as Ajax request or we can call it in Laravel app using GuzzleHttp

Benefits of GuzzleHttp as mentioned on their website

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc…
  • Can send both synchronous and asynchronous requests using the same interface.
  • Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops.
  • Middleware system allows you to augment and compose client behavior.

Install GuzzleHttp

Install it using Composer.

composer require guzzlehttp/guzzle

Once installed we can use in controller by name space.

use GuzzleHttp\Client;
Now call API using this client.

$client = new Client();

$response = $client->post(‘/lumenapi/public/getDetails’, [
‘form_params’ => []
]);

$data = json_decode($response->getBody()->getContents(), true);

Here in above code if you want to pass any param in POST request, you can do so inside form_params.
Above code is for the synchronous request. We can also have async request.
$request = new Request(‘GET’,’/lumenapi/public/getDetails’);
$client->sendAsync($request)->then(function ($response) {
       $data = json_decode($response->getBody()->getContents(), true);
});
So this way we can build Lumen Microservice and consume it with Laravel app.

Laravel Cron Job Monitoring

When we build applications using Laravel, we also have lots of Laravel cron jobs. Once the application is deployed on production server, it’s mandatory to monitor cron jobs. Many time it happens that due to certain issues crons are not executed. That may lead to lot of problems with your customers. As a server admin we can always login to server and check the logs. But what if we have a very simple page where we can see this logs.

Laravel Cron Jobs

Laravel Cron Jobs

Here in this blog I am going to explain a very simple approach to monitoring cron jobs. First of all for each cron jobs we will build a log file.

Build Laravel Cron Log File

We will use Laravel File class to create log files. First of all add to controller.

use File;

Add a variable to have counts of processed records with cron.

$count = 0;

Now inside foreach loop or any other loop you have increase this count.

foreach($records as $record){

$count++;

}

Once the cron execution logic is over build log file.

//Preparing log files on server.
$data = json_encode([“description”=>”Cron Execution Report”,”count”=>$count,”date”=>date(‘Y-m-d H:i:s’)]);
$fileName = ‘cronExecutionReport.json’;
File::put(public_path(‘/logs/’.$fileName),$data);

This way add logs files everywhere in your cron.

Read The Log File and Display It

Now that we have all the log files generated, we just have to read it and display it. Here is the logic for the same.

foreach (glob(public_path(“logs/*.json”)) as $file) {

$json = json_decode(file_get_contents($file), true);

echo ‘<span style=”font-weight:bold;font-size:25px”>’.$json[‘description’].'<br/>’;

print( ‘<span style=”font-weight:normal;font-size:20px”>Total Count : ‘.$json[‘count’].'<br/>’);

print( ‘<span style=”font-weight:normal;font-size:20px”>Last Processed Date : ‘.$json[‘date’].'<br/>’);

}
echo ‘<script type=”text/javascript”>setInterval(function(){window.location.reload()},10000)</script>’;

Add the above code in your controller function so that it will display following output and it will also refresh the page after 10 seconds to get the latest logs from server.

Cron Execution Report
Total Count : 10
Last Processed Date : 2018-11-09 10:56:01

So this way you can build a very simple Laravel Cron Job monitoring page.