10
10
*/
11
11
12
12
import { db , mirrorJobs } from "../src/lib/db" ;
13
- import { eq } from "drizzle-orm" ;
13
+ import { eq , and } from "drizzle-orm" ;
14
14
15
15
// Parse command line arguments
16
16
const args = process . argv . slice ( 2 ) ;
@@ -21,18 +21,19 @@ async function fixInterruptedJobs() {
21
21
console . log ( "Checking for interrupted jobs..." ) ;
22
22
23
23
// Build the query
24
- let query = db
25
- . select ( )
26
- . from ( mirrorJobs )
27
- . where ( eq ( mirrorJobs . inProgress , true ) ) ;
24
+ const whereConditions = userId
25
+ ? and ( eq ( mirrorJobs . inProgress , true ) , eq ( mirrorJobs . userId , userId ) )
26
+ : eq ( mirrorJobs . inProgress , true ) ;
28
27
29
28
if ( userId ) {
30
29
console . log ( `Filtering for user: ${ userId } ` ) ;
31
- query = query . where ( eq ( mirrorJobs . userId , userId ) ) ;
32
30
}
33
31
34
32
// Find all in-progress jobs
35
- const inProgressJobs = await query ;
33
+ const inProgressJobs = await db
34
+ . select ( )
35
+ . from ( mirrorJobs )
36
+ . where ( whereConditions ) ;
36
37
37
38
if ( inProgressJobs . length === 0 ) {
38
39
console . log ( "No interrupted jobs found." ) ;
@@ -45,21 +46,15 @@ async function fixInterruptedJobs() {
45
46
} ) ;
46
47
47
48
// Mark all in-progress jobs as failed
48
- let updateQuery = db
49
+ await db
49
50
. update ( mirrorJobs )
50
51
. set ( {
51
52
inProgress : false ,
52
53
completedAt : new Date ( ) ,
53
54
status : "failed" ,
54
55
message : "Job interrupted and marked as failed by cleanup script"
55
56
} )
56
- . where ( eq ( mirrorJobs . inProgress , true ) ) ;
57
-
58
- if ( userId ) {
59
- updateQuery = updateQuery . where ( eq ( mirrorJobs . userId , userId ) ) ;
60
- }
61
-
62
- await updateQuery ;
57
+ . where ( whereConditions ) ;
63
58
64
59
console . log ( `✅ Successfully marked ${ inProgressJobs . length } interrupted jobs as failed.` ) ;
65
60
console . log ( "These jobs can now be deleted through the normal cleanup process." ) ;
0 commit comments