Skip to content

Commit

Permalink
feature: default disabled roles
Browse files Browse the repository at this point in the history
  • Loading branch information
rezonant committed Jan 24, 2024
1 parent c83b259 commit fbddaa4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ⏭ vNext

# v3.6.1
- `@/runtime`: Add `enabledByDefault` to `RoleRegistration` and default to all services which are enabled by default.
Allows for some roles to be disabled unless specifically asked for. Roles which are disabled by default are still
included in `all-except` configuration. Use the new `default-except` (or via `ALT_ROLES_DEFAULT_EXCEPT` environment
variable) to enable all default services except those listed. Additionally there is now an `--roles-skip` option
which enables the `default-except` mode.

# v3.6.0
- `@/runtime`: Allow specifying additional providers when bootstrapping an application
- `@/platform-nodejs`
Expand Down
18 changes: 14 additions & 4 deletions packages/runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ To register a role, use `RolesService.registerRole(roleRegistration)`. This is u

## Configuring enabled roles

When an Alterior app is bootstrapped, the `ALT_ROLES_ONLY`/`ALT_ROLES_ALL_EXCEPT` environment variables are inspected to determine which roles should be started when the application starts. The variables are comma-delimited lists of role `identifiers` that should be started or ignored. By default all registered roles are started. If both variables are specified, `ALT_ROLES_ONLY` takes precedence.
When an Alterior app is bootstrapped, several environment variables are inspected to determine which roles should be
enabled by default. They are checked in the following order (the first one found wins).

Alternatively you can specify roles via the command line when the application is started using one of the following options:
- `ALT_ROLES_ONLY` - Enable only the given roles
- `ALT_ROLES_ALL_EXCEPT` - Enable all roles except those listed (including roles that are disabled by default)
- `ALT_ROLES_DEFAULT_EXCEPT` - Enable all default-enabled roles except those listed

The variables are comma-delimited lists of role `identifiers` that should be started or ignored. By default all
registered roles which are configured as enabled by default are started.

Alternatively you can specify roles via the command line when the application is started using one of the following
options:

```
--roles-only, -r [role,...] Enable only the specified roles
--roles-except, -R [role,...] Enable all roles except the specified roles
--roles-only, -r [role,...] Enable only the specified roles
--roles-skip, -x [role,...] Enable all default-enabled roles except those specified
--all-roles-except, -R [role,...] Enable all roles except the specified roles (regardless of default status)
```

For example, to enable only the `web-server` and `tasks` roles:
Expand Down
18 changes: 18 additions & 0 deletions packages/runtime/src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export class Runtime {
;

rolesService.configure({ mode: 'all-except', roles });
} else if (environment.get<any>().ALT_ROLES_DEFAULT_EXCEPT) {
let value = environment.get<any>().ALT_ROLES_DEFAULT_EXCEPT;
let roles = value.split(',')
.map(x => allRoles.find(y => y.identifier == x))
.map(x => x.class)
;

rolesService.configure({ mode: 'default-except', roles });
}

if (typeof process !== 'undefined' && process.argv) {
Expand Down Expand Up @@ -143,6 +151,16 @@ export class Runtime {
;

rolesService.configure({ mode: 'only', roles });
} else if (arg == '-r' || arg == '--roles-skip') {
let value = getArgumentValue();

let roles = value.split(',')
.map(x => allRoles.find(y => y.identifier == x))
.filter(x => x)
.map(x => x.class)
;

rolesService.configure({ mode: 'default-except', roles });
} else if (arg == '-R' || arg == '--roles-all-except') {
let value = getArgumentValue();

Expand Down
18 changes: 14 additions & 4 deletions packages/runtime/src/roles.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from "@alterior/di";
import { timeout, InvalidOperationError, ArgumentError } from "@alterior/common";

const SUPPORTED_ROLE_MODES = ['all-except', 'only' ];
export type RoleConfigurationMode = 'all-except' | 'only' ;
const SUPPORTED_ROLE_MODES = [ 'default', 'default-except', 'all-except', 'only' ];
export type RoleConfigurationMode = 'default' | 'default-except' | 'all-except' | 'only' ;

export interface RoleConfiguration {
mode : RoleConfigurationMode;
Expand All @@ -19,6 +19,12 @@ export interface RoleRegistration {
* called from an Alterior module's `altOnInit()` method.
*/
instance : any;

/**
* Set to false to cause this role to be disabled unless explicitly asked for. When unspecified, the default is
* true.
*/
enabledByDefault?: boolean;

/**
* The identifier that will be matched when interpreting command line role enablements.
Expand Down Expand Up @@ -65,7 +71,7 @@ export class RolesService {
}

_activeRoles : any[] = null;
_configuration : RoleConfiguration = { mode: 'all-except', roles: [] };
_configuration : RoleConfiguration = { mode: 'default', roles: [] };
_roles : RoleState[] = [];

get configuration() : RoleConfiguration {
Expand Down Expand Up @@ -97,8 +103,12 @@ export class RolesService {
get effectiveRoles(): RoleState[] {
let config = this._configuration;

if (config.mode == 'all-except')
if (config.mode === 'default')
return this._roles.filter(x => x.enabledByDefault !== false);
else if (config.mode == 'all-except')
return this._roles.filter(x => !config.roles.includes(x.class));
else if (config.mode == 'default-except')
return this._roles.filter(x => x.enabledByDefault !== false).filter(x => !config.roles.includes(x.class));
else if (config.mode == 'only')
return this._roles.filter(x => config.roles.includes(x.class));

Expand Down

0 comments on commit fbddaa4

Please sign in to comment.