Skip to content

Commit

Permalink
feat: safe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Apr 5, 2024
1 parent 07ad305 commit 0f77888
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
7 changes: 4 additions & 3 deletions framework/core/js/src/admin/components/AdvancedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ export default class AdvancedPage<CustomAttrs extends IPageAttrs = IPageAttrs> e
help: app.translator.trans('core.admin.advanced.maintenance.help'),
setting: 'maintenance_mode',
options: {
0: app.translator.trans('core.admin.advanced.maintenance.options.0'),
1: {
[MaintenanceMode.NO_MAINTENANCE]: app.translator.trans('core.admin.advanced.maintenance.options.0'),
[MaintenanceMode.HIGH_MAINTENANCE]: {
label: app.translator.trans('core.admin.advanced.maintenance.options.1'),
disabled: true,
},
2: app.translator.trans('core.admin.advanced.maintenance.options.2'),
[MaintenanceMode.LOW_MAINTENANCE]: app.translator.trans('core.admin.advanced.maintenance.options.2'),
[MaintenanceMode.SAFE_MODE]: app.translator.trans('core.admin.advanced.maintenance.options.3'),
},
default: 0,
})}
Expand Down
38 changes: 21 additions & 17 deletions framework/core/js/src/common/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export interface RouteResolver<

export enum MaintenanceMode {
NO_MAINTENANCE = 0,
HIGH_MAINTENANCE = 1,
LOW_MAINTENANCE = 2,
SAFE_MODE = 3,
}

export interface ApplicationData {
Expand Down Expand Up @@ -277,23 +279,25 @@ export default class Application {
public boot() {
const caughtInitializationErrors: CallableFunction[] = [];

this.initializers.toArray().forEach((initializer) => {
try {
initializer(this);
} catch (e) {
const extension = initializer.itemName.includes('/')
? initializer.itemName.replace(/(\/flarum-ext-)|(\/flarum-)/g, '-')
: initializer.itemName;

caughtInitializationErrors.push(() =>
fireApplicationError(
extractText(app.translator.trans('core.lib.error.extension_initialiation_failed_message', { extension })),
`${extension} failed to initialize`,
e
)
);
}
});
if (this.data.maintenanceMode !== MaintenanceMode.SAFE_MODE) {
this.initializers.toArray().forEach((initializer) => {
try {
initializer(this);
} catch (e) {
const extension = initializer.itemName.includes('/')
? initializer.itemName.replace(/(\/flarum-ext-)|(\/flarum-)/g, '-')
: initializer.itemName;

caughtInitializationErrors.push(() =>
fireApplicationError(
extractText(app.translator.trans('core.lib.error.extension_initialiation_failed_message', { extension })),
`${extension} failed to initialize`,
e
)
);
}
});
}

this.store.pushPayload({ data: this.data.resources });

Expand Down
4 changes: 3 additions & 1 deletion framework/core/locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ core:
0: No maintenance.
1: High maintenance mode. No one can access the forum (can only be enabled through config.php)
2: Low maintenance mode. Admins can access the forum.
3: Safe mode. No extensions are booted and only admins can access the forum.
search:
section_label: Search Drivers
driver_heading: "Search Driver: {model}"
Expand Down Expand Up @@ -706,7 +707,8 @@ core:

# These translations are used in forum & admin notices.
notices:
maintenance_mode_2: Down for maintenance. Only administrators may access the forum.
maintenance_mode_2: Down for maintenance. Only administrators can access the forum.
maintenance_mode_3: Down for maintenance with safe mode. Only administrators can access the forum and no extensions are booted.

# These translations are used as suffixes when abbreviating numbers.
number_suffix:
Expand Down
10 changes: 9 additions & 1 deletion framework/core/src/Extension/ExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Extension\Event\Disabling;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\MaintenanceMode;
use Illuminate\Contracts\Events\Dispatcher;

class ExtensionServiceProvider extends AbstractServiceProvider
Expand All @@ -25,7 +26,14 @@ public function register(): void
// below, so that extensions have a chance to register things on the
// container before the core boots up (and starts resolving services).
$this->container['flarum']->booting(function () {
$this->container->make('flarum.extensions')->extend($this->container);
/** @var ExtensionManager $manager */
$manager = $this->container->make('flarum.extensions');
/** @var MaintenanceMode $maintenance */
$maintenance = $this->container->make(MaintenanceMode::class);

if (! $maintenance->isSafeMode()) {
$manager->extend($this->container);
}
});
}

Expand Down
13 changes: 9 additions & 4 deletions framework/core/src/Foundation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@ public function inDebugMode(): bool

public function inMaintenanceMode(): bool
{
return $this->inHighMaintenanceMode() || $this->inLowMaintenanceMode();
return $this->inHighMaintenanceMode() || $this->inLowMaintenanceMode() || $this->isSafeMode();
}

public function inHighMaintenanceMode(): bool
{
return $this->maintenanceMode() === 1;
return $this->maintenanceMode() === MaintenanceMode::HIGH;
}

public function inLowMaintenanceMode(): bool
{
return $this->maintenanceMode() === 2;
return $this->maintenanceMode() === MaintenanceMode::LOW;
}

public function isSafeMode(): bool
{
return $this->maintenanceMode() === MaintenanceMode::SAFE;
}

public function maintenanceMode(): int
{
return intval($this->data['offline'] ?? 0);
return intval($this->data['offline'] ?? MaintenanceMode::NONE);
}

private function requireKeys(mixed ...$keys): void
Expand Down
8 changes: 7 additions & 1 deletion framework/core/src/Foundation/MaintenanceMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MaintenanceMode
public const NONE = 0;
public const HIGH = 1;
public const LOW = 2;
public const SAFE = 3;

public function __construct(
protected readonly Config $config,
Expand All @@ -25,7 +26,7 @@ public function __construct(

public function inMaintenanceMode(): bool
{
return $this->inHighMaintenanceMode() || $this->inLowMaintenanceMode();
return $this->inHighMaintenanceMode() || $this->inLowMaintenanceMode() || $this->isSafeMode();
}

public function inHighMaintenanceMode(): bool
Expand All @@ -38,6 +39,11 @@ public function inLowMaintenanceMode(): bool
return $this->mode() === self::LOW;
}

public function isSafeMode(): bool
{
return $this->mode() === self::SAFE;
}

public function mode(): int
{
$mode = $this->config->maintenanceMode();
Expand Down

0 comments on commit 0f77888

Please sign in to comment.