Ho due funzioni scheduleScan()
e scan()
.
scan()
chiama scheduleScan()
quando non c'è nient'altro da fare se non programmare una nuova scansione , quindi scheduleScan()
può pianificare un scan()
. Ma c'è un problema, alcuni lavori vengono eseguiti due volte.
Voglio assicurarmi che venga elaborato un solo lavoro alla volta. Come posso raggiungerlo? Credo che abbia qualcosa a che fare con done()
(era in scan (), rimosso ora) ma non sono riuscito a trovare una soluzione.
Versione Bull: 3.12.1
Importante modifica tardiva: scan()
chiama un'altra funzione e possono o meno chiamare altre funzioni, ma sono tutte funzioni di sincronizzazione, quindi chiamano una funzione solo quando i loro lavori sono stati completati, c'è solo un modo per andare avanti. Alla fine dell '"albero", lo chiamo, l'ultima funzione chiama scheduleScan (), ma non ci possono essere due lavori simultanei in esecuzione. Ad ogni scan()
modo, ogni singolo lavoro inizia e finisce conscheduleScan(stock, period, milliseconds, 'called by file.js')
export function update(job) {
// does some calculations, then it may call scheduleScan() or
// it may call another function, and that could be the one calling
// scheduleScan() function.
// For instance, a function like finalize()
}
export function scan(job) {
update(job)
}
import moment from 'moment'
import stringHash from 'string-hash'
const opts = { redis: { port: 6379, host: '127.0.0.1', password: mypassword' } }
let queue = new Queue('scan', opts)
queue.process(1, (job) => {
job.progress(100).then(() => {
scan(job)
})
})
export function scheduleScan (stock, period, milliseconds, triggeredBy) {
let uniqueId = stringHash(stock + ':' + period)
queue.getJob(uniqueId).then(job => {
if (!job) {
if (milliseconds) {
queue.add({ stock, period, triggeredBy }, { delay: milliseconds, jobId: uniqueId }).then(() => {
// console.log('Added with ms: ' + stock + ' ' + period)
}).catch(err => {
if (err) {
console.log('Can not add because it exists ' + new Date())
}
})
} else {
queue.add({ stock, period, triggeredBy }, { jobId: uniqueId }).then(() => {
// console.log('Added without ms: ' + stock + ' ' + period)
}).catch(err => {
if (err) {
console.log('Can not add because it exists ' + new Date())
}
})
}
} else {
job.getState().then(state => {
if (state === 'completed') {
job.remove().then(() => {
if (milliseconds) {
queue.add({ stock, period, triggeredBy }, { delay: milliseconds, jobId: uniqueId }).then(() => {
// console.log('Added with ms: ' + stock + ' ' + period)
}).catch(err => {
if (err) {
console.log('Can not add because it exists ' + new Date())
}
})
} else {
queue.add({ stock, period, triggeredBy }, { jobId: uniqueId }).then(() => {
// console.log('Added without ms: ' + stock + ' ' + period)
}).catch(err => {
if (err) {
console.log('Can not add because it exists ' + new Date())
}
})
}
}).catch(err => {
if (err) {
// console.log(err)
}
})
}
}).catch(err => {
// console.log(err)
})
}
})
}
scan
funzione, puoi aiutarmi?