@@ -9,7 +9,73 @@ const ALLOWED_STATUSES = [200, 201];
99const REQUEST_TIMEOUT = 10000 ;
1010const MIN_VIEWPORT_HEIGHT = 1080 ;
1111
12- export default async ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > => {
12+ export default class Queue {
13+ private snapshots : Array < Snapshot > = [ ] ;
14+ private processedSnapshots : Array < Record < string , any > > = [ ] ;
15+ private processing : boolean = false ;
16+ private processingSnapshot : string = '' ;
17+ private ctx : Context ;
18+
19+ constructor ( ctx : Context ) {
20+ this . ctx = ctx ;
21+ }
22+
23+ enqueue ( item : Snapshot ) : void {
24+ this . snapshots . push ( item ) ;
25+ if ( ! this . processing ) {
26+ this . processing = true ;
27+ this . processNext ( ) ;
28+ }
29+ }
30+
31+ private async processNext ( ) : Promise < void > {
32+ if ( ! this . isEmpty ( ) ) {
33+ const snapshot = this . snapshots . shift ( ) ;
34+ try {
35+ this . processingSnapshot = snapshot ?. name ;
36+ let { processedSnapshot, warnings } = await processSnapshot ( snapshot , this . ctx ) ;
37+ await this . ctx . client . uploadSnapshot ( this . ctx , processedSnapshot ) ;
38+ this . ctx . totalSnapshots ++ ;
39+ this . processedSnapshots . push ( { name : snapshot . name , warnings} ) ;
40+ } catch ( error : any ) {
41+ this . ctx . log . debug ( `snapshot failed; ${ error } ` ) ;
42+ this . processedSnapshots . push ( { name : snapshot . name , error : error . message } ) ;
43+ }
44+ // Close open browser contexts and pages
45+ if ( this . ctx . browser ) {
46+ for ( let context of this . ctx . browser . contexts ( ) ) {
47+ for ( let page of context . pages ( ) ) {
48+ await page . close ( ) ;
49+ this . ctx . log . debug ( `Closed browser page for snapshot ${ snapshot . name } ` ) ;
50+ }
51+ await context . close ( ) ;
52+ this . ctx . log . debug ( `Closed browser context for snapshot ${ snapshot . name } ` ) ;
53+ }
54+ }
55+ this . processNext ( ) ;
56+ } else {
57+ this . processing = false ;
58+ }
59+ }
60+
61+ isProcessing ( ) : boolean {
62+ return this . processing ;
63+ }
64+
65+ getProcessingSnapshot ( ) : string {
66+ return this . processingSnapshot ;
67+ }
68+
69+ getProcessedSnapshots ( ) : Array < Record < string , any > > {
70+ return this . processedSnapshots ;
71+ }
72+
73+ isEmpty ( ) : boolean {
74+ return this . snapshots . length ? false : true ;
75+ }
76+ }
77+
78+ async function processSnapshot ( snapshot : Snapshot , ctx : Context ) : Promise < Record < string , any > > {
1379 ctx . log . debug ( `Processing snapshot ${ snapshot . name } ` ) ;
1480
1581 let launchOptions : Record < string , any > = { headless : true }
0 commit comments