Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion resources/views/docs/mobile/4/digging-deeper/async-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,36 @@ rethrow it there to let your handler end quietly.

</aside>

## Laravel's Concurrency facade

Inside your app, each closure that you pass to Laravel's [`Concurrency` facade](https://laravel.com/docs/concurrency)
runs as an async task, so `Concurrency::run()` and `Concurrency::defer()` work in a screen as you know them from
Laravel:

```php
use Illuminate\Support\Facades\Concurrency;

[$this->users, $this->unpaid] = Concurrency::run([
fn () => User::count(),
fn () => Order::where('paid', false)->count(),
]);
```

There is nothing to configure. On a device and in a [Jump](../the-basics/jump) session, this is the default driver of
the facade, whatever your `concurrency` configuration says, and the `process` and `fork` drivers give you the same one.
When you deploy your Laravel app as a website, it has Laravel's own drivers there. You need Laravel 11.24 or newer.

The closures follow the rules of [the work closure](#the-work-closure), and as in a group, each should be written on a
line of its own.

`run()` holds your handler and your screen until every closure is done, as `wait()` does for a group, and its results
keep their PHP types, such as models and collections. It suits short work, and code that your app shares with a
website. For anything longer, use `AsyncTask::all()` with `finished()`.

You may only call `run()` in a native screen. In a job, in the work of an async task and in a web view, it throws a
`RuntimeException` and starts nothing. There you may use Laravel's `sync` driver, `Concurrency::driver('sync')->run()`,
which runs the closures one after another. You may call `defer()` in a job as well.

## Task classes

For anything you'd rather not write inline, extend `AsyncTask` and put the work in `handle()`:
Expand Down Expand Up @@ -509,6 +539,14 @@ A [group](#grouping-tasks) runs its tasks inline too, in the order of your array
`wait()` returns the results at once. Every task runs, also the ones after a task that failed. Without the fake no task
runs in a test, so `wait()` fails there with a message that tells you to call `AsyncTask::fake()`.

In a test, the [`Concurrency` facade](#laravels-concurrency-facade) uses the driver of your configuration. To test a
screen that calls `Concurrency::run()`, choose the `async-task` driver in your `phpunit.xml` file and call
`AsyncTask::fake()` in your test. The closures then run one after another, and each is recorded as a dispatch:

```xml
<env name="CONCURRENCY_DRIVER" value="async-task"/>
```

## How It Works

When you dispatch a task, the work closure is serialized and handed to a background PHP context — a full PHP
Expand All @@ -529,7 +567,7 @@ the callbacks run on your UI thread, in your component.
- An assignment to `$this` in the work changes the copy. Return the value, and assign it in `finished()`.
- Results round-trip as JSON — return scalars and arrays; pass large data by file path or cache key.
- Tasks run once, with no automatic retry.
- `wait()` holds your screen until the whole group is done, so keep it for short work.
- `wait()` and `Concurrency::run()` hold your screen until everything is done, so keep them for short work.
- Scoped callbacks are dropped if the user navigates away from the screen that started the work;
use `shared()` when the result matters regardless of what screen the user moves to.
- Async tasks don't survive the app being killed. For durable background work, use [Queues](queues).
Expand Down
6 changes: 5 additions & 1 deletion resources/views/docs/mobile/4/digging-deeper/queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ NativePHP manages the worker lifecycle natively on both platforms.
## Things to Note

- The queue worker requires ZTS (Thread-Safe) PHP, which NativePHP includes by default.
- Only the `database` queue connection is supported. This uses the same SQLite database as your app.
- The worker runs jobs on the `database` connection, which uses the same SQLite database as your app.
- A job on Laravel's `background` connection runs as an [async task](async-tasks#laravels-concurrency-facade) instead of
in the worker. It is tried once, and it is lost when the app closes before it ran.
- `Concurrency::run()` can't be called in a job. Use `Concurrency::driver('sync')->run()` there, which runs the closures
one after another.
- Jobs are persisted to the database, so they survive app restarts.
- If a job fails, Laravel's standard retry and failure handling applies.