How servers are managed under the hood | laravel-dynamic-servers | Spatie

 SPATIE

  Laravel Dynamic Servers
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-dynamic-servers](https://spatie.be/docs/laravel-dynamic-servers/v1)  Advanced-usage  How servers are managed under the hood

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-dynamic-servers/v1)

  How servers are managed under the hood
- [ Introduction ](https://spatie.be/docs/laravel-dynamic-servers/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-dynamic-servers/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-dynamic-servers/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-dynamic-servers/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-dynamic-servers/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-dynamic-servers/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-dynamic-servers/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/getting-started)
- [ Configuring your first server type ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/configuring-your-first-server-type)
- [ Ensuring a number of servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/ensuring-a-number-of-servers)
- [ Rebooting servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/rebooting-servers)
- [ Using multiple server types ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/using-multiple-server-types)
- [ Manually increasing and decreasing servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/basic-usage/manually-increasing-and-decreasing-servers)

Advanced usage
--------------

- [ Creating your own server provider ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/creating-your-own-server-provider)
- [ How servers are managed under the hood ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/how-servers-are-managed-under-the-hood)
- [ Dealing with erroring servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/dealing-with-erroring-servers)
- [ Dealing with hanging servers ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/dealing-with-hanging-servers)
- [ Fired Events ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/fired-events)
- [ Overriding actions and jobs ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/overriding-actions)
- [ Setting a server limit ](https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/setting-a-server-limit)

 How servers are managed under the hood
======================================

###  On this page

1. [ Creating and starting servers ](#content-creating-and-starting-servers)
2. [ Stopping and deleting servers ](#content-stopping-and-deleting-servers)

To use the package, you don't need to know how things are working on the hood. However, to troubleshoot things, or when you create your own server provider, it might be handy to know what is going on. On this page, we'll describe how the package works internally.

An important thing to know is that the package tracks state of all servers it has created in the `dynamic_servers` table.

Creating and starting servers
-----------------------------------------------------------------------------------------------------------------------------------------------

Whenever you call `DynamicServers::increase()` (or `::ensure()` and it creates servers for you), we will create a new `Spatie\DynamicServers\Models\Server` model. When it is created it will have the status `new`.

Right after such a model is created, we will call `start()` on it.

```
// happens internally

$server->start():
```

This will start a job, `Spatie\DynamicServers\Jobs\StopServer` that will call the `startServer` method on the `ServerProvider` class of the server. The `Server` model will now have status `starting`.

Most hosting servers cannot start servers instantly. It takes a while to provision them. That's why the `CreateServerJob` will dispatch another job, `VerifyServerStartedJob` to verify that a server has been started. The `VerifyServerStartedJob` will use the `hasStarted` method on the `ServerProvider` class to determine if the server has been fully started at the hosting provider. If the server has not started, the `VerifyServerStartedJob` will release it self, and another attempt will be made 20 seconds later.

If `hasStarted` returns `true`, then `VerifyServerStartedJob` will finish properly and the `Server` model will be marked as `running`.

Stopping and deleting servers
-----------------------------------------------------------------------------------------------------------------------------------------------

Whenever you call `DynamicServers::decrease()` (or `::ensure()` and it removes servers for you), we will call `stop()` on those servers.

```
// happens internally

$server->stop():
```

This will start a job, `Spatie\DynamicServers\Jobs\StopServerJob` that will call the `stopServer` method on the `ServerProvider` class of the server. The `Server` model will now have status `stopping`.

Most hosting servers cannot stop servers instantly. It takes a while to shut them down them. That's why the `StopServerJob` will dispatch another job, `VerifyServerStoppedJob` to verify that a server has been stopped. The `VerifyServerStoppedJob` will use the `hasStopped` method on the `ServerProvider` class to determine if the server has been fully stopped by the hosting provider. If the server has not stopped, the `VerifyServerStoppedJob` will release itself, and another attempt will be made 20 seconds later.

If `hasStopped` returns `true`, then `VerifyServerStoppedJob` will mark `Server` model `stopped`. Next, the `DeleteServerJob` will be called. This job will call `deleteServer` on the `ServerProvider` to delete the server. The `Server` model will have status `deleting`

The `VerifyServerDeletedJob` will be dispatched to verify that a server has been deleting. The `VerifyServerDeletedJob` will use the `hasBeenDeleted` method on the `ServerProvider` class to determine if the server has been fully stopped by the hosting provider. If the server has not stopped, the `VerifyServerDeletedJob` will release itself, and another attempt will be made 20 seconds later.

If `hasBeenDeleted` returns `true`, then the `Server` will have status `deleted`.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
