Skip to content

Commit

Permalink
Added a slide to show the batch running. Tweaked the formatting and w…
Browse files Browse the repository at this point in the history
…ording of some slides.
  • Loading branch information
philipnorton42 committed Oct 18, 2024
1 parent 6fb8e54 commit 31a6365
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ marp: true
---

## Source Code
- This presentation is available at
- This presentation:
<small>https://github.com/hashbangcode/drupal-batch-api-talk</small>
- All code seen in this presentaiton is available at
- All code seen here is available:
<small>https://github.com/hashbangcode/drupal_batch_examples</small>
- I have also written extensively about the Batch API on <small>https://www.hashbangcode.com/</small>

Expand Down Expand Up @@ -89,7 +89,7 @@ Source: https://www.cloudflare.com/learning/performance/more/website-performance
- But, setting them high would cause problems on your web server.
- Think about how long a page request should take in you web server.
2-5 seconds MAX!
- The more resources you give to your web server, the users you can accommodate at once.
- The more resources you give to these services, the users you can accommodate at once.
-->

---
Expand Down Expand Up @@ -176,14 +176,14 @@ foreach ($chunks as $id => $chunk) {

## Initialise - Start Batch Run

Set the batch running by calling `toArray()` and passing the array to `batch_set()`.
- Set the batch running by calling `toArray()` and passing the array to `batch_set()`.

```php
batch_set($batch->toArray());
```

The whole purpose of `BatchBuilder` is to generate that array.
This will trigger and start up the batch process.
- The whole purpose of `BatchBuilder` is to generate that array.
- This will trigger and start up the batch process.
<!--
- This will redirect to the batch processor interface and call the process operations.
- Yes you can also just define the array and send it to batch_set(), but I find BatchBuilder a nicer interface.
Expand Down Expand Up @@ -232,7 +232,34 @@ $context['message'] = t('Processing batch #@batch_id batch size @batch_size for
'@count' => number_format($context['sandbox']['max']),
]);
```
---

## Process

- Perform the task you want in the batch.

```php
public static function batchProcess(int $batchId, array $chunk, array &$context): void {
// --- Set up and messages goes here...
$random = new Random();
foreach ($chunk as $number) {
$context['results']['progress']++;
$node = Node::create([
'type' => 'article',
'title' => $random->name(15),
'body' => [
'value' => '<p>' . $random->sentences(2) . '</p>', 'format' => filter_default_format(),
],
'uid' => 1,
'status' => 1,
]);
$node->save();
}
}
```
<!--
- This creates 1,000 nodes based on the chunks of 100 items from the setup.
-->
---

## Finish - The Finished Callback
Expand Down

0 comments on commit 31a6365

Please sign in to comment.