İki fonksiyonum var scheduleScan()
ve scan()
.
scan()
çağrılar scheduleScan()
Yeni bir tarama zamanlama dışında yapacak başka bir şey yokken , bu yüzden scheduleScan()
bir planlayabilirsiniz scan()
. Ama bir sorun var, bazı işler iki kez çalışıyor.
Herhangi bir zamanda yalnızca bir işin işlendiğinden emin olmak istiyorum. Bunu nasıl başarabilirim? Bunun bir ilgisi olduğuna inanıyorum done()
, (şimdi tarandı (), kaldırıldı) ama bir çözüm bulamadım.
Boğa sürümü: 3.12.1
Önemli geç düzenleme: scan()
başka işlevleri çağırır ve diğer işlevleri çağırabilir veya çağırabilirler, ancak hepsi senkronizasyon işlevleridir, bu nedenle yalnızca kendi işleri tamamlandığında bir işlevi çağırırlar, yalnızca bir yol vardır. "Ağacın" sonunda, ben diyorum, son işlev scheduleScan () çağırır, ama aynı anda çalışan iki iş olamaz. Her iş scan()
bu arada başlar ve biter.scheduleScan(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
İşlev bulamıyorum , yardımcı olabilir misiniz?