-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstorage.ts
More file actions
111 lines (92 loc) · 3.27 KB
/
storage.ts
File metadata and controls
111 lines (92 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SVELTEKIT SUBSTITUTE: This file already uses Netlify Blobs correctly, minimal changes needed
// SVELTEKIT SUBSTITUTE: Fixed initialization to properly handle Netlify Functions environment
let jobStore: any = null
async function getJobStore() {
if (!jobStore) {
const { getStore } = await import('@netlify/blobs')
jobStore = getStore('email-jobs')
}
return jobStore
}
// Storage interface functions
export const JobStorage = {
// Create new job entry
async createJob(jobId: string, initialWriteState: any) {
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
const jobData = {
jobId,
status: 'pending',
writeState: initialWriteState,
createdAt: new Date().toISOString(),
error: null,
completedAt: null
}
await store.set(jobId, JSON.stringify(jobData))
return jobData
},
// Get job by ID
async getJob(jobId: string) {
try {
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
const data = await store.get(jobId)
if (!data) return null
return JSON.parse(data)
} catch (error) {
console.error('Error retrieving job:', error)
return null
}
},
// Update job status
async updateJobStatus(jobId: string, status: string, error: string | null = null) {
const jobData = await this.getJob(jobId)
if (!jobData) throw new Error(`Job ${jobId} not found`)
jobData.status = status
jobData.error = error
if (status === 'completed' || status === 'failed') {
jobData.completedAt = new Date().toISOString()
}
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
await store.set(jobId, JSON.stringify(jobData))
return jobData
},
// Update WriteState for job
async updateWriteState(jobId: string, newWriteState: any) {
const jobData = await this.getJob(jobId)
if (!jobData) throw new Error(`Job ${jobId} not found`)
jobData.writeState = newWriteState
jobData.status = 'processing'
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
await store.set(jobId, JSON.stringify(jobData))
return jobData
},
// Complete job with final results
async completeJob(jobId: string, finalWriteState: any) {
const jobData = await this.getJob(jobId)
if (!jobData) throw new Error(`Job ${jobId} not found`)
jobData.writeState = finalWriteState
jobData.status = 'completed'
jobData.completedAt = new Date().toISOString()
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
await store.set(jobId, JSON.stringify(jobData))
return jobData
},
// Mark job as failed
async failJob(jobId: string, errorMessage: string) {
return await this.updateJobStatus(jobId, 'failed', errorMessage)
},
// Delete job (cleanup)
async deleteJob(jobId: string) {
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
await store.delete(jobId)
},
// List all jobs (for debugging/admin)
async listJobs() {
const store = await getJobStore() // SVELTEKIT SUBSTITUTE: Ensure store is initialized
const { blobs } = await store.list()
return blobs.map((blob: any) => blob.key)
}
}
// Utility function to generate unique job IDs
export function generateJobId(): string {
return `job_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}