-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,19 @@ | ||
// export type ServiceEditHandle = ( | ||
// editType: string, | ||
// callback: (...args: any[]) => any | ||
// ) => Promise<void> | ||
export interface EditHandler { | ||
search(): void | ||
replace(): void | ||
delete(): void | ||
} | ||
export interface EditService { | ||
register(source: DataSource): Promise<EditServiceClient> | ||
activeUsers(): number | ||
} | ||
export interface EditServiceClient { | ||
close(): void | ||
id(): string | ||
request: (request: any) => any | ||
onDidProcessRequest: (response: any) => any | ||
onDidProcess: (response: any) => any | ||
} | ||
|
||
export interface DataSource {} | ||
export interface GetDataSourceStrategy { | ||
get(): Promise<DataSource> | ||
} | ||
export interface ServiceUser<S extends DataSource> { | ||
dataSource(): S | ||
// readonly getSource: GetDataSourceStrategy | ||
// readonly handler: EditHandler | ||
} // OmegaEdit implements this as Session type |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FilePath } from '..' | ||
import { IConfig } from '../../config' | ||
|
||
export class Connection { | ||
constructor( | ||
readonly host: string, | ||
readonly port: number | ||
) {} | ||
} | ||
|
||
export type GetServerConfigStrategy = () => Promise<ServerConfig> | ||
export class ServerConfig { | ||
readonly conn: Connection | ||
readonly logFile: FilePath | ||
readonly logLevel: string | ||
readonly checkpointPath: string | ||
|
||
constructor(config: () => IConfig) { | ||
const { checkpointPath, logFile, logLevel, port } = config() | ||
this.conn = new Connection('127.0.0.1', port) | ||
this.logFile = new FilePath(logFile) | ||
this.logLevel = logLevel | ||
this.checkpointPath = checkpointPath | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { getServerHeartbeat, IServerHeartbeat } from '@omega-edit/client' | ||
const InitialHeartbeat: IServerHeartbeat = { | ||
latency: 0, | ||
sessionCount: 0, | ||
serverTimestamp: 0, | ||
serverUptime: 0, | ||
serverCpuCount: 0, | ||
serverCpuLoadAverage: 0, | ||
serverMaxMemory: 0, | ||
serverCommittedMemory: 0, | ||
serverUsedMemory: 0, | ||
} | ||
type HeartbeatCallback = (heartbeat: IServerHeartbeat) => any | ||
let ActiveHeartbeat: Heartbeat | undefined | ||
const Receivers: HeartbeatCallback[] = [] | ||
|
||
export class Heartbeat { | ||
private readonly msInterval = 1000 | ||
private heartbeat: IServerHeartbeat = InitialHeartbeat | ||
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 17)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 8)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 17)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 11)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 8)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 11)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 11)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 17)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 8)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 11)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 17)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 17)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 8)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 8)
Check failure on line 19 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 11)
|
||
private retrieveInterval: NodeJS.Timeout | ||
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 17)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 8)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 17)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 18.20.1, Java: 11)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 8)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: ubuntu-20.04, Node: 16, Java: 11)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 11)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 17)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 16, Java: 8)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 11)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 17)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 17)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: macos-12, Node: 18.20.1, Java: 8)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 8)
Check failure on line 20 in src/dataEditor/omegaEdit/server/heartbeat.ts GitHub Actions / Build, Test, and Package (OS: windows-2019, Node: 18.20.1, Java: 11)
|
||
private constructor(getSessionIds: () => string[]) { | ||
this.retrieveInterval = setInterval(() => { | ||
getServerHeartbeat(getSessionIds(), 1000) | ||
}, this.msInterval) | ||
} | ||
static Start(getSessionIds: () => string[]) { | ||
if (ActiveHeartbeat) | ||
throw 'Server heartbeat is already active. Use `onReceive(...)` to do something with it' | ||
ActiveHeartbeat = new Heartbeat(getSessionIds) | ||
} | ||
static OnReceive(listener: (heartbeat: IServerHeartbeat) => any) { | ||
Receivers.push(listener) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { createSimpleFileLogger, setLogger } from '@omega-edit/client' | ||
import { ServerConfig } from './config' | ||
|
||
import * as fs from 'fs' | ||
import path from 'path' | ||
import XDGAppPaths from 'xdg-app-paths' | ||
export const APP_DATA_PATH: string = XDGAppPaths({ name: 'omega_edit' }).data() | ||
|
||
const MAX_LOG_FILES = 5 | ||
function rotateLogFiles(logFile: string): void { | ||
interface LogFile { | ||
path: string | ||
ctime: Date | ||
} | ||
|
||
// assert( | ||
// MAX_LOG_FILES > 0, | ||
// 'Maximum number of log files must be greater than 0' | ||
// ) | ||
|
||
if (fs.existsSync(logFile)) { | ||
const logDir = path.dirname(logFile) | ||
const logFileName = path.basename(logFile) | ||
|
||
// Get list of existing log files | ||
const logFiles: LogFile[] = fs | ||
.readdirSync(logDir) | ||
.filter((file) => file.startsWith(logFileName) && file !== logFileName) | ||
.map((file) => ({ | ||
path: path.join(logDir, file), | ||
ctime: fs.statSync(path.join(logDir, file)).ctime, | ||
})) | ||
.sort((a, b) => b.ctime.getTime() - a.ctime.getTime()) | ||
|
||
// Delete oldest log files if maximum number of log files is exceeded | ||
while (logFiles.length >= MAX_LOG_FILES) { | ||
const fileToDelete = logFiles.pop() as LogFile | ||
fs.unlinkSync(fileToDelete.path) | ||
} | ||
|
||
// Rename current log file with timestamp and create a new empty file | ||
const timestamp = new Date().toISOString().replace(/:/g, '-') | ||
fs.renameSync(logFile, path.join(logDir, `${logFileName}.${timestamp}`)) | ||
} | ||
} | ||
|
||
export function setupLogging(config: ServerConfig): Promise<void> { | ||
return new Promise((res, rej) => { | ||
const filePath = config.logFile.fullPath() | ||
const level = config.logLevel | ||
rotateLogFiles(filePath) | ||
setLogger(createSimpleFileLogger(filePath, level)) | ||
res() | ||
}) | ||
} | ||
|
||
export function generateLogbackConfigFile(server: ServerConfig): string { | ||
const serverLogFile = path.join(APP_DATA_PATH, `serv-${server.conn.port}.log`) | ||
const dirname = path.dirname(server.logFile.fullPath()) | ||
if (!fs.existsSync(dirname)) { | ||
fs.mkdirSync(dirname, { recursive: true }) | ||
} | ||
const logbackConfig = `<?xml version="1.0" encoding="UTF-8"?>\n | ||
<configuration> | ||
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> | ||
<file>${serverLogFile}</file> | ||
<encoder> | ||
<pattern>[%date{ISO8601}] [%level] [%logger] [%marker] [%thread] - %msg MDC: {%mdc}%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="${server.logLevel.toUpperCase()}"> | ||
<appender-ref ref="FILE" /> | ||
</root> | ||
</configuration> | ||
` | ||
const logbackConfigFile = path.join( | ||
APP_DATA_PATH, | ||
`serv-${server.conn.port}.logconf.xml` | ||
) | ||
rotateLogFiles(server.logFile.fullPath()) | ||
fs.writeFileSync(logbackConfigFile, logbackConfig) | ||
return logbackConfigFile // Return the path to the logback config file | ||
} |