@@ -13,6 +13,7 @@ import { rename, readdir } from "fs";
1313import { promisify } from "util" ;
1414import Repository from "./Repository" ;
1515import { Handle } from "../domain/Handle" ;
16+ import { newLogger } from "../utilities/Logging" ;
1617import { getChecksumFromFile } from "../utilities/Checksum" ;
1718import { Coprocessor , PolicyInjection } from "../public/Coprocessor" ;
1819import { Script_ManagerClient as ManagementClient } from "../rpc/serverAndClients/server" ;
@@ -24,6 +25,8 @@ import {
2425 validateEnableResponseCode ,
2526} from "./HandleError" ;
2627
28+ const logger = newLogger ( "FileManager" ) ;
29+
2730/**
2831 * FileManager class is an inotify implementation, it receives a
2932 * Repository and updates this object when to add a new file in
@@ -46,8 +49,8 @@ class FileManager {
4649 . then ( ( ) => this . readCoprocessorFolder ( repository , this . submitDir ) )
4750 . then ( ( ) => this . startWatchers ( repository ) ) ;
4851 } catch ( e ) {
49- console . error ( e ) ;
50- //TODO: implement winston for loggin information and error handler
52+ // TODO: This should be a FATAL error
53+ logger . error ( `Failed to register watch(es): ${ e . message } ` ) ;
5154 }
5255 }
5356
@@ -73,7 +76,7 @@ class FileManager {
7376 return this . moveCoprocessorFile ( prevHandle , this . inactiveDir )
7477 . then ( ( ) =>
7578 this . deregisterCoprocessor ( prevHandle . coprocessor ) . catch ( ( e ) => {
76- console . error ( e . message ) ;
79+ logger . error ( e . message ) ;
7780 return Promise . resolve ( ) ;
7881 } )
7982 )
@@ -130,7 +133,7 @@ class FileManager {
130133 path . join ( folder , file ) ,
131134 repository ,
132135 validatePrevExist
133- ) . catch ( ( e ) => console . error ( e . message ) )
136+ ) . catch ( ( e ) => logger . error ( e . message ) )
134137 ) ;
135138 } ) ;
136139 //TODO: implement winston for loggin information and error handler
@@ -150,22 +153,23 @@ class FileManager {
150153 const id = hash64 ( Buffer . from ( name ) , 0 ) . readBigUInt64LE ( ) ;
151154 const [ handle ] = repository . getHandlesByCoprocessorIds ( [ id ] ) ;
152155 if ( ! handle ) {
153- console . error (
154- "error: trying to disable a removed coprocessor from " +
155- "'active' folder but it wasn't loaded in memory, file name " +
156- `${ name } .js`
156+ logger . error (
157+ `Trying to disable a removed coprocessor from 'active' folder but it` +
158+ `wasn't loaded in memory, file name: ${ name } .js`
157159 ) ;
158160 return Promise . resolve ( ) ;
159161 } else {
160162 return this . disableCoprocessors ( [ handle . coprocessor ] )
161163 . then ( ( ) => this . repository . remove ( handle ) )
162- . then ( ( ) =>
163- console . log (
164+ . then ( ( ) => {
165+ logger . info (
164166 `disabled coprocessor: ID ${ handle . coprocessor . globalId } ` +
165167 `filename: '${ name } .js'`
166- )
167- )
168- . catch ( ( error ) => console . log ( error . message ) ) ;
168+ ) ;
169+ } )
170+ . catch ( ( error ) => {
171+ logger . error ( error . message ) ;
172+ } ) ;
169173 }
170174 }
171175
@@ -221,12 +225,20 @@ class FileManager {
221225 * @param coprocessors
222226 */
223227 disableCoprocessors ( coprocessors : Coprocessor [ ] ) : Promise < void > {
228+ logger . info (
229+ `Initiating RPC call to redpandas coprocessor service at endpoint - ` +
230+ `disable_coprocessors with data: ${ coprocessors } `
231+ ) ;
224232 return this . managementClient
225233 . disable_copros ( { inputs : coprocessors . map ( ( coproc ) => coproc . globalId ) } )
226234 . then ( ( response ) => {
227235 const errors = validateDisableResponseCode ( response , coprocessors ) ;
228236 if ( errors . length > 0 ) {
229- return Promise . reject ( this . compactErrors ( [ errors ] ) ) ;
237+ const compactedErrors = this . compactErrors ( [ errors ] ) ;
238+ logger . error (
239+ `disable_coprocessors RPC returned with errors: ${ compactedErrors } `
240+ ) ;
241+ return Promise . reject ( compactedErrors ) ;
230242 } else {
231243 return Promise . resolve ( ) ;
232244 }
@@ -256,6 +268,10 @@ class FileManager {
256268 if ( coprocessors . length == 0 ) {
257269 return Promise . resolve ( ) ;
258270 } else {
271+ logger . info (
272+ `Initiating RPC call to redpandas coprocessor service at endpoint - ` +
273+ `enable_coprocessors with data: ${ coprocessors } `
274+ ) ;
259275 return this . managementClient
260276 . enable_copros ( {
261277 coprocessors : coprocessors . map ( ( coproc ) => ( {
@@ -282,7 +298,12 @@ class FileManager {
282298 coprocessors
283299 ) ;
284300 if ( errors . find ( ( errors ) => errors . length > 0 ) ) {
285- return Promise . reject ( this . compactErrors ( errors ) ) ;
301+ const compactedErrors = this . compactErrors ( errors ) ;
302+ logger . error (
303+ `enable_coprocessors RPC returned with some error: ` +
304+ `${ compactedErrors } `
305+ ) ;
306+ return Promise . reject ( compactedErrors ) ;
286307 } else {
287308 return Promise . resolve ( ) ;
288309 }
@@ -363,13 +384,15 @@ class FileManager {
363384 */
364385 private startWatchers ( repository : Repository ) : void {
365386 this . submitDirWatcher = watch ( this . submitDir ) . on ( "add" , ( filePath ) => {
366- this . addCoprocessor ( filePath , repository ) . catch ( ( e ) =>
367- console . error ( e . message )
368- ) ;
387+ logger . info ( `Detected new file in submit dir: ${ filePath } ` ) ;
388+ this . addCoprocessor ( filePath , repository ) . catch ( ( e ) => {
389+ logger . error ( `addCoprocessor failed with exception: ${ e . message } ` ) ;
390+ } ) ;
391+ } ) ;
392+ this . activeDirWatcher = watch ( this . activeDir ) . on ( "unlink" , ( filePath ) => {
393+ logger . info ( `Detected removed file from active dir: ${ filePath } ` ) ;
394+ this . removeHandleFromFilePath ( filePath , repository ) ;
369395 } ) ;
370- this . activeDirWatcher = watch ( this . activeDir ) . on ( "unlink" , ( filePath ) =>
371- this . removeHandleFromFilePath ( filePath , repository )
372- ) ;
373396 }
374397}
375398
0 commit comments