@@ -4,6 +4,7 @@ import type { PluginOptions } from './types.js';
44import { afLogger } from "adminforth" ;
55import pLimit from 'p-limit' ;
66import { Level } from 'level' ;
7+ import fs from 'fs/promises' ;
78
89type TaskStatus = 'SCHEDULED' | 'IN_PROGRESS' | 'DONE' | 'FAILED' ;
910type setStateFieldParams = ( state : Record < string , any > ) => void ;
@@ -48,6 +49,35 @@ export default class extends AdminForthPlugin {
4849 pluginInstanceId : this . pluginInstanceId ,
4950 }
5051 } ) ;
52+
53+ if ( ! this . resourceConfig . hooks ) {
54+ this . resourceConfig . hooks = { } ;
55+ }
56+ if ( ! this . resourceConfig . hooks . delete ) {
57+ this . resourceConfig . hooks . delete = { } ;
58+ }
59+ if ( ! this . resourceConfig . hooks . delete . beforeSave ) {
60+ this . resourceConfig . hooks . delete . beforeSave = [ ] ;
61+ }
62+ this . resourceConfig . hooks . delete . beforeSave . push ( async ( { record, recordId} : { record : any , recordId : any } ) => {
63+
64+ const levelDbPath = `${ this . options . levelDbPath || './background-jobs-dbs/' } job_${ recordId } ` ;
65+ const jobLevelDb = this . levelDbInstances [ recordId ] ;
66+
67+ //close level db instance if it's open and delete the level db folder for the job
68+ if ( jobLevelDb ) {
69+ await jobLevelDb . close ( ) ;
70+ delete this . levelDbInstances [ recordId ] ;
71+ }
72+
73+ //delete level db folder for the job
74+ await fs . rm ( levelDbPath , {
75+ recursive : true ,
76+ force : true ,
77+ } ) ;
78+
79+ return { ok : true } ;
80+ } )
5181 }
5282
5383 private checkIfFieldInResource ( resourceConfig : AdminForthResource , fieldName : string , fieldString ?: string ) {
@@ -198,6 +228,7 @@ export default class extends AdminForthPlugin {
198228
199229 //define the setTaskStateField and getTaskStateField functions to pass to the task
200230 const setTaskStateField = async ( state : Record < string , any > ) => {
231+ this . adminforth . websocket . publish ( `/background-jobs-task-update/${ jobId } ` , { taskIndex, state } ) ;
201232 await this . setLevelDbTaskStateField ( jobLevelDb , taskIndex . toString ( ) , state ) ;
202233 }
203234 const getTaskStateField = async ( ) => {
@@ -242,11 +273,13 @@ export default class extends AdminForthPlugin {
242273 if ( lastJobStatus !== 'CANCELLED' && failedTasks === 0 ) {
243274 await this . adminforth . resource ( this . getResourceId ( ) ) . update ( jobId , {
244275 [ this . options . statusField ] : 'DONE' ,
276+ [ this . options . finishedAtField ] : ( new Date ( ) ) . toISOString ( ) ,
245277 } )
246278 this . adminforth . websocket . publish ( '/background-jobs' , { jobId, status : 'DONE' } ) ;
247279 } else if ( failedTasks > 0 ) {
248280 await this . adminforth . resource ( this . getResourceId ( ) ) . update ( jobId , {
249281 [ this . options . statusField ] : 'DONE_WITH_ERRORS' ,
282+ [ this . options . finishedAtField ] : ( new Date ( ) ) . toISOString ( ) ,
250283 } )
251284 this . adminforth . websocket . publish ( '/background-jobs' , { jobId, status : 'DONE_WITH_ERRORS' } ) ;
252285 }
@@ -349,11 +382,13 @@ export default class extends AdminForthPlugin {
349382 async validateConfigAfterDiscover ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
350383 // optional method where you can safely check field types after database discovery was performed
351384 this . checkIfFieldInResource ( resourceConfig , this . options . createdAtField , 'createdAtField' ) ;
385+ this . checkIfFieldInResource ( resourceConfig , this . options . finishedAtField , 'finishedAtField' ) ;
352386 this . checkIfFieldInResource ( resourceConfig , this . options . startedByField , 'startedByField' ) ;
353387 this . checkIfFieldInResource ( resourceConfig , this . options . stateField , 'stateField' ) ;
354388 this . checkIfFieldInResource ( resourceConfig , this . options . progressField , 'progressField' ) ;
355389 this . checkIfFieldInResource ( resourceConfig , this . options . statusField , 'statusField' ) ;
356390 this . checkIfFieldInResource ( resourceConfig , this . options . nameField , 'nameField' ) ;
391+ this . checkIfFieldInResource ( resourceConfig , this . options . jobHandlerField , 'jobHandlerField' ) ;
357392
358393
359394 //Add temp delay to make sure, that all resources active. Probably should be fixed
@@ -402,6 +437,7 @@ export default class extends AdminForthPlugin {
402437 try {
403438 await this . adminforth . resource ( this . getResourceId ( ) ) . update ( jobId , {
404439 [ this . options . statusField ] : 'CANCELLED' ,
440+ [ this . options . finishedAtField ] : ( new Date ( ) ) . toISOString ( ) ,
405441 } ) ;
406442 this . adminforth . websocket . publish ( '/background-jobs' , {
407443 jobId,
@@ -426,6 +462,7 @@ export default class extends AdminForthPlugin {
426462 } else {
427463 try {
428464 jobLevelDb = new Level ( levelDbPath , { valueEncoding : 'json' } ) ;
465+ this . levelDbInstances [ jobId ] = jobLevelDb ;
429466 } catch ( error ) {
430467 return { ok : false , message : `Failed to access tasks for job with id ${ jobId } .` } ;
431468 }
0 commit comments