Skip to content

Commit 020f6ce

Browse files
committed
include sample content and update themes with smaller fonts
1 parent a5b6074 commit 020f6ce

10 files changed

+637
-14
lines changed

src/Commands/InitCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ protected function configure()
3434
/**
3535
* Execute the command.
3636
*
37-
* @param InputInterface $input
38-
* @param OutputInterface $output
37+
* @param InputInterface $input
38+
* @param OutputInterface $output
3939
* @return int
4040
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4141
* @throws \Mpdf\MpdfException
@@ -66,6 +66,11 @@ public function execute(InputInterface $input, OutputInterface $output)
6666
$currentPath.'/content'
6767
);
6868

69+
$this->disk->copyDirectory(
70+
__DIR__.'/../../stubs/content',
71+
$currentPath.'/content'
72+
);
73+
6974
$this->disk->put(
7075
$currentPath.'/ibis.php',
7176
$this->disk->get(__DIR__.'/../../stubs/ibis.php')

stubs/assets/theme-dark.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<style>
44
body {
55
font-family: calibri;
6-
font-size: 19px;
6+
font-size: 14px;
77
line-height: 1.4;
88
background-color: #011627;
99
color: #c1d4ea;
@@ -21,21 +21,21 @@
2121
}
2222

2323
h1 {
24-
font-size: 45px;
24+
font-size: 35px;
2525
padding-top: 0;
2626
padding-bottom: 70px;
2727
}
2828

2929
h2 {
3030
text-align: left;
31-
font-size: 30px;
31+
font-size: 20px;
3232
padding-top: 0;
3333
padding-bottom: 30px;
3434
}
3535

3636
h3 {
3737
text-align: left;
38-
font-size: 21px;
38+
font-size: 19px;
3939
padding-top: 20px;
4040
padding-bottom: 0;
4141
}

stubs/assets/theme-light.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<style>
44
body {
55
font-family: calibri;
6-
font-size: 19px;
6+
font-size: 14px;
77
line-height: 1.4;
88
color: #252525;
99
}
@@ -18,21 +18,21 @@
1818
}
1919

2020
h1 {
21-
font-size: 45px;
21+
font-size: 35px;
2222
padding-top: 0;
2323
padding-bottom: 70px;
2424
}
2525

2626
h2 {
2727
text-align: left;
28-
font-size: 30px;
28+
font-size: 20px;
2929
padding-top: 0;
3030
padding-bottom: 30px;
3131
}
3232

3333
h3 {
3434
text-align: left;
35-
font-size: 21px;
35+
font-size: 19px;
3636
padding-top: 20px;
3737
padding-bottom: 0;
3838
}

stubs/content/001-queues-101.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Queues 101
2+
3+
Every good technical book starts with a crash course, and this one is no exception.
4+
5+
In this part, we will explore the key components of a queue system, see how queues are used in Laravel, and understand why we need to use queues in our applications.

stubs/content/002-key-components.md

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
## Key Components
2+
3+
>{notice} This is a sample content from [Laravel Queues in Action](https://learn-laravel-queues.com/). A book by [Mohamed Said](https://twitter.com/themsaid) the creator of Ibis.
4+
5+
A queue system has 3 main components; queues, messages, and workers.
6+
7+
Let's start by exploring each of these components.
8+
9+
### Queues
10+
11+
>{quote} A queue is a linear data structure in which elements can be added to one end, and can only be removed from the other end.
12+
13+
That's the definition you find in most Computer Science books, and it can be quite confusing.
14+
15+
Let me show you a queue in action:
16+
17+
```php
18+
$queue = [
19+
'DownloadProject',
20+
'RunTests'
21+
];
22+
```
23+
24+
This queue contains two messages. We can `enqueue` a new message and it'll be added to the end of the queue:
25+
26+
```php
27+
enqueue('Deploy');
28+
29+
$queue = [
30+
'DownloadProject',
31+
'RunTests',
32+
'Deploy'
33+
];
34+
```
35+
36+
We can also `dequeue` a message and it'll be removed from the beginning of the queue:
37+
38+
```php
39+
$message = dequeue(); // == DownloadProject
40+
41+
$queue = [
42+
'RunTests',
43+
'Deploy'
44+
];
45+
```
46+
47+
If you ever heard the term "first-in-first-out (FIFO)" and didn't understand what it means, now you know it means the first message in the queue is the first message that gets processed.
48+
49+
### Messages
50+
51+
A message is a call-to-action trigger. You send a message from one part of your application and it triggers an action in another part. Or you send it from one application and it triggers an action in a completely different application.
52+
53+
The message sender doesn't need to worry about what happens after the message is sent, and the message receiver doesn't need to know who the sender is.
54+
55+
A message body contains a string, this string is interpreted by the receiver and the call to action is extracted.
56+
57+
### Workers
58+
59+
A worker is a long-running process that dequeues messages and executes the call-to-action.
60+
61+
Here's an example worker:
62+
63+
```php
64+
while (true) {
65+
$message = dequeue();
66+
67+
processMessage(
68+
$message
69+
);
70+
}
71+
```
72+
73+
Once you start a worker, it'll keep dequeuing messages from your queue. You can start as many workers as you want; the more workers you have, the faster your messages get dequeued.
74+
75+
## Queues in Laravel
76+
77+
Laravel ships with a powerful queue system right out of the box. It supports multiple drivers for storing messages:
78+
79+
- Database
80+
- Beanstalkd
81+
- Redis
82+
- Amazon SQS
83+
84+
Enqueuing messages in Laravel can be done in several ways, and the most basic method is using the `Queue` facade:
85+
86+
```php
87+
use Illuminate\Support\Facades\Queue;
88+
89+
Queue::pushRaw('Send invoice #1');
90+
```
91+
92+
If you're using the database queue driver, calling `pushRaw()` will add a new row in the `jobs` table with the message "Send invoice #1" stored in the `payload` field.
93+
94+
Enqueuing raw string messages like this is not very useful. Workers won't be able to identify the action that needs to be triggered. For that reason, Laravel allows you to enqueue class instances:
95+
96+
```php
97+
use Illuminate\Support\Facades\Queue;
98+
99+
Queue::push(
100+
new SendInvoice(1)
101+
);
102+
```
103+
104+
>{notice} Laravel uses the term "push" instead of "enqueue", and "pop" instead of "dequeue".
105+
106+
When you enqueue an object, the queue manager will serialize it and build a string payload for the message body. When workers dequeue that message, they will be able to extract the object and call the proper method to trigger the action.
107+
108+
Laravel refers to a message that contains a class instance as a "Job". To create a new job in your application, you may run this artisan command:
109+
110+
```php
111+
php artisan make:job SendInvoice
112+
```
113+
114+
This command will create a `SendInvoice` job inside the `app/Jobs` directory. That job will look like this:
115+
116+
```php
117+
namespace App\Jobs;
118+
119+
class SendInvoice implements ShouldQueue
120+
{
121+
use Dispatchable;
122+
use InteractsWithQueue;
123+
use Queueable;
124+
use SerializesModels;
125+
126+
public function __construct()
127+
{
128+
129+
}
130+
131+
public function handle()
132+
{
133+
// Execute the job logic.
134+
}
135+
}
136+
```
137+
138+
When a worker dequeues this job, it will execute the `handle()` method. Inside that method, you should put all your business logic.
139+
140+
>{notice} Starting Laravel 8.0, you can use `__invoke` instead of `handle` for the method name.
141+
142+
### The Command Bus
143+
144+
Laravel ships with a command bus that can be used to dispatch jobs to the queue. Dispatching through the command bus allows us to use several functionalities that I'm going to show you later.
145+
146+
Throughout this book, we're going to use the command bus to dispatch our jobs instead of the `Queue::push()` method.
147+
148+
Here's an example of using the `dispatch()` helper which uses the command bus under the hood:
149+
150+
```php
151+
dispatch(
152+
new SendInvoice(1)
153+
);
154+
```
155+
156+
Or you can use the `Bus` facade:
157+
158+
```php
159+
use Illuminate\Support\Facades\Bus;
160+
161+
Bus::dispatch(
162+
new SendInvoice(1)
163+
);
164+
```
165+
166+
You can also use the `dispatch()` static method on the job class:
167+
168+
```php
169+
SendInvoice::dispatch(1);
170+
```
171+
172+
>{notice} Arguments passed to the static `dispatch()` method will be transferred to the job instance automatically.
173+
174+
### Starting A Worker
175+
176+
To start a worker, you need to run the following artisan command:
177+
178+
```shell
179+
php artisan queue:work
180+
```
181+
182+
This command will bootstrap an instance of your application and keep checking the queue for jobs to process.
183+
184+
```php
185+
$app = require_once __DIR__.'/bootstrap/app.php';
186+
187+
while (true) {
188+
$job = $app->dequeue();
189+
190+
$app->process($job);
191+
}
192+
```
193+
194+
Re-using the same instance of your application has a major performance gain as your server will have to bootstrap the application only once during the time the worker process is alive. We'll talk more about that later.
195+
196+
## Why Use a Message Queue?
197+
198+
Remember that queuing is a technique for indirect program-to-program communication. Your application can send messages to the queue, and other parts of the application can read those messages and execute them.
199+
200+
Here's how this can be useful:
201+
202+
### Better User Experience
203+
204+
The most common reason for using a message queue is that you don't want your users to wait for certain actions to be performed before they can continue using your application.
205+
206+
For example, a user doesn't have to wait until your server communicates with a third-party mail service before they can complete a purchase. You can just send a success response so the user continues using the application while your server works on sending the invoice in the background.
207+
208+
### Fault Tolerance
209+
210+
Queue systems are built to persist jobs until they are processed. In the case of failure, you can configure your jobs to be retried several times. If the job keeps failing, the queue manager will put it in safe storage so you can manually intervene.
211+
212+
For example, if your job interacts with an external service and it went down temporarily, you can configure the job to retry after some time until this service is back online.
213+
214+
### Scalability
215+
216+
For applications with unpredictable load, it's a waste of money to allocate more resources all the time even when there's no load. With queues, you can control the rate at which your workers consume jobs.
217+
218+
Allocate more resources and your jobs will be consumed faster, limit those resources and your jobs will be consumed slower but you know they'll eventually be processed.
219+
220+
### Batching
221+
222+
Batching a large task into several smaller tasks is more efficient. You can tune your queues to process these smaller tasks in parallel which will guarantee faster processing.
223+
224+
### Ordering and Rate Limiting
225+
226+
With queues, you can ensure that certain jobs will be processed in a specific order. You can also limit the number of jobs running concurrently.

stubs/content/003-cookbook.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Cookbook
2+
3+
In this part, we will walk through several real-world challenges with solutions that actually run in production. These are challenges I met while building products at Laravel and others collected while supporting the framework users for the past four years.
4+
5+
While doing this, I'm going to explain every queue configuration, gotcha, and technique we meet on our way.

0 commit comments

Comments
 (0)