// @bun
import{yb as E}from"./chunk-8ty8fefr.js";function G(){let q=process.env.APP_DIR,y=`${process.env.ROOT_DIR}/dist/server/bootstrap.mjs`,z=new File([`
      import { app } from "${y}"
      self.onmessage = async (event) => {
        const clone = app.clone()
        let result = null;
        let error = null;
        try {
          result = clone.dispatchJob(event.data.jobName, event.data.args)
        } catch (err) {
          error = err
        }
        clone.destroy()
        if(error) {
          self.postMessage({error});
        } else {
          self.postMessage({result});
        }
      };
    `],"worker.ts"),A=URL.createObjectURL(z);return new Worker(A)}async function H(q,x){let y=G();return y.postMessage({jobName:q,args:x}),await new Promise((z,A)=>{y.onmessage=(F)=>{let B=F.data;if("error"in B)A(B.error);else z(B.result)}})}class K extends E{service;static _name="QueueServiceContainer";queue=new Set;activeRunningJobsCount=0;isRunning=!1;jobs={};constructor(q){super();this.service=q;this.jobs=Object.fromEntries(this.service.jobs.map((x)=>[x.name,x]))}dispatchJob(q,x){if(this.jobs[q])return new this.jobs[q]().run(JSON.parse(x))}async next(){if(!this.isRunning)this.isRunning=!0;if(this.activeRunningJobsCount>=this.service.concurrency)return await new Promise((x)=>setTimeout(x,1000)),this.next();let q=this.queue.values().next().value;if(this.jobs[q?.class]){let{value:x}=this.queue.values().next();if(x)this.queue.delete(x);this.run(q)}if(this.queue.size===0){this.isRunning=!1;return}await this.next()}async run(q){let x=this.jobs[q.class],y=new x,z=JSON.parse(q.args);this.activeRunningJobsCount++;try{let A=await(y.worker?H(q.class,q.args):y.run(...z));y.onSuccess(A,...z)}catch(A){if(y.onFail(A,...z),q.retries>=y.maxAttempts-1)y.onDeadletter(A,...z);else this.push(x,q.args,q.retries+1)}this.activeRunningJobsCount--}push(q,x,y=0){if(this.queue.add({class:q.name,args:x,createdAt:Date.now(),retries:y}),!this.isRunning)this.next()}}
export{K as rb};

//# debugId=A06129CE7CFD6E4564756E2164756E21
