hotFix: refs #8264 parallelism #1182

Merged
alexm merged 5 commits from 8264-hotFix_globalInvoicing_paralellism into master 2025-01-09 09:11:37 +00:00
1 changed files with 17 additions and 16 deletions
Showing only changes of commit ebb4d36fda - Show all commits

View File

@ -10,7 +10,6 @@ const { notify } = useNotify();
export const useInvoiceOutGlobalStore = defineStore({ export const useInvoiceOutGlobalStore = defineStore({
id: 'invoiceOutGlobal', id: 'invoiceOutGlobal',
state: () => ({ state: () => ({
initialDataLoading: true, initialDataLoading: true,
formInitialData: { formInitialData: {
@ -33,6 +32,7 @@ export const useInvoiceOutGlobalStore = defineStore({
nRequests: 0, nRequests: 0,
nPdfs: 0, nPdfs: 0,
totalPdfs: 0, totalPdfs: 0,
formData: null,
}), }),
actions: { actions: {
async init() { async init() {
@ -94,7 +94,6 @@ export const useInvoiceOutGlobalStore = defineStore({
async makeInvoice(formData, clientsToInvoice) { async makeInvoice(formData, clientsToInvoice) {
this.invoicing = true; this.invoicing = true;
const promises = [];
try { try {
this.printer = formData.printer; this.printer = formData.printer;
const params = { const params = {
@ -120,10 +119,9 @@ export const useInvoiceOutGlobalStore = defineStore({
throw new Error("There aren't addresses to invoice"); throw new Error("There aren't addresses to invoice");
} }
this.status = 'invoicing'; this.status = 'invoicing';
for (let index = 0; index < this.parallelism; index++) { this.formData = formData;
promises.push(this.invoiceClient(formData, index)); this.addressIndex = 0;
} await this.invoiceClient(this.addressIndex);
await Promise.all(promises);
} catch (err) { } catch (err) {
this.handleError(err); this.handleError(err);
} }
@ -182,8 +180,11 @@ export const useInvoiceOutGlobalStore = defineStore({
} }
}, },
async invoiceClient(formData, index) { async invoiceClient(index = this.addressIndex++) {
if (this.nRequests >= this.parallelism) return;
const address = this.addresses[index]; const address = this.addresses[index];
if (!address || !this.status || this.status == 'stopping') { if (!address || !this.status || this.status == 'stopping') {
this.status = 'stopping'; this.status = 'stopping';
this.invoicing = false; this.invoicing = false;
@ -193,17 +194,17 @@ export const useInvoiceOutGlobalStore = defineStore({
const params = { const params = {
clientId: address.clientId, clientId: address.clientId,
addressId: address.id, addressId: address.id,
invoiceDate: new Date(formData.invoiceDate), invoiceDate: new Date(this.formData.invoiceDate),
maxShipped: new Date(formData.maxShipped), maxShipped: new Date(this.formData.maxShipped),
companyFk: formData.companyFk, companyFk: this.formData.companyFk,
serialType: formData.serialType, serialType: this.formData.serialType,
}; };
this.invoicing = true; this.invoicing = true;
const { data } = await axios.post('InvoiceOuts/invoiceClient', params); const { data } = await axios.post('InvoiceOuts/invoiceClient', params);
Review

await?

await?
if (data) await this.makePdfAndNotify(data, address); if (data) this.makePdfAndNotify(data, address);
this.isInvoicing = false; this.isInvoicing = false;
} catch (err) { } catch (err) {
if (err?.response?.status >= 400 && err?.response?.status < 500) { if (err?.response?.status >= 400 && err?.response?.status < 500) {
@ -218,9 +219,7 @@ export const useInvoiceOutGlobalStore = defineStore({
throw new Error('Critical invoicing error, process stopped'); throw new Error('Critical invoicing error, process stopped');
Review

await?

await?
} }
} finally { } finally {
this.addressIndex++; await this.invoiceClient();
if (this.status != 'stopping')
await this.invoiceClient(formData, this.addressIndex);
} }
}, },
@ -231,9 +230,11 @@ export const useInvoiceOutGlobalStore = defineStore({
const params = { printerFk: this.printer }; const params = { printerFk: this.printer };
await axios.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params); await axios.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params);
this.nPdfs++; this.nPdfs++;
this.nRequests--;
} catch (err) { } catch (err) {
this.invoiceClientError(client, err.response?.data?.error?.message, true); this.invoiceClientError(client, err.response?.data?.error?.message, true);
Review

await?

await?
} finally {
this.nRequests--;
await this.invoiceClient(); // Comprobar que no haya ninguna factura pendiente
} }
}, },