私には2つの機能がscheduleScan()
ありscan()
ます。
scan()
scheduleScan()
新しいスキャンをスケジュールする以外に何もすることがないときにを呼び出すので、scheduleScan()
をスケジュールできscan()
ます。しかし、問題があります。一部のジョブは2回実行されます。
常に1つのジョブのみが処理されていることを確認したいと思います。どうすればそれを達成できますか?私はそれがdone()
(scan()に含まれていたが、現在は削除されている)と関係があると思いますが、解決策を見つけることができませんでした。
Bullバージョン:3.12.1
重要な後半の編集: scan()
別の関数を呼び出すと、他の関数を呼び出す場合と呼び出さない場合がありますが、それらはすべて同期関数であるため、独自のジョブが完了したときにのみ関数を呼び出します。「ツリー」の最後でそれを呼び出します。最後の関数はscheduleScan()を呼び出しますが、2つのジョブを同時に実行することはできません。scan()
ちなみに、すべての仕事はで始まります。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
機能が見つかりません、お手伝いできますか?