File size: 13,267 Bytes
abb8f61
 
 
6f1215f
abb8f61
a69dd77
 
 
 
abb8f61
 
 
 
 
 
 
 
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
 
 
 
 
 
6f1215f
abb8f61
6f1215f
abb8f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
6f1215f
abb8f61
6f1215f
 
 
abb8f61
6f1215f
abb8f61
 
 
 
6f1215f
abb8f61
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
 
 
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
 
 
6f1215f
abb8f61
6f1215f
abb8f61
 
 
 
 
 
 
 
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
6f1215f
 
 
 
 
abb8f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f1215f
 
abb8f61
 
 
 
6f1215f
abb8f61
6f1215f
 
abb8f61
 
 
 
 
6f1215f
abb8f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69dd77
 
 
 
 
 
abb8f61
 
 
 
 
 
6f1215f
abb8f61
 
 
 
 
a69dd77
 
 
 
 
 
 
 
 
 
 
 
abb8f61
a69dd77
6f1215f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69dd77
abb8f61
23b78bf
 
 
 
 
 
 
 
a69dd77
 
abb8f61
 
 
a69dd77
 
 
abb8f61
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
 * src/services/JobQueue.ts
 *
 * Job routing layer for titan_builder_queue.
 *
 * Changes from v2:
 *   - handleWebView: ูŠู‚ุฑุฃ buildType + manifestUrl ู…ู† Firestore
 *     ูˆูŠู…ุฑุฑู‡ู… ู„ู€ triggerWebViewBuild
 *   - ู„ูˆ buildType = pwa_twa ุฃูˆ twa_native ูŠุณุฌู„ ุฐู„ูƒ ููŠ ุงู„ู„ูˆุฌ
 */

import admin from 'firebase-admin';
import { logger } from '../utils/logger';
import {
  FirebaseManager,
  QUEUE_COLLECTION,
  type QueueDoc,
  type TranspilerJob,
} from '../managers/FirebaseManager';
import { LLMOrchestrator } from './llmOrchestrator';
import { GitHubActionsService } from './githubActions';
import { hfStorage } from './hfStorage';

// โ”€โ”€โ”€ Job Queue Service โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

export class JobQueue {
  private readonly firebase: FirebaseManager;
  private readonly orchestrator: LLMOrchestrator;
  private readonly github: GitHubActionsService;
  private readonly inFlight = new Set<string>();
  private stopFn: (() => void) | null = null;

  constructor() {
    this.firebase     = FirebaseManager.getInstance();
    this.orchestrator = new LLMOrchestrator();
    this.github       = new GitHubActionsService();
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Lifecycle
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  start(): () => void {
    const db    = (this.firebase as unknown as { db: admin.firestore.Firestore }).db
                  ?? admin.firestore();
    const query = db
      .collection(QUEUE_COLLECTION)
      .where('status', '==', 'pending')
      .orderBy('createdAt', 'asc');

    const unsubscribe = query.onSnapshot(
      async (snapshot) => {
        for (const change of snapshot.docChanges()) {
          if (change.type !== 'added' && change.type !== 'modified') continue;

          const doc  = change.doc;
          const data = doc.data() as Omit<QueueDoc, 'id'>;
          if (data.status !== 'pending') continue;

          const job: QueueDoc = { id: doc.id, ...data };
          if (this.inFlight.has(job.id)) continue;

          const claimed = await this.claimJob(doc.ref);
          if (!claimed) continue;

          this.inFlight.add(job.id);
          logger.info(`JobQueue: claimed job ${job.id} (type="${job.type}")`);

          this.dispatch(job)
            .catch(err =>
              logger.error(`JobQueue: unhandled error for job ${job.id}`, err)
            )
            .finally(() => this.inFlight.delete(job.id));
        }
      },
      (err) => {
        logger.error('JobQueue: Firestore listener error', err);
      }
    );

    this.stopFn = unsubscribe;
    logger.info(`JobQueue: โœ… listening on "${QUEUE_COLLECTION}"`);
    return unsubscribe;
  }

  stop(): void {
    this.stopFn?.();
    this.stopFn = null;
    logger.info('JobQueue: listener stopped');
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Distributed Claim
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  private async claimJob(
    docRef: admin.firestore.DocumentReference
  ): Promise<boolean> {
    try {
      await admin.firestore().runTransaction(async (tx) => {
        const snap = await tx.get(docRef);
        if (!snap.exists) throw new Error('Job disappeared');
        const status = (snap.data() as Omit<QueueDoc, 'id'>).status;
        if (status !== 'pending') throw new Error(`Already claimed: ${status}`);
        tx.update(docRef, {
          status:    'processing',
          updatedAt: admin.firestore.Timestamp.now(),
        });
      });
      return true;
    } catch {
      return false;
    }
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Dispatch
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  private async dispatch(job: QueueDoc): Promise<void> {
    try {
      switch (job.type) {
        case 'transpile': await this.handleTranspile(job); break;
        case 'build':     await this.handleBuild(job);     break;
        case 'webview':   await this.handleWebView(job);   break;
        default: {
          const unknown = (job as QueueDoc).type;
          logger.warn(`JobQueue: unknown type "${unknown}" for job ${job.id}`);
          await this.firebase.updateQueueDoc(job.id, {
            status:       'failed',
            errorMessage: `Unknown job type: "${unknown}"`,
          });
        }
      }
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      logger.error(`JobQueue: top-level dispatch error for job ${job.id} โ€” ${msg}`);
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: msg,
      }).catch(() => undefined);
    }
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Handler: 'transpile'
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  private async handleTranspile(job: QueueDoc): Promise<void> {
    logger.info(`JobQueue[transpile]: starting job ${job.id}`);

    if (!job.zipUrl) {
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: 'transpile job is missing required field: zipUrl',
      });
      return;
    }

    const transpilerJob: TranspilerJob = {
      id:        job.id,
      zipUrl:    job.zipUrl,
      appName:   job.appName,
      status:    'processing',
      createdAt: job.createdAt ?? admin.firestore.Timestamp.now(),
    };

    await this.firebase.createTranspilerJobDoc(transpilerJob);
    await this.orchestrator.processJob(transpilerJob);

    const result = await this.firebase.getJob(job.id);

    if (result?.status === 'completed' && result.outputUrl) {
      await this.firebase.updateQueueDoc(job.id, {
        status:       'completed',
        resultZipUrl: result.outputUrl,
      });
      logger.info(`JobQueue[transpile]: โœ… completed โ€” job ${job.id}`);
    } else {
      const reason = result?.errorMessage ?? 'Pipeline ended without an output URL';
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: reason,
      });
      logger.warn(`JobQueue[transpile]: โŒ failed โ€” job ${job.id}: ${reason}`);
    }
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Handler: 'build'
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  /**
   * The ZIP arrives as a base64 string in job.zipBase64.
   * No Firebase Storage needed.
   */
  private async handleBuild(job: QueueDoc): Promise<void> {
    logger.info(`JobQueue[build]: starting job ${job.id}`);

    if (!job.zipBase64) {
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: 'build job is missing required field: zipBase64',
      });
      return;
    }

    try {
      const buffer = Buffer.from(job.zipBase64, 'base64');
      await this.github.triggerBuild(buffer, job.id);
      await this.firebase.updateQueueDoc(job.id, { status: 'building' });
      logger.info(`JobQueue[build]: โœ… GitHub build triggered โ€” job ${job.id}`);
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      logger.error(`JobQueue[build]: โŒ failed โ€” job ${job.id}: ${msg}`);
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: msg,
      });
    }
  }

  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
  //  Handler: 'webview'
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

  /**
   * Handles webview / pwa_twa / twa_native jobs.
   *
   * Changes v3:
   *   - ูŠู‚ุฑุฃ buildType + manifestUrl ู…ู† ุงู„ู€ Firestore doc
   *   - ูŠุณุฌู„ ููŠ ุงู„ู„ูˆุฌ ู„ูˆ ูƒุงู† TWA build
   *   - ูŠู…ุฑุฑ buildType + manifestUrl ู„ู€ triggerWebViewBuild
   */
  private async handleWebView(job: QueueDoc): Promise<void> {
    logger.info(`JobQueue[webview]: starting job ${job.id}`);

    if (!job.url || !job.appName) {
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: 'webview job is missing required fields: url, appName',
      });
      return;
    }

    // โ”€โ”€ ู‚ุฑุงุกุฉ buildType + manifestUrl โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    const buildType   = job.buildType   ?? 'webview';
    const manifestUrl = job.manifestUrl ?? '';

    // โ”€โ”€ ุชุณุฌูŠู„ ุฎุงุต ู„ู„ู€ TWA builds โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    if (buildType === 'pwa_twa' || buildType === 'twa_native') {
      logger.info(
        `JobQueue[webview]: ๐Ÿ”ท TWA build detected โ€” ` +
        `buildType="${buildType}", manifestUrl="${manifestUrl}", job=${job.id}`
      );
    }

    try {
      // โ”€โ”€ ุฑูุน ุงู„ุฃูŠู‚ูˆู†ุฉ ุนู„ู‰ HF Dataset ู„ูˆ ู…ูˆุฌูˆุฏุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
      let finalIconUrl = job.iconUrl || '';

      if (job.iconBase64 && job.iconMime) {
        logger.info(`JobQueue[webview]: uploading icon for job ${job.id}`);
        const ext      = job.iconMime.split('/')[1] || 'png';
        const filename = `icons/${job.id}/icon.${ext}`;
        const buf      = Buffer.from(job.iconBase64, 'base64');
        finalIconUrl   = await hfStorage.uploadBuffer(buf, filename, job.iconMime);
        logger.info(`JobQueue[webview]: icon uploaded โ†’ ${finalIconUrl}`);
      } else if (job.iconUrl && job.iconUrl.startsWith('http')) {
        try {
          finalIconUrl = await hfStorage.uploadIconFromUrl(job.iconUrl, job.id);
        } catch {
          logger.warn(`JobQueue[webview]: icon re-upload failed, using original URL`);
          finalIconUrl = job.iconUrl;
        }
      }

      // โ”€โ”€ Trigger GitHub Actions โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
      await this.github.triggerWebViewBuild({
        url:                  job.url,
        appName:              job.appName,
        iconUrl:              finalIconUrl,
        trackingId:           job.id,
        enableCamera:         job.enableCamera         ?? true,
        enableStorage:        job.enableStorage        ?? true,
        enableLocation:       job.enableLocation       ?? false,
        enableNotifications:  job.enableNotifications  ?? false,
        buildType,    // โ† ุฌุฏูŠุฏ: ูŠูู…ุฑูŽู‘ุฑ ู…ู† Firestore
        manifestUrl,  // โ† ุฌุฏูŠุฏ: ูŠูู…ุฑูŽู‘ุฑ ู…ู† Firestore
      });

      await this.firebase.updateQueueDoc(job.id, { status: 'building' });
      logger.info(
        `JobQueue[webview]: โœ… GitHub ${buildType} build triggered โ€” job ${job.id}`
      );

    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      logger.error(`JobQueue[webview]: โŒ failed โ€” job ${job.id}: ${msg}`);
      await this.firebase.updateQueueDoc(job.id, {
        status:       'failed',
        errorMessage: msg,
      });
    }
  }
}