fixes #5529 Fixes: missing awaits, buffer-to-file
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Carlos Andrés 2023-04-04 15:00:28 +02:00
parent e8f0a49f0c
commit 0372d44415
7 changed files with 72 additions and 75 deletions

View File

@ -56,7 +56,7 @@ module.exports = Self => {
reference: invoiceOut.ref,
recipientId: invoiceOut.clientFk
});
const stream = await invoiceReport.toPdfStream();
const buffer = await invoiceReport.toPdfStream();
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
@ -66,7 +66,7 @@ module.exports = Self => {
const fileName = `${year}${invoiceOut.ref}.pdf`;
// Store invoice
print.storage.write(stream, {
await print.storage.write(buffer, {
type: 'invoice',
path: `${year}/${month}/${day}`,
fileName: fileName

View File

@ -100,16 +100,23 @@ class Controller extends Section {
};
this.$http.post(`InvoiceOuts/invoiceClient`, params)
.then(() => this.invoiceNext())
.catch(res => {
this.errors.unshift({
address,
message: res.data.error.message
});
const message = res.data?.error?.message || res.message;
if (res.status >= 400 && res.status < 500) {
this.errors.unshift({address, message});
this.invoiceNext();
} else {
this.invoicing = false;
this.status = 'done';
throw new UserError(`Critical invoicing error, proccess stopped`);
}
})
.finally(() => {
this.addressIndex++;
this.invoiceOut();
});
}
invoiceNext() {
this.addressIndex++;
this.invoiceOut();
}
get nAddresses() {

View File

@ -17,4 +17,5 @@ Ended process: Proceso finalizado
Invoice out: Facturar
One client: Un solo cliente
Choose a valid client: Selecciona un cliente válido
Stop: Parar
Stop: Parar
Critical invoicing error, proccess stopped: Error crítico al facturar, proceso detenido

View File

@ -46,7 +46,7 @@ module.exports = async function(Self, tickets, reqArgs = {}) {
const fileName = `${year}${invoiceOut.ref}.pdf`;
// Store invoice
storage.write(stream, {
await storage.write(stream, {
type: 'invoice',
path: `${year}/${month}/${day}`,
fileName: fileName

View File

@ -4,40 +4,38 @@ const {cpus} = require('os');
module.exports = {
init() {
if (!this.pool) {
Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: cpus().length,
puppeteerOptions: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--no-zygote'
]
}
})
.then(cluster => {
this.pool = cluster;
if (this.pool) return;
Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: cpus().length,
puppeteerOptions: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--no-zygote'
]
}
}).then(cluster => {
this.pool = cluster;
log4js.configure({
appenders: {
out: {type: 'stdout'}
},
categories: {default: {appenders: ['out'], level: 'info'}},
});
log4js.configure({
appenders: {
out: {type: 'stdout'}
},
categories: {default: {appenders: ['out'], level: 'info'}},
});
const logger = log4js.getLogger();
const logger = log4js.getLogger();
cluster.on('taskerror', (err, data, willRetry) => {
if (willRetry)
logger.warn(`[Print] => ${err.message}\nThis job will be retried`);
else
logger.error(`[Print] => ${err.message}`);
});
cluster.on('taskerror', (err, data, willRetry) => {
if (willRetry)
logger.warn(`[Print] => ${err.message}\nThis job will be retried`);
else
logger.error(`[Print] => ${err.message}`);
});
cluster.on('queue', () => logger.info('Printing task initialized by pool'));
});
}
cluster.on('queue', () => logger.info('Printing task initialized by pool'));
});
}
};

View File

@ -32,28 +32,31 @@ class Report extends Component {
if (fs.existsSync(fullPath))
options = require(optionsPath);
return new Promise(resolve => {
return new Promise((resolve, reject) => {
Cluster.pool.queue({}, async({page}) => {
await page.emulateMediaType('screen');
await page.setContent(template);
try {
await page.emulateMediaType('screen');
await page.setContent(template);
const element = await page.$('#pageFooter');
const element = await page.$('#pageFooter');
let footer = '\n';
if (element) {
footer = await page.evaluate(el => {
const html = el.innerHTML;
el.remove();
return html;
}, element);
let footer = '\n';
if (element) {
footer = await page.evaluate(el => {
const html = el.innerHTML;
el.remove();
return html;
}, element);
}
options.headerTemplate = '\n';
options.footerTemplate = footer;
const buffer = await page.pdf(options);
resolve(buffer);
} catch (err) {
reject(err);
}
options.headerTemplate = '\n';
options.footerTemplate = footer;
const stream = await page.pdf(options);
resolve(stream);
});
});
}

View File

@ -3,26 +3,14 @@ const path = require('path');
const fs = require('fs-extra');
module.exports = {
async write(stream, options) {
async write(buffer, options) {
const storage = config.storage[options.type];
if (!storage) return;
const src = path.join(storage.root, options.path);
const fileSrc = path.join(src, options.fileName);
await fs.mkdir(src, {recursive: true});
const writeStream = fs.createWriteStream(fileSrc);
writeStream.on('open', () => writeStream.write(stream));
writeStream.on('finish', () => writeStream.end());
return new Promise(resolve => {
writeStream.on('close', () => resolve());
});
},
load(type, data) {
await fs.writeFile(fileSrc, buffer);
}
};