@@ -53,9 +53,10 @@ export type DatabaseInstance = {
53
53
ended_on : string | null ;
54
54
} ;
55
55
56
+ const ENGINE_STATUS_KEY = "ENGINE_STATUS" ;
57
+
56
58
export class Engine extends DurableObject < Env > {
57
59
logs : Array < unknown > = [ ] ;
58
- status : InstanceStatus = InstanceStatus . Queued ;
59
60
60
61
isRunning : boolean = false ;
61
62
accountId : number | undefined ;
@@ -66,21 +67,32 @@ export class Engine extends DurableObject<Env> {
66
67
67
68
constructor ( state : DurableObjectState , env : Env ) {
68
69
super ( state , env ) ;
69
-
70
70
void this . ctx . blockConcurrencyWhile ( async ( ) => {
71
71
this . ctx . storage . transactionSync ( ( ) => {
72
- this . ctx . storage . sql . exec ( `
73
- CREATE TABLE IF NOT EXISTS priority_queue (
74
- id INTEGER PRIMARY KEY NOT NULL,
75
- created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
76
- target_timestamp INTEGER NOT NULL,
77
- action INTEGER NOT NULL, -- should only be 0 or 1 (1 for added, 0 for deleted),
78
- entryType INTEGER NOT NULL,
79
- hash TEXT NOT NULL,
80
- CHECK (action IN (0, 1)), -- guararentee that action can only be 0 or 1
81
- UNIQUE (action, entryType, hash)
82
- )
83
- ` ) ;
72
+ try {
73
+ this . ctx . storage . sql . exec ( `
74
+ CREATE TABLE IF NOT EXISTS priority_queue (
75
+ id INTEGER PRIMARY KEY NOT NULL,
76
+ created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
77
+ target_timestamp INTEGER NOT NULL,
78
+ action INTEGER NOT NULL, -- should only be 0 or 1 (1 for added, 0 for deleted),
79
+ entryType INTEGER NOT NULL,
80
+ hash TEXT NOT NULL,
81
+ CHECK (action IN (0, 1)), -- guararentee that action can only be 0 or 1
82
+ UNIQUE (action, entryType, hash)
83
+ );
84
+ CREATE TABLE IF NOT EXISTS states (
85
+ id INTEGER PRIMARY KEY NOT NULL,
86
+ groupKey TEXT,
87
+ target TEXT,
88
+ metadata TEXT,
89
+ event INTEGER NOT NULL
90
+ )
91
+ ` ) ;
92
+ } catch ( e ) {
93
+ console . error ( e ) ;
94
+ throw e ;
95
+ }
84
96
} ) ;
85
97
} ) ;
86
98
@@ -96,38 +108,55 @@ export class Engine extends DurableObject<Env> {
96
108
target : string | null = null ,
97
109
metadata : Record < string , unknown >
98
110
) {
99
- this . logs . push ( {
111
+ this . ctx . storage . sql . exec (
112
+ "INSERT INTO states (event, groupKey, target, metadata) VALUES (?, ?, ?, ?)" ,
100
113
event ,
101
114
group ,
102
115
target ,
103
- metadata,
104
- } ) ;
116
+ JSON . stringify ( metadata )
117
+ ) ;
105
118
}
106
119
107
120
readLogsFromStep ( _cacheKey : string ) : RawInstanceLog [ ] {
108
121
return [ ] ;
109
122
}
110
123
111
124
readLogs ( ) : InstanceLogsResponse {
125
+ const logs = [
126
+ ...this . ctx . storage . sql . exec < Record < string , string | number > > (
127
+ "SELECT event, groupKey, target, metadata FROM states"
128
+ ) ,
129
+ ] ;
130
+
112
131
return {
113
132
// @ts -expect-error TODO: Fix this
114
- logs : this . logs ,
133
+ logs : logs . map ( ( log ) => ( {
134
+ ...log ,
135
+ metadata : JSON . parse ( log . metadata as string ) ,
136
+ group : log . groupKey ,
137
+ } ) ) ,
115
138
} ;
116
139
}
117
140
118
141
async getStatus (
119
142
_accountId : number ,
120
143
_instanceId : string
121
144
) : Promise < InstanceStatus > {
122
- return this . status ;
145
+ const res = await this . ctx . storage . get < InstanceStatus > ( ENGINE_STATUS_KEY ) ;
146
+
147
+ // NOTE(lduarte): if status don't exist, means that engine is running for the first time, so we assume queued
148
+ if ( res === undefined ) {
149
+ return InstanceStatus . Queued ;
150
+ }
151
+ return res ;
123
152
}
124
153
125
154
async setStatus (
126
155
accountId : number ,
127
156
instanceId : string ,
128
157
status : InstanceStatus
129
158
) : Promise < void > {
130
- this . status = status ;
159
+ await this . ctx . storage . put ( ENGINE_STATUS_KEY , status ) ;
131
160
}
132
161
133
162
async abort ( _reason : string ) {
0 commit comments