@@ -2,6 +2,8 @@ import { mkdirSync } from 'fs'
22import type { RedisClientType } from 'redis'
33import { RedisMemoryServer } from 'redis-memory-server'
44import type { RedisMemoryInstancePropT } from 'redis-memory-server/lib/types'
5+ import { ensureBuildTools } from '../utils/check-build-tools'
6+ import { internalLogger } from '../utils/internal-logger'
57import type { RedisConnectionInfo } from './types'
68
79export class RedisMemoryManager {
@@ -25,6 +27,9 @@ export class RedisMemoryManager {
2527 async startServer ( baseDir : string ) : Promise < RedisConnectionInfo > {
2628 if ( ! this . server ) {
2729 try {
30+ // Check for required build tools before attempting to start
31+ await ensureBuildTools ( )
32+
2833 mkdirSync ( baseDir , { recursive : true } )
2934
3035 const instance : RedisMemoryInstancePropT = {
@@ -36,27 +41,61 @@ export class RedisMemoryManager {
3641 }
3742
3843 this . server = new RedisMemoryServer ( { instance } )
39- console . log ( 'Redis Memory Server started' )
40- this . running = true
4144 this . registerCleanupHandlers ( )
4245 } catch ( error ) {
43- console . error ( '[Redis Memory Server] Failed to start:' , error )
46+ internalLogger . error ( 'Failed to initialize Redis Memory Server' )
47+ if ( error instanceof Error ) {
48+ console . error ( error . message )
49+ if ( process . env . LOG_LEVEL === 'debug' && error . stack ) {
50+ console . error ( '\nStack trace:' )
51+ console . error ( error . stack )
52+ }
53+ }
4454 throw error
4555 }
4656 }
4757
48- const host = await this . server . getHost ( )
49- const port = await this . server . getPort ( )
58+ try {
59+ const host = await this . server . getHost ( )
60+ const port = await this . server . getPort ( )
61+
62+ this . running = true
63+ internalLogger . info ( 'Redis Memory Server started' , `${ host } :${ port } ` )
64+
65+ return { host, port }
66+ } catch ( error ) {
67+ internalLogger . error ( 'Failed to start Redis Memory Server' )
68+
69+ if ( error instanceof Error ) {
70+ console . error ( error . message )
71+
72+ // Provide helpful suggestions based on common error patterns
73+ if ( error . message . includes ( 'make' ) || error . message . includes ( 'compile' ) ) {
74+ console . error ( '\nThis error typically occurs when build tools are missing.' )
75+ console . error ( 'Please ensure you have "make" and a C compiler installed.' )
76+ }
77+
78+ if ( process . env . LOG_LEVEL === 'debug' && error . stack ) {
79+ console . error ( '\nStack trace:' )
80+ console . error ( error . stack )
81+ }
82+ }
83+
84+ console . error ( '\nAlternative: Use an external Redis server' )
85+ console . error ( ' Set MOTIA_DISABLE_MEMORY_SERVER=true' )
86+ console . error ( ' Set MOTIA_REDIS_HOST=<your-redis-host>' )
87+ console . error ( ' Set MOTIA_REDIS_PORT=<your-redis-port> (default: 6379)' )
5088
51- return { host, port }
89+ throw error
90+ }
5291 }
5392
5493 async stop ( ) : Promise < void > {
5594 if ( this . server && this . running ) {
5695 try {
5796 await this . server . stop ( )
5897 } catch ( error : unknown ) {
59- console . error ( '[ Redis Memory Server] Error stopping: ' , ( error as Error ) ?. message )
98+ internalLogger . error ( 'Error stopping Redis Memory Server' , ( error as Error ) ?. message )
6099 } finally {
61100 this . running = false
62101 this . server = null
0 commit comments