# ServiceManager

`com.jsmacrosce.jsmacros.core.service.ServiceManager`

**Since:** 1.6.3

Manages registered script services, their running contexts, and reloads triggered by file changes.

## Nested types

- [ServiceStatus](ServiceManager.ServiceStatus.md)

## Constructors

### new ServiceManager {#com.jsmacrosce.jsmacros.core.service.ServiceManager_constructor-Core-}

```js
new ServiceManager(Core<?, ?> runner)
```

Stores the core used for subsequent service operations; this constructor does not register or start services.

**Parameters:**
- `runner`: core that owns this manager

## Methods

### disableReload {#com.jsmacrosce.jsmacros.core.service.ServiceManager_disableReload-String-}

```js
disableReload(String serviceName): void
```

Removes cached crash and file-timestamp state for the registered service; this does not stop it.

**Parameters:**
- `serviceName`: the name of the service to disable the reload feature for

**Since:** 1.8.4

### disableService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_disableService-String-}

```js
disableService(String name): ServiceManager.ServiceStatus
```

Disables a registered service without stopping it.

**Parameters:**
- `name`: service name

**Returns:** [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unregistered; otherwise [ServiceStatus.ENABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_ENABLED) if this call disabled it, or [ServiceStatus.DISABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_DISABLED) if already disabled

### enableService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_enableService-String-}

```js
enableService(String name): ServiceManager.ServiceStatus
```

Enables a registered service without starting it.

**Parameters:**
- `name`: service name

**Returns:** [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unregistered; otherwise [ServiceStatus.DISABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_DISABLED) if this call enabled it, or [ServiceStatus.ENABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_ENABLED) if already enabled

### getServiceData {#com.jsmacrosce.jsmacros.core.service.ServiceManager_getServiceData-String-}

```js
getServiceData(String name): EventService
```

Returns the triggering event for a registered service's current context; the service must be running.

**Parameters:**
- `name`: registered service name

**Returns:** the service event

**Since:** 1.6.5

### getServices {#com.jsmacrosce.jsmacros.core.service.ServiceManager_getServices-}

```js
getServices(): Set<String>
```

**Returns:** an immutable snapshot of the registered service names

### getTrigger {#com.jsmacrosce.jsmacros.core.service.ServiceManager_getTrigger-String-}

```js
getTrigger(String name): ServiceTrigger
```

Returns the trigger configuration for a registered service.

**Parameters:**
- `name`: registered service name

**Returns:** the service trigger

**Since:** 1.6.5

**Note:** [named getServiceData previously]

### hasKeepAlive {#com.jsmacrosce.jsmacros.core.service.ServiceManager_hasKeepAlive-BaseScriptContext-}

```js
static hasKeepAlive(BaseScriptContext<?> ctx): boolean
```

Checks whether a service-triggered context's non-null sync object is in the shared keep-alive set.

**Parameters:**
- `ctx`: context to check

**Returns:** `true` only when the triggering event is an [EventService](../../../../../../events/com/jsmacrosce/jsmacros/core/service/EventService.md) and its non-null sync object is in the set

### isCrashed {#com.jsmacrosce.jsmacros.core.service.ServiceManager_isCrashed-String-}

```js
isCrashed(String serviceName): boolean
```

**Parameters:**
- `serviceName`: the name of the service to check

**Returns:** `true` if the service is marked crashed

**Since:** 1.8.4

### isEnabled {#com.jsmacrosce.jsmacros.core.service.ServiceManager_isEnabled-String-}

```js
isEnabled(String name): boolean
```

Returns whether a registered service is enabled.

**Parameters:**
- `name`: service name

**Returns:** `false` if unregistered or disabled; `true` if enabled

**Since:** 1.8.4

### isRunning {#com.jsmacrosce.jsmacros.core.service.ServiceManager_isRunning-String-}

```js
isRunning(String name): boolean
```

Returns whether the registered service's context is open, regardless of its enabled state.

**Parameters:**
- `name`: service name

**Returns:** `true` if registered with an open context; `false` otherwise

**Since:** 1.8.4

### load {#com.jsmacrosce.jsmacros.core.service.ServiceManager_load-}

```js
load(): void
```

Loads configured services, registering missing services and updating existing triggers.

### markCrashed {#com.jsmacrosce.jsmacros.core.service.ServiceManager_markCrashed-String-}

```js
markCrashed(String serviceName): void
```

Marks a service as crashed so the file-change listener can restart it after its file changes.

**Parameters:**
- `serviceName`: the name of the service to mark as crashed

**Since:** 1.8.4

### registerService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_registerService-String-String-}

```js
registerService(String name, String pathToFile): boolean
```

Registers a service from a file under the macro folder and starts it immediately.

**Parameters:**
- `name`: service name
- `pathToFile`: path relative to the macro folder

**Returns:** `false` if a service with this name is already registered

### registerService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_registerService-String-String-boolean-}

```js
registerService(String name, String pathToFile, boolean enabled): boolean
```

Registers a service from a file under the macro folder, optionally starting it immediately.

**Parameters:**
- `name`: service name
- `pathToFile`: path relative to the macro folder
- `enabled`: whether to start the service immediately

**Returns:** `false` if a service with this name is already registered

### registerService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_registerService-String-ServiceTrigger-}

```js
registerService(String name, ServiceTrigger trigger): boolean
```

Registers a service with the given trigger and starts it if the trigger is enabled.

**Parameters:**
- `name`: service name
- `trigger`: service trigger configuration

**Returns:** `false` if a service with this name is already registered

### renameService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_renameService-String-String-}

```js
renameService(String oldName, String newName): boolean
```

Renames a registered service, preserving its trigger and running context.

**Parameters:**
- `oldName`: current service name
- `newName`: replacement service name

**Returns:** `false` if the new name is already registered or the old name is unknown

### restartService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_restartService-String-}

```js
restartService(String name): ServiceManager.ServiceStatus
```

Stops the service, then starts it again if registered, even if disabled.

**Parameters:**
- `name`: service name

**Returns:** the result of stopping the service, or [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unregistered; a stopped service returns [ServiceStatus.STOPPED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_STOPPED) even though it is then started

### save {#com.jsmacrosce.jsmacros.core.service.ServiceManager_save-}

```js
save(): void
```

Saves registered service triggers to configuration and writes the configuration.

### setAutoUnregisterKeepAlive {#com.jsmacrosce.jsmacros.core.service.ServiceManager_setAutoUnregisterKeepAlive-BaseScriptContext-boolean-}

```js
static setAutoUnregisterKeepAlive(BaseScriptContext<?> ctx, boolean keepAlive): void
```

Adds or removes the context's sync object from the shared auto-unregister keep-alive set.

**Parameters:**
- `ctx`: context whose sync object is tracked
- `keepAlive`: `true` to add the sync object; `false` to remove it

### startReloadListener {#com.jsmacrosce.jsmacros.core.service.ServiceManager_startReloadListener-}

```js
startReloadListener(): void
```

Enables service reloads in response to file changes.

**Since:** 1.8.4

### startService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_startService-String-}

```js
startService(String name): ServiceManager.ServiceStatus
```

Starts a service if it is not already running.

**Parameters:**
- `name`: service name

**Returns:** [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unknown; otherwise [ServiceStatus.STOPPED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_STOPPED) if this call starts it, or [ServiceStatus.RUNNING](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_RUNNING) if it was already running

### status {#com.jsmacrosce.jsmacros.core.service.ServiceManager_status-String-}

```js
status(String name): ServiceManager.ServiceStatus
```

**Parameters:**
- `name`: service name

**Returns:** [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unregistered; otherwise [ServiceStatus.ENABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_ENABLED) or [ServiceStatus.STOPPED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_STOPPED) when enabled and respectively running or stopped, or [ServiceStatus.RUNNING](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_RUNNING) or [ServiceStatus.DISABLED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_DISABLED) when disabled and respectively running or stopped

### stopReloadListener {#com.jsmacrosce.jsmacros.core.service.ServiceManager_stopReloadListener-}

```js
stopReloadListener(): void
```

Disables service reloads in response to file changes.

**Since:** 1.8.4

### stopService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_stopService-String-}

```js
stopService(String name): ServiceManager.ServiceStatus
```

Stops a running service, invoking its stop callbacks and unregistering its event listeners and registrables.

**Parameters:**
- `name`: service name

**Returns:** [ServiceStatus.UNKNOWN](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_UNKNOWN) if unknown; otherwise [ServiceStatus.RUNNING](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_RUNNING) if this call stopped it, or [ServiceStatus.STOPPED](ServiceManager.ServiceStatus.md#com.jsmacrosce.jsmacros.core.service.ServiceManager.ServiceStatus_STOPPED) if it was already stopped

### tickReloadListener {#com.jsmacrosce.jsmacros.core.service.ServiceManager_tickReloadListener-}

```js
tickReloadListener(): void
```

Checks eligible services for file modification and restarts them when their file timestamp changes.

**Since:** 1.8.4

### unregisterService {#com.jsmacrosce.jsmacros.core.service.ServiceManager_unregisterService-String-}

```js
unregisterService(String name): boolean
```

Stops and unregisters a service if present.

**Parameters:**
- `name`: service name

**Returns:** `true` if a service was registered and removed
