Laravel background jobs & commands monitoring with Inspector

Valerio Barbera

Have you ever desired to see your code running, instead of being able to just imagine it?

Hi, I’m Valerio software engineer from Italy and CTO at Inspector.

As product owner I learned on my skin how an application issue can be so hard to fix, creating a negative impact on the users experience, or block new potential customers during on-boarding.

In this article I’ll show you how to turn on the light in the dark side of your application: “background Jobs and scheduled Artisan commands execution”.

Laravel background jobs: How it works

Laravel provide a great architecture for queued Jobs and scheduled cron tasks. I myself use them a lot. They allow us to execute some piece of code asynchronously in the background out of a classic HTTP request cycle.

Often they are a cause of concern for developers because their execution is not related to direct user interaction. They can go wrong for days and there would be no way of knowing it until we manually check their results typically inside logs.

If something goes wrong during an HTTP request it will appear red bubbles or messages that inform user immediately of the problem. It’s quite easy to discover relevant errors before release the software in production by automated tests, or using the application yourself.

If a queued Jobs or a scheduled Artisan command fails he will do it silently, without anyone noticing.

The tweet below has caught my attention:

I understand this concern and live it every day trying to do the best I can to avoid unpredictable errors or bottlenecks.

We make software; automation is in our DNA, but here is the trap. We are always focused on how to automate our customers’ work and we forget that we can do the same (or even better) with our own.

Inspector is a composer package to add real-time monitoring in Laravel applications, it’s very easy to install and use, and it takes just two minutes to get started.

Let me show you how it works.

Installing Inspector

Run the composer command below in your terminal:

composer require inspector-apm/inspector-laravel

Configure the API key

Get a fresh API key by signing up for Inspector (https://app.inspector.dev/register) and creating a new application, it takes just a few seconds.

Select and copy the API key:

Put the API key in your environment file:

INSPECTOR_API_KEY=xxxxxxxxxxxxxxxxxx

Laravel background process

By default Inspector will monitor all background tasks:

  • Queued Jobs
  • Scheduled Artisan commands
  • Unhandled Exceptions

You can also monitor your application when it is executed due to an incoming HTTP request. You can read more about HTTP monitoring in the official documentation: https://app.inspector.dev/docs/3.x/getting-started/installation#requests

Instantly you will see transaction streams in your project’s dashboard:

and for each transaction you can monitor what your application is executing in real-time:

Laravel custom analytics

Thanks to Inspector you are able to put everything you want in your timeline getting a real-time feedback about the execution of a code block inside your Job or in an Artisan command.

Suppose to run an external http request in your code that by default is not present in the timeline. Use Inspector facade:

$segment = \Inspector::startSegment('http');
$guzzleClient->get('https://www.example.com');
$segment->end();

This will produce a new segment in the timeline and now you can understand what your code is executing and its performance in real time:

Exceptions Alerting

By default, every unhandled exception fired in your Laravel app will be reported automatically to be sure you’re alerted for unpredictable errors in real time.

I wish that every change I make to my code could be perfect. But the reality is, this is not always the case. Some errors appear immediately after an update, while others pop up unpredictably.

However, Inspector automates the detection of unknown issues especially in background tasks where it’s even more tricky to investigate, so I no longer need to manually check the status of my apps continuously or wait reports directly from users.

Inspector allows you to report an exception manually if you want be aware of it:

try {
    $segment = Inspector::startSegment('http');    
    $guzzleClient->get('https://www.example.com');

} catch (GuzzleException $exception) {
    Inspector::reportException($exception);
} finally {
    $segment->end();
}

If something goes wrong in your code you will be alerted in real time in your inbox and the exception will be monitored for all subsequent occurrences:

Conclusion

When background Jobs or scheduled commands are involved getting a true picture of what’s happening can require hours or, based on my experience, even days. Inspector can make a huge difference in terms of efficiency and productivity.

By delegating bug discovery to an automatic monitoring tool, allows you to solve 90% of the problems in the half the time, before users even know about them.

Related Posts

NodeJs Real-Time Execution Monitoring with Inspector

Have you ever desired to watch your code running, instead of just imagining it? Hi, I’m Valerio software engineer from Italy and CTO at Inspector. As product owner I learned on my skin how an application issue can be so hard to fix, creating a negative impact on the users experience, or block new potential

How to accelerate application performance with smart SQL queries.

Why so many performance issues are caused by the database? We often forget that each request is not independent of other requests. If one request is slow, it’s unlikely to affect the others… right? Database is a shared resource used by all processes that runs in your application. Even just one poorly designed access can

Application monitoring tools – Why, when, what!

In this article I reported my experience working on large applications, and how monitoring cames in my journey when I started producing software designed to solve business critical problems. Hi, I’m Valerio, software engineer from Italy and CTO at Inspector. Solve customers critical problems can generate great business opportunities, but in these situations you need

Stop losing customers and money
due to technical problems in your applications

Monitor performance of your code execution in real-time and identify bugs and bottlenecks before your users do.
Real-Time Application Monitoring Platform.
2020 © Inspector S.R.L - VAT: 09552901218