Laravel Jobs and Queues – Configuring, Sending Mail, Dispatching Jobs

Laravel Jobs and Queues – Configuring, Sending Mail, Dispatching Jobs

https://ift.tt/2VxbWgU

It has been a lot of time since I started using Laravel Queues. During earlier days back than learning complex technologies on own was not easy and I somehow managed guts to learn them and apply in real-time projects.

Today I’m happy that I took that decision. While learning Laravel Framework I got introduced to a lot of intermediate and high-level programming concepts and one concept or say feature on this framework caught my attention was Queues because of its ability to run jobs or tasks in the background.

From the day I learnt queues in started to use it in each of the projects I do in laravel.

Table of Contents

What are Laravel Queues?

Consider you have a heavy processing task to be performed by application this may be generating large data for reporting, blasting emails for a large number of users or such notifying people when their task gets completed.

These are some of the examples and as they are time-consuming you cannot execute these task is normal manner such are on button click or page submit. So to properly handling these task laravel provides queue functionality where tasks data is storing in a database and then the queue will automatically detect any jobs and execute them in background.

Create a Fresh New Laravel Project

Laravel provides two different options for creating a fresh project.

Before you execute below command you must navigate to your directory where you would like to create this project and open the terminal from that particular path.

Using Laravel Installer

This installer is downloaded through composer. Before running this command composer must be installed on your computer.

composer global require laravel/installer
 

Then you can create a new project by typing below command.

laravel new your_project_name
 

Directly creating a project through composer command

This is another method and I always go through this method because it doesn’t need the additional installer to be installed other than a composer and also I can specify project version at the end.

composer create-project --prefer-dist laravel/laravel your_project_name "6.*"
 

At the end 6.* specifies which version you would like to install. You can visit Laravel versions and support policy page for more information.

Note

Directly creating project second option will take a few minutes. So don’t cancel the process.

Database Connections

In the root directory of the freshly created project, theirs an .env file which contains all the environment setting for the project.
There is an option for adding database connection which username and password.

DB_CONNECTION=mysql
 DB_HOST=127.0.0.1
 DB_PORT=3306
 DB_DATABASE=laravel_queues
 DB_USERNAME=root
 DB_PASSWORD=
 

laravel_queues is our database name and then goto PHPMyAdmin and create a database of laravel_queues name.

Note

For demonstration purpose we are using MySql are database. You more robust performance laravel also provides drivers for Redis, Amazon SQS, Beanstalkd etc for more information you can visit drivers and prerequisites of laravel queues.

 

Queue Configuration and Settings

Since we are telling laravel to use our database as queue driver there is just a simple step to do.

That is to go to .env file and replace below code.

#Replace this line.
 QUEUE_CONNECTION=sync 
 
 #With this line.
 QUEUE_CONNECTION=database
 

Note

You can also take a look at queue.php inside config a directory which contains an array of all available drivers.

 

Database Migration

Database migration takes place by creating a migration class inside the folder database/migrations. It contains the schema description of the table and can be migrated to the database.
But before start migration, you must inform laravel to create a migration class for queue table for this use below command.

php artisan queue:table
 

A migration file is created inside database/migrations folder.

Schema::create('jobs', function (Blueprint $table) {
 $table->bigIncrements('id');
 $table->string('queue')->index();
 $table->longText('payload');
 $table->unsignedTinyInteger('attempts');
 $table->unsignedInteger('reserved_at')->nullable();
 $table->unsignedInteger('available_at');
 $table->unsignedInteger('created_at');
 });
 

Now all these migrations must be migrated to the database for that run below command.

php artisan migrate
 

Laravel php artisan migrate command output

By switching to PHPMyAdmin you can see that tables are created in the database.

Laravel database after running migrations

For testing, purpose let us also create two new tables they are countries_census, states_census.

Note

countries_census the table holds the total population of the country.
states_census the table holds the total population of each state which is then mapped to the country table through country_id column.

php artisan make:migration create_countries_census_table --create=countries_census
 
 php artisan make:migration create_states_census_table --create=states_census
 

database/migrations/2019_08_19_000000_create_countries_census_table.php.

<?php
 
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
 class create_countries_census_table extends Migration
 {
 /**
 * Run the migrations.
 * 
 * @return void
 */
 public function up()
 {
 Schema::create('countries_census', function (Blueprint $table) {
 $table->bigIncrements('country_id');
 $table->text('country_name');
 $table->unsignedInteger('total_population');
 $table->timestamp('updated_at')->nullable();
 });
 }
 
 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
 Schema::dropIfExists('countries_census');
 }
 }
 
 

database/migrations/2019_08_19_000000_create_states_census_table.php.

<?php
 
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
 class create_states_census_table extends Migration
 {
 /**
 * Run the migrations.
 * 
 * @return void
 */
 public function up()
 {
 Schema::create('states_census', function (Blueprint $table) {
 $table->bigIncrements('state_id');
 $table->text('state_name');
 $table->unsignedInteger('country_id');
 $table->unsignedInteger('state_population');
 });
 }
 
 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
 Schema::dropIfExists('states_census');
 }
 }
 
php artisan migrate
 

Laravel new tables countries census and states_census created through migration

Creating models for countries_census and states_census tables.

Laravel models are the representation of the database table which contains tables schematic information as well as one table to another table relationship description.

To create model use below command.

php artisan make:model CountryPopulationModel
 

In app/CountryPopulationModel.php a new model has been created.

<?php
 
 namespace App;
 
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\Log;
 
 class CountryPopulationModel extends Model
 {
 protected $table="countries_census";
 protected $primaryKey = 'country_id';
 protected $fillable = ['country_name', 'total_population'];
 
 public function states()
 {
 return $this->hasMany(CountryStatePopulationModel::class, 'country_id', 'country_id');
 }
 }
 

$table is database table name of than model.
$primaryKey is database table primary.
$fillable is columns of database table except for primary key.

Similarly, let us create a model for states_census.
In app/CountryStatePopulationModel.php.

<?php
 
 namespace App;
 
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\Log;
 
 class CountryStatePopulationModel extends Model
 {
 protected $table="states_census";
 protected $primaryKey = 'state_id';
 protected $fillable = ['country_id', 'state_name', 'state_population'];
 
 public function country()
 {
 return $this->belongsTo(CountryPopulationModel::class, 'country_id', 'country_id');
 }
 }
 

Dummy data is also added into both the tables and before running jobs, these tables look like below.
Laravel countries census table data before running jobs

Creating a controller for handling logic

Command to create a new controller.

php artisan make:controller CountryController
 

app/Http/Controllers/CountryController.php

<?php
 
 namespace App\Http\Controllers;
 
 use App\CountryPopulationModel;
 use App\Jobs\ProcessCountriesPopulation;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 
 class CountryController extends Controller
 {
 public function process(Request $request){
 
 }
 }
 

Routing

routes/web.php

Route::get('countries-census/process', 'CountryController@process');
 

Creating New Job for Queue

Use php artisan the command for creating a new job class.

php artisan make:job ProcessCountriesPopulation
 

ProcessCountriesPopulation class is created inside the application root directory app/Jobs/ProcessCountriesPopulation.php path.
This job class will periodically check for population data for different countries, sum them and update the latest population of the country in countries_census table.

The ProcessCountriesPopulation job class looks like below.

<?php
 
 namespace App\Jobs;
 
 use App\CountryPopulationModel;
 use App\CountryStatePopulationModel;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Support\Facades\Log;
 
 class ProcessCountriesPopulation implements ShouldQueue
 {
 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
 
 /**
 * Create a new job instance.
 *
 * @return void
 */
 public function __construct()
 {
 
 }
 
 /**
 * Execute the job.
 *
 * @return void
 */
 public function handle()
 {
 
 }
 } 
 

Pre-made methods __construct and handle are available.

Note

__construct() the method is used for passing dynamic data to the job.

handle() the method is executed whenever a new job is dispatched. This dispatch event is listened by command queue:work.

<?php
 
 namespace App\Jobs;
 
 use App\CountryPopulationModel;
 use App\CountryStatePopulationModel;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Support\Facades\Log;
 
 class ProcessCountriesPopulation implements ShouldQueue
 {
 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
 protected $country_census;
 
 /**
 * Create a new job instance.
 *
 * @return void
 */
 public function __construct(CountryPopulationModel $country)
 {
 Log::info('Entered Job ProcessCountriesPopulation __constructor method');
 $this->country_census = $country;
 }
 
 /**
 * Execute the job.
 *
 * @return void
 */
 public function handle()
 {
 Log::info('Entered Job ProcessCountriesPopulation handle method');
 $country_states_population_sum = CountryStatePopulationModel::where(["country_id" => $this->country_census["country_id"]])->sum('state_population');
 
 $country = CountryPopulationModel::find($this->country_census["country_id"]); 
 $country->total_population = $country_states_population_sum;
 $country->save();
 
 Log::info('Exited from Job ProcessCountriesPopulation handle method');
 }
 }
 

In the above job, the class constructor method __construct(CountryPopulationModel $country) takes an object which is of type CountryPopulationModel as an argument. This object is then assigned to a class variable $this->country_census.
Log::info logs the message in the log file which is placed in the path laravel_queues/storage/logs/laravel.log.

Note

Background logic is always performed in handle() method of job class this place where the jobs are inserted to the job table.

Dispatching Jobs to Queue

app/Http/Controllers/CountryController.php

<?php
 
 namespace App\Http\Controllers;
 
 use App\CountryPopulationModel;
 use App\Jobs\ProcessCountriesPopulation;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 
 class CountryController extends Controller
 {
 public function process(Request $request){
 Log::info('Entered Job CountryController process method');
 
 $country = CountryPopulationModel::find(101);
 
 ProcessCountriesPopulation::dispatch($country);
 
 Log::info('Exited Job CountryController process method');
 }
 }
 

In ProcessCountriesPopulation::dispatch($country); the static dispatch() method passed $country object to job class constructor.

Running Queue Workers on Development Machine

php artisan queue:work
 

This command will detect any pending jobs is jobs table and executes them one by one.

After going to route countries-census/process in browser. You can see that a new row is already been inserted in jobs table. And below log message appears in laravel_queues/storage/logs/laravel.log a file.

[2020-04-09 11:48:54] local.INFO: Entered Job CountryController process method 
 [2020-04-09 11:48:54] local.INFO: Entered Job ProcessCountriesPopulation __constructor method 
 [2020-04-09 11:48:54] local.INFO: Exited Job CountryController process method 
 [2020-04-09 11:48:55] local.INFO: Entered Job ProcessCountriesPopulation handle method 
 [2020-04-09 11:48:55] local.INFO: Exited from Job ProcessCountriesPopulation handle method 
 

Jobs Table before running queue.

Laravel job table before running queue work command
When the job is processed queue automatically remove the completed job rows.

Population data inside the database table countries_census is also updated.
Laravel country census table after running queue work command

Processing of queued jobs.
laravel processing of queued jobs in command line

Using Laravel Queues for sending mail

Here you’ll see a simple example of sending an email using queues.

For creating new email class.

php artisan make:mail WelcomeEmail
 

In laravel_queues/app/Mail/WelcomeEmail.php

public function build()
 {
 return $this->from('abc@gmail.com')->view('emails.welcome-email');
 }
 

If you want to send email using SMTP then you must specify below credentials in .env file

MAIL_DRIVER=smtp

MAIL_HOST=smtp.googlemail.com

MAIL_PORT=587

MAIL_USERNAME=*****@gmail.com

MAIL_PASSWORD=

MAIL_ENCRYPTION=tls

For sending emails use Mail class and inside send() method pass the name of the email class you would like to send.

Mail::to("to-email@gmail.com")->send(new WelcomeEmail());
 

Running Queue Workers on Production Server

Use cron job option provided by servers and set the frequency to each minute. Which means that the cron job will execute the artisan console command schedule method each minute which is placed at the path laravel_queues/app/Console/Kernel.php.

I have created a personalized script while using executing job command inside console which I’ll be sharing with you.

$schedule->command('queue:restart')->everyFifteenMinutes()->withoutOverlapping();
 
 $schedule->command('queue:work --sleep=3 --tries=3')->everyMinute()->sendOutputTo(storage_path() . '/logs/queue-jobs.log')->withoutOverlapping();
 

First command queue:restart will restart the processing job listeners, every fifteen minutes. The withoutOverlapping method will prevent the same jobs from overlapping with each other.

Laravel has also provided a detailed overview of the Overlapping of tasks.

Caution

Running jobs without overlapping will be draining servers physical memory can also leave server hanged up.

Next command queue:work will listen to new jobs every minute. You can also specify flags which have different purposes.
Flag --sleep will send queue to sleep mode for specified seconds once all jobs are processed.
Flag --tries will attempt to try executing queued for a specified number of times before sending them for failed_jobs.

How to handle failed jobs in Queues?

Queued jobs which were unable to execute even after many attempts are sent to failed_jobs the table. This table consists of information such as connection type, payload, exception information and date-time when a job has failed.

Larael failed queue jobs table

For re-attempting, these failed jobs use below command.

php artisan queue:retry all
 

Command queue:retry all will retrieve a list of all jobs that are failed and will try to execute them. You can also specify which job to executed by specifying its primary key number like php artisan queue:retry 2 this will execute the job with primary key 2.

List failed jobs

To see all the jobs which are failed use php artisan queue:failed command. This will display all the failed jobs in the terminal.

Laravel show table of failed jobs in terminal

Queueing Jobs by Priorities

Queue executes jobs on default occurrence. But you can specify the priority of a job during dispatching stage.

dispatch((new ProcessCountriesPopulation($country))->onQueue('high'));
 

Conclusion

Time has come to say goodbye. Coming to the end of this post you have all the basic and intermediate information and examples of working and queues in the development and production stage. This post was regarding Laravel Jobs and Queues – Configuring, Sending Mail, Dispacting Jobs and were are happy to have taken time to go read this post. Support us by sharing this post which will help us grow and comment if you have any doubts we will reach you soon.

Popular Posts

programming

via Laravel News Links https://ift.tt/2dvygAJ

April 23, 2020 at 09:42AM

The Mandalorian Documentary’s First Trailer Goes Behind the Scenes on Bringing Baby Yoda to Life

The Mandalorian Documentary’s First Trailer Goes Behind the Scenes on Bringing Baby Yoda to Life

https://ift.tt/2VQyecn

Baby Yoda, once again star of the show.
Image: Lucasfilm

Trailer FrenzyA special place to find the newest trailers for movies and TV shows you’re craving.

You guys: Werner Herzog wanted to direct that baby so much. Give him a whole episode!

Disney has dropped the first trailer for Disney Gallery: The Mandalorian, a surprise eight-part documentary series that will launch this Star Wars Day, May 4, on Disney+. Going behind the scenes on the first season of the hit Small Green Child show, the series will provide insight from roundtables featuring the directors, producers, and cast of the show, as well as behind-the-scenes footage from the production.

Which means yes, even more Baby Yoda footage to gawk at.

Does it rob a little of the magic of Baby Yoda to see the puppet in action, all its strings (well, metaphorical ones in the age of modern puppetry) attached for us to see? Maybe, but then you get director Deborah Chow recalling how Werner Herzog—who played the Imperial Remnant known only as “The Client” in the first season—would be directing the puppet itself like it was a real baby during his scenes, and your heart melts all over again.

Disney Gallery: The Mandalorian’s first episode will release on Disney+ May 4, alongside the final episode of Star Wars: The Clone Wars. Meanwhile, here’s a new poster for the documentary:

See, you know it’s behind the scenes because you can see a camera.
Image: Lucasfilm

Because when you’re Star Wars, even your behind-the-scenes footage gets a poster.


For more, make sure you’re following us on our Instagram @io9dotcom.

geeky,Tech

via Gizmodo https://gizmodo.com

April 23, 2020 at 10:42AM

Ruger’s New PC Charger

Ruger’s New PC Charger

https://ift.tt/2KuL4re

In keeping with the current firearms market rage over pistol carbine type guns, Ruger has introduced the innovative new PC Charger. This provides shooters with another exciting option for everyday carry, a truck gun, or for home or personal defense.

The PC Charger is a handheld pistol with an AR type pistol grip and a large magazine well for offhand gripping with a hand stop up front at the muzzle. Though the PC is a handgun, it’s not exactly compact with a threaded 6.5-inch barrel and overall length of 16.5 inches . It is considered a pistol for all legal aspects. The PC’s weight is 5.2 pounds.

Chambered for the street standard 9mm Luger, the PC comes from the factory ready to handle proprietary Ruger magazines, but can be converted with included parts to switch it over to use Glock magazines. This is a handy feature for sure. The Ruger Security 9 magazine holds 17 rounds, enough to start a good shooting round at the range or ward off any threat at home or on the road.

This PC pistol has the capability to take down by locking back the bolt and pushing a recessed lever, allowing the two sections to be twisted until they release and pull apart. One can quickly imagine the utility of being able to take this firearm apart into two subassemblies for easy carry and secure carry or storage.

The PC has a blue finish with the handguard assembly finished in a Type III hard coat anodized finish for tough wear durability and resistance to outdoor elements. The handguard is equipped with M-LOK slots in the 3-6 and 9 o’clock positions. This provides a number of options for adding accessories. The top rail has a built in Picatinny section for mounting a variety of optic options.

The Ruger PC Charger’s chassis is constructed of a glass filled polymer. The rear portion of the frame has an integrated QD cup to allow a quick connection of a QD cup sling. This would permit a single point sling attachment for shoulder carry in a variety of modes. Other features and specifications can be seen at www.ruger.com.

Some may inquire or question as to what the utility is of such a weapon as the PC Charger. That of course is up to the users themselves. Certainly this firearm can fit roles in personal defense, property security, travel security, or just plain and simple shooting fun. Check with your local firearms dealer to see when they expect the PC Charger to be available. It retails at $799.

The post Ruger’s New PC Charger appeared first on AllOutdoor.com.

guns

via All Outdoor https://ift.tt/2yaNKUu

April 23, 2020 at 11:33AM

You Should Set Up Two-Step Verification on Your Nintendo Account Right Now

You Should Set Up Two-Step Verification on Your Nintendo Account Right Now

https://ift.tt/3cy3JOV

If you have a Nintendo account, now’s a good time to make sure that you have two-step verification set up on that account to keep it secure.

Nintendo makes two-step verification optional for its accounts but has recently started a small social media campaign encouraging users to opt in.

This weekend a number of Nintendo Switch owners have posted about third-party logins happening on their accounts, some resulting in the loss of funds, Nintendo Life reports. Nintendo has also recently issued a warning to account holders in Japan citing an increase in credit card fraud through Nintendo accounts.

My own account was accessed by someone in Thailand in late March. Although I luckily was able to catch it and change my password before any damage was done, my secure password clearly wasn’t secure enough, and I’m embarrassed I didn’t have two-step set up prior to that unauthorized login

To set up two-step verification on your account, go to the Nintendo website and then sign into your account.

Afterward, go to “Sign-in and security settings” and then go down to “2-step verification” and click “Edit.”

While some services use your phone number for two-step services, Nintendo only allows two-step verification via an Authenticator app.

Screenshot: E.Price / Nintendo

When you click “2-Step Verification setup” on Nintendo’s site you’ll be sent an email with instructions on how to complete the verification process using Google Authenticator—which should also work on any third-party app you prefer.

As part of that process, you’ll be given some codes to use as backup if you need to access your account and don’t have the app handy. Make sure you store those in a safe place where you’ll be able to find them if need be.

Two-step authentication will make it a tiny bit harder for you to login to your Nintendo account going forward, but it will make it infinitely harder for would-be attackers.

geeky,Tech

via Gizmodo https://gizmodo.com

April 20, 2020 at 05:42PM

Novostella LED Floodlights

Novostella LED Floodlights

https://ift.tt/3cuO0QP

Novostella LED Floodlights

 | Buy

These waterproof LED lights add a wash of color indoors or out. The brightest model punches out 60 watts of illumination, and comes with a 44-key remote to change colors and brightness, as well as fade and strobe modes. The 15-watt Wi-Fi model is compatible with Google Home and Amazon Alexa. Sold in sets of two.

fun

via The Awesomer https://theawesomer.com

April 20, 2020 at 10:15AM

The Best Mosquito Control Gear for Your Patio or Yard

The Best Mosquito Control Gear for Your Patio or Yard

https://ift.tt/2MI9yjy

The Best Mosquito Control Gear for Your Patio or Yard

To keep mosquitoes away from your deck or patio without slathering your skin in bug repellent, get the Thermacell Radius Zone Mosquito Repellent Gen 2.0. After 45 hours researching a category full of marketing hype and debunked methods (including popular options like citronella candles), we found that the Radius stands out by actually being effective. Its rechargeable six-and-a-half-hour battery lasts long enough to odorlessly keep a bedroom-sized area mosquito-free for an entire evening—as long as there’s no breeze.

technology

via Wirecutter: Reviews for the Real World https://ift.tt/2gcK1uO

April 17, 2020 at 12:39PM

If You Want to Build a Gigantic House on the Cheap, Look Into “Barndominiums”

If You Want to Build a Gigantic House on the Cheap, Look Into “Barndominiums”

https://ift.tt/2XFuM6T

While most homes in America have frames made of wood, a barndominium is a steel-framed house that offers a lot of advantages over wood: Lower costs, both in construction and maintenance, and flexibility of design, whether you prefer open-plan or filled with lots of rooms.

The average cost of building a home in America is $100 to $155 per square foot, according to HomeGuide, a web portal between customers and homebuilders. And while that price will vary depending on what materials the structure of the house is made out of, the 2018 U.S. Census shows that 92.6% of single-family homes built that year were wood-framed.

But there is a far cheaper alternative that’s starting to gain traction, primarily in the South: Steel-framed homes known as "barndominiums" (see bottom for etymology).

Image: LoJo

The first cost savings comes in the construction costs: "If you include slab commissioning and finishing into the equation, expect to pay around $80 to $90 per square foot," writes Allied Steel Buildings, who should know; their business is to manufacture and erect them.

We spoke with Mark Kisenwether, a consultant and machine designer with decades of experience in construction and heavy industry. Kisenwether has erected 65 steel buildings in his career, "everything from a 12-by-12 guard shack to a 275-by-372 waste oil facility," he says, and he confirmed that $80 per square foot is do-able. "And if I was doing it myself, I could get it down to $50 per square foot, but I’m Mr. Frugal," he laughs. (He also cautions that he has 34 years of experience, so that $50 price is probably not within the range of your average DIY’er).

In any case, when you go with a barndominium, the initial cost savings isn’t the whole story:

Maintenance Savings. Unlike wood, steel is impervious to rot, mold and termites, and "you don’t have to repaint it every season," Kisenweather points out.

Energy Savings. Structures made of steel move and settle less than wood-framed ones, making them less likely to spring air leaks. With proper insulation installed, the structures are well-poised to protect against the elements.

Insurance Savings. Steel structures obviously don’t burn, and can better withstand earthquakes and tornadoes, which results in lower insurance rates.

Given those savings, the barndominium has become a thing, particularly in the South. And as you’ll see, there is no common architectural style associated with them, it’s really up to the homeowner:

Image: Metal Building Homes

Image: Architecture Lab

Image: Architecture Lab

Image: Sunward Steel Buildings

Image: Sunward Steel Buildings

Image: Sunward Steel Buildings

Image: Sunward Steel Buildings

Image: Sunward Steel Buildings

Image: Sunward Steel Buildings

Image: Rafter J Construction

Image: LoJo

Image: LoJo

Image: LoJo

Image: LoJo

Image: LoJo

As you can see by many of the interior shots here, steel construction affords a lot of design flexibility inside: "You can get spans of over 30 feet, no problem," Kisenwether states. You can live open-plan, "or you can subdivide, put up walls wherever you want, you don’t have to worry about load-bearing."

Why are barndominiums primarily built in the South?

According to the National Association of Home Builders, "Non-wood based framing methods are primarily concentrated in the South due to resiliency requirements [i.e. natural disasters]…Approximately two-thirds of steel framed homes built in 2018 were located in the South, with another one-third in the West."

As for why they’re not popular in the Northeast: According to General Steel Buildings, the American South has the cheapest concrete, insulation and building erection costs. The West is the second cheapest. The most expensive is in the Northeast.

Chart by General Steel Buildings

Since steel homes must be built on concrete slabs and will of course need insulation, the savings of steel framing can evaporate in the Northeast.

Where did the name "barndominium" come from?

According to Allied Steel Buildings, the name has been around for decades. "Barnodominium was a name given to barns that had been modified to include living quarters," i.e. a condominium.

In 2016, however, the term jumped from the industry into the minds of millions of TV viewers. On a season three episode of the HGTV show "Fixer Upper," designers/remodelers Chip and Joanna Gaines renovated a 1980 horse barn that had been modified to have an apartment upstairs. Chip referred to it as a barndominium, and given the popularity of the show, awareness of and demand for the structures increased.

Image: Joanna Gaines – Magnolia

Image: Joanna Gaines – Magnolia

Image: Joanna Gaines – Magnolia

Image: Joanna Gaines – Magnolia

Image: Joanna Gaines – Magnolia

Image: Joanna Gaines – Magnolia

If you want to test drive a barndominium, all you have to do is wait until the COVID-19 crisis is over and you can travel again. The Gaines-renovated one you see above is actually up on AirBNB.

fun

via Core77 https://ift.tt/1KCdCI5

April 16, 2020 at 01:18PM

Things That Don’t Suck: Electronic Shooters Protection, Stealth Ear Plugs

Things That Don’t Suck: Electronic Shooters Protection, Stealth Ear Plugs

https://ift.tt/2yjLIoL

I haven’t changed my mind. I still love my Walker’s Razor Slim Electronic Ear Muffs (see my review here). However, when I shoot rifles or shotguns, the Ear Muffs are not the easiest option for protecting my hearing.

Recently this was pointed out in painfully clarity. Unfortunately it happened to my wife, Frances. She was shooting a rifle we were reviewing and was using my Walker’s Razor Slims.

Unnoticed to me or Frances, when getting a good cheek weld on the rifle, the muff slipped up and when she touched off the round it blasted her right ear. She had tinnitus for sometime afterwards.

The same has happened to me in the past and I’ve tried to avoid this with using additional foam plugs in my ear canals.

In January, at the Dallas Safari Club Convention, Frances and I had the opportunity to interview Jack Homa of Electronic Shooters Protection about their ESP electronic ear plugs. Needless to say, my interest was peaked, and shortly after getting back home to Georgia I sent Jack results from a hearing test and ear impressions – both supplied by my audiologist.

Ten days later a pair of Stealth ear plugs arrived in my mailbox.

 

 

At the range

There are two crucial requirements that electronic hearing protection — muffs or plugs — must fulfill. First, they have to block out damaging sound frequencies. Second, they must allow the shooter or hunter to hear quiet conversation from guides or companions as well as game movements.

As I stated in the video, when I inserted the Stealth ear plugs and turned them up, I could easily hear normal conversations. My question was whether they would block out damaging decibels when shooting at the range or hunting.

I took my Walker’s muffs along with me for my first outing to my gun club. I needed to complete a review of a Daniel Defense PDW and could not afford to find out that the ESP Stealth plugs sucked without a fallback.

The first round I touched off through the 300 Blackout PDW was accompanied by an involuntary wince. The sound didn’t hurt, but I was definitely ready for the effect of the lack of full-coverage (as provided by my Walker’s ear muffs) to let in painful sound.

The Stealth ear plugs didn’t let such sound through. However, Daniel Defense equips their PDWs with a linear compensator to direct muzzle blast away from the shooter, so that may have helped.

That thought came to me about the time my range buddy, positioned two benches to my right, touched off a round with his muzzle brake-equipped rifle. The shock wave pushed against my face, but my ear heard only a low boom.

He actually kept apologizing throughout the morning for the noise he was making (with all four of his similarly-equipped rifles), but I assured him that he was helping me test the ear plugs I was using. The range work with the PDW included a lot or rounds down range, with the ESP product worn throughout.

Needless to say, I was relieved that the ESP Stealth electronic ear plugs came through.

My next opportunity to use the STEALTH ear plugs came when Frances and I headed to the Government Training Institute’s ranges near Barnwell, South Carolina. This was where Frances experienced the problem with the Walker’s Ear Muffs. I, on the other hand, found the ESP products able to handle a very different environment.

We had a stiff wind blowing most of the day, but I was able to hear Frances’ comments when she called my shots

The two rifles being tested that day were both equipped with muzzle brakes, but the extreme decibels were quelled whether standing next to Frances to call her shots or shooting the rifles.

They just work

As a hunter, especially one who mainly hunts with shotguns or rifles, I was particularly interested in how the ESP Stealth ear plugs would perform when I went after game. My chance for this final test of the Stealths came recently when I hunted quail with my host and friend, Keane Phillips. Keane is a member at Dorchester Shooting Preserve near Savannah, Georgia.

Cutting to the chase, when out hunting at Dorchester, I could hear not only my guide Jeff’s sotto voce directions, but also Keane’s low chuckles as I missed time and again. What I did NOT hear were the damaging frequencies from Keane’s or my shotguns.

I could also hear the friendly yaps of our companions.

The price tag of the ESP Stealth ear plugs is not inconsequential. The MSRP is $2100 (they have other models that range from $900 to $2400). But the custom-molded fit, the quality of sound they produce along with the capacity to hear people and game while providing excellent hearing protection make this a good example of a ‘pay once, cry once’ accessory.

If I went cheaper, I would worry that I would risk my longterm hearing or lose the capacity to hear the voices and natural sounds at the range or the hunting field.

 

Mike Arnold writes for a number of outlets; links to other articles can be found here.

[All photos and video courtesy of Frances Arnold, Keane Phillips and Mike Arnold.]

 

guns

via The Truth About Guns https://ift.tt/1TozHfp

April 16, 2020 at 05:00PM

Poll: What Post-Processing Software Do You Use to Edit Your Photos?

Poll: What Post-Processing Software Do You Use to Edit Your Photos?

https://ift.tt/34Ew7wh

The post Poll: What Post-Processing Software Do You Use to Edit Your Photos? appeared first on Digital Photography School. It was authored by Caz Nowaczyk.

Poll: What Post-Processing Software Do You Use to Edit Your Photos?

Here at dPS, we’d like to know what post-processing software you use to edit your photos so that we can deliver some post-production tutorials that better suit you.

Let us know below. You can vote for more than one if you use multiple editing platforms. If the software isn’t listed, please let us know what you use in the comments section!

Note: There is a poll embedded within this post, please visit the site to participate in this post’s poll.

The post Poll: What Post-Processing Software Do You Use to Edit Your Photos? appeared first on Digital Photography School. It was authored by Caz Nowaczyk.

photography

via Digital Photography School https://ift.tt/29wB9CX

April 16, 2020 at 08:38AM

Replacing web sockets with Livewire

Replacing web sockets with Livewire

https://ift.tt/34DS5PW

Up until a few days ago, the real-time UI of Oh Dear (an uptime monitoring SaaS I run) was powered with web sockets. We recently replaced this with Livewire components.

In this blog post, I’d like to explain why and how we did that.

I’m going to assume that you already know what Livewire is. If not, head over to the Livewire docs. There’s even a short video course on there. In short, Livewire enables you to create dynamic UIs using server-rendered partials.

Why ditch web sockets #

There are hundreds of uptime checking services, but most of them look pretty crappy. When my buddy Mattias and I created Oh Dear, we set out the goal to create a beautiful service that is easy to use. We believe that our service doesn’t necessarily has to be unique. By providing a good design, UX, and docs, you can get ahead of a large part of the competition.

One of the things we decided on very early on was that our UI needed to be real-time. We don’t want users to refresh to see the latest results of their checks. When a new user adds their first site to our service, we wanted the whole process to be snappy and display results fast.

This is what that looks like in Oh Dear. After signing up, users can fill out this form. When pressing enter, it gets added to the list, and results are immediately displayed as they come in.

This behavior used to be powered by web sockets. Whenever Oh Dear started and completed a check, it would broadcast events to the front end. The broadcasting itself would be done using Laravel’s native broadcasting features and the Laravel WebSockets package that Marcel and I created. On the front end, we used Laravel Echo to receive the events and a couple of Vue components.

It all worked fine, but it sure added some complexity to our stack. Also, most of the time, once users have set up their sites, they won’t visit our website often again. But we’d still keep broadcasting events. We could avoid this by tracking presence in some way, but this would complicate things even further.

By moving to Livewire, we can ditch all of the things mentioned above entirely. There’s no need for a web sockets server, Echo, and Vue to create the experience in the movie above. In fact, in that movie, Livewire is already being used.

Let’s take a look at these Livewire components.

Displaying the list of sites #

This is what the list of sites looks like.

screenshot of site list

Here is the Blade view that is used to render that screen.

<x-app-layout title="Dashboard" :breadcrumbs="Breadcrumbs::render('sites')"> <div class="wrap"> <section class="card pb-8 cloak-fade" v-cloak> @include('app.sites.list.partials.notificationsNotConfigured') <livewire:site-list /> <div class="flex items-center mt-8" id="site-adder"> <livewire:site-adder /> </div> </section> </div> </x-app-layout> 

So we have two Livewire components going on: site-list and site-adder. In the LivewireServiceProvider you can see

namespace App\Providers; use App\Http\App\Livewire\Sites\SiteListComponent; use App\Http\App\Livewire\SiteSettings\SiteAdderComponent; use Illuminate\Support\ServiceProvider; use Livewire\Livewire; class LivewireServiceProvider extends ServiceProvider { public function boot() { Livewire::component('site-list', SiteListComponent::class); Livewire::component('site-adder', SiteAdderComponent::class); // other components } } 

Next, let’s take a look at the SiteListComponent class and its underlying view. You don’t have to understand it all now. After the source code, I’m going to explain some parts, but I think it’s good that you already take a look at the component as a whole, so you have some context.

namespace App\Http\App\Livewire\Sites; use Livewire\Component; use Livewire\WithPagination; class SiteListComponent extends Component { use WithPagination; protected $listeners = ['siteAdded' => '$refresh']; public int $perPage = 30; public string $search = ''; public bool $onlySitesWithIssues = false; public bool $showSiteAdder = false; public function showAll() { $this->search = ''; $this->onlySitesWithIssues = false; } public function showOnlyWithIssues() { $this->search = ''; return $this->onlySitesWithIssues = true; } public function showSiteAdder() { $this->showSiteAdder = true; } public function hideSiteAdder() { $this->showSiteAdder = false; } public function render() { return view('app.sites.list.components.siteList', [ 'sites' => $this->sites(), 'sitesWithIssuesCount' => $this->sitesWithIssuesCount(), ]); } protected function sites() { $query = currentTeam()->sites()->search($this->search); if ($this->onlySitesWithIssues) { $query = $query->hasIssues(); } return $query ->orderByRaw('(SUBSTRING(url, LOCATE("://", url)))') ->paginate($this->perPage); } protected function sitesWithIssuesCount(): int { return currentTeam()->sites()->hasIssues()->count(); } } 

And this is the content of the view that is being rendered in that render function: app.sites.list.components.siteList.

<div> <div wire:poll.5000ms> <section class="flex items-center justify-between w-full mb-8"> <div class="text-xs"> <div class="switcher"> <button wire:click="showAll" class="switcher-button ">Display all sites </button> <button wire:click="showOnlyWithIssues" class="switcher-button "> Display   with issues </button> </div> </div> <div class="flex items-center justify-end"> <a href="#site-adder" wire:click="$emit('showSiteAdder')" class="button is-secondary mr-4">Add another site</a> <input wire:model="search" class="form-input is-small w-48 focus:w-64" type="text" placeholder="Filter sites..."/> </div> </section> <table class="site-list-table w-full"> <thead> <th style="width: 27%;">Site</th> <th style="width: 14%;">Uptime</th> <th style="width: 14%;">Broken links</th> <th style="width: 14%;">Mixed Content</th> <th style="width: 14%;">Certificate Health</th> <th style="width: 17%;">Last check</th> </thead> <tbody> @forelse($sites as $site) <tr> <td class="pr-2"> <div class="flex"> <span class="w-6"><i class="fad text-gray-700 "></i></span> <a href="" class="flex-1 underline truncate"></a> </div> </td> <td class="pr-2"> <x-check-result :site="$site" check-type="uptime"/> </td> <td class="pr-2"> <x-check-result :site="$site" check-type="broken_links"/> </td> <td class="pr-2"> <x-check-result :site="$site" check-type="mixed_content"/> </td> <td class="pr-2"> <x-check-result :site="$site" check-type="certificate_health"/> </td> <td class="text-sm text-gray-700"> @if($site->latest_run_date) <a href=""> <time datetime="" title=""></time> </a> @else No runs yet @endif </td> </tr> @empty <tr><td class="text-center" colspan="6">There are no sites that match your search...</td></tr> @endforelse </tbody> </table> @if ($sites->total() > $perPage) <div class="flex justify-between mt-4"> <div class="flex-1 w-1/2 mt-4">  </div> <div class="flex w-1/2 text-right text-muted text-sm text-gray-700 mt-4"> <div class="w-full block"> Showing  to  out of  sites </div> </div> </div> @endif </div> </div> 

In the render function, you probably have noticed that the component itself is responsible for getting the sites.

public function render() { return view('app.sites.list.components.siteList', [ 'sites' => $this->sites(), 'sitesWithIssuesCount' => $this->sitesWithIssuesCount(), ]); } protected function sites() { $query = currentTeam()->sites()->search($this->search); if ($this->onlySitesWithIssues) { $query = $query->hasIssues(); } return $query ->orderByRaw('(SUBSTRING(url, LOCATE("://", url)))') ->paginate($this->perPage); } 

Filtering sites #

Notice that $this->onlySitesWithIssues check? Let’s see how that variable is being set. If you take a look at the screenshot of the site list, you’ll notice a little filter on the top with “Display all sites” and “Display sites with issues”.

This is the part of the view that renders that.

<div class="switcher"> <button wire:click="showAll" class="switcher-button ">Display all sites </button> <button wire:click="showOnlyWithIssues" class="switcher-button "> Display   with issues </button> </div> 

Notice that wire:click="showOnlyWithIssues". When the user clicks an element with wire:click the method whose name is in the attribute value will be executed, and the component will be re-rendered. So, in this case, showOnlyWithIssues is executed.

public function showOnlyWithIssues() { $this->search = ''; return $this->onlySitesWithIssues = true; } 

So, here our onlySitesWithIssues instance variable is changed, causing our component to re-render. Because onlySitesWithIssues is now set to true, the sites() method will now filter on sites having issues.

if ($this->onlySitesWithIssues) { $query = $query->hasIssues(); } 

When the component gets rendered, only sites with issues are displayed. Under the hood, Livewire takes care of a lot of things, most notably calling the method on the component when that wire:click element is called, and replacing the HTML of the component on the page when the HTML of the re-rendered version.

Searching sites #

In the top right corner of the site list, you see a search box. When you type something there, you will only see sites who’s name contain your query. Here’s how that works.

In the Blade view, this is the relevant HTML.

<input wire:model="search" class="form-input is-small w-48 focus:w-64" type="text" placeholder="Filter sites..." /> 

Here, another Livewire directive is used: wire:model. This directive will make sure that each time you type something in that element, Livewire will update the instance variable with the same name in the component and re-render the component. By default, this behavior is debounced, meaning that when you type fast, only one request will be made every 150ms.

So, because the search instance variable is changed on the component, it will re-render. In the first line of the sites() method, the value is being used.

$query = currentTeam()->sites()->search($this->search); 

And with this in place, searching sites works. I think it’s kinda amazing that you can have this behavior by merely adding livewire:model in your view, and using that value to scope your query. Sure, you could do this with Vue, but this is way simpler.

For completeness, here’s the scopeSearch that’s on the Site model.

public static function scopeSearch(Builder $query, string $searchFor): void { if ($query === '') { return; } $query->where('url', 'like', "%{$searchFor}%"); } 

Paginating sites #

Laravel natively has excellent support for paginating query results. You only have to call paginate on your query, which we have done in the sites() method.

// in the sites() method return $query ->orderByRaw('(SUBSTRING(url, LOCATE("://", url)))') ->paginate($this->perPage); 

Calling $sites->links() in the view will render URLs with a page parameter. Under the hood of the paginate() method, that query parameter is used to fetch the results of that page. That’s in short how pagination works in Laravel.

Adding pagination support to a Livewire component is super easy with Livewire. You just have to use the Livewire\WithPagination trait on your component. This trait does some magic so the render links don’t contain a URL to your page, but to an internal Livewire route. This is the output of $sites()->links() in the view of Livewire component.

<ul> <li class="page-item active" aria-current="page"><span class="page-link">1</span></li> <li class="page-item"><a class="page-link" href="https://ohdear.app/livewire/message/site-list?page=2">2</a></li> <li class="page-item"><a class="page-link" href="https://ohdear.app/livewire/message/site-list?page=3">3</a></li> <li class="page-item"><a class="page-link" href="https://ohdear.app/livewire/message/site-list?page=4">4</a></li> <li class="page-item"> <a class="page-link" href="https://ohdear.app/livewire/message/site-list?page=2" rel="next" aria-label="Next »"></a> </li> </ul> 

When clicking such a link, Livewire will set the page query parameter in the URL and re-render the component. It’s an elegant solution, and I like that it hooks in so nicely with Laravel’s default pagination.

As a Livewire user, the only thing you needed to do extra was to apply that WithPagination trait. Beautiful.

Updating the list #

As stated earlier, want to have our UI to display the current state, without users having to refresh. If one of the sites displayed in the list goes down, the list should get updated automatically.

The solution for this is straightforward: we’re just going to poll for changes. Livewire comes with support for polling out of the box. At the top of the view you might have noticed this:

<div wire:poll.5000ms> 

This will make Livewire refresh the component automatically every five seconds. To re-render the component in the browser, Livewire makes use of morphdom. This will compare the HTML of the component with the HTML of the re-rendered version. Only the changes will get updated in the DOM, making the cost for re-rendering in the browser very low.

While typing this blog post, I can already feel the screams of some readers: “Freek, what are you doing!?!? This is highly inefficient: it will perform queries and make requests the whole time…”. And yes, that is true.

Using Livewire with polling is a trade-off. One the one hand, we have some more requests/queries, but this only happens when users are looking at their site lists. This won’t happen often. To know the state of their sites, the vast majority of our users rely on the alerts we sent out (via Slack, Mail, Telegram, …), and they won’t be staring at the site list the whole time.

It’s also very nice that Livewire only polls if the browser is displaying the page. When a browser tab is in the background, polling is paused.

By using a little bit of polling, we don’t need to set up web sockets or Vue, and we don’t need to broadcast events the whole time, … It vastly simplifies our tech stack. I think in this case, the trade-off that Livewire / polling brings to the table is worth it.

In closing #

I hope you enjoyed this dissection of our site list Livewire component. Other place in our UI, where realtime info is being displayed, was refactored in a similar fashion.

Working with Livewire was a pleasure. The API makes sense, the docs are well written, and it’s easy to create powerful components in a short amount of time.

If you want to see the Livewire components discussed in this blogpost in action, head over to Oh Dear and start your ten-day trial (no credit card needed).

After ten days, you might want to get a subscription. What sets Oh Dear apart from the competition is that we not only monitor your homepage, but your entire site. We can alert you when a broken link, or mixed content, is found on any pages of your website. And we also monitor certificate health and provide beautiful status pages like this one.

Should you have questions about these Livewire components, or Oh Dear in general, let me know in the comments below.

programming

via Laravel News Links https://ift.tt/2dvygAJ

April 16, 2020 at 09:06AM