Tôi có hai chức năng, scheduleScan()
và scan()
.
scan()
gọi scheduleScan()
khi không có gì khác để làm ngoại trừ lên lịch quét mới , vì vậyscheduleScan()
có thể lên lịch a scan()
. Nhưng có một vấn đề, một số công việc chạy hai lần.
Tôi muốn đảm bảo rằng chỉ có một công việc đang được xử lý tại bất kỳ thời điểm nào. Làm thế nào tôi có thể đạt được điều đó? Tôi tin rằng nó có cái gì đó để làm vớidone()
, (nó đã được quét (), loại bỏ ngay bây giờ) nhưng tôi không thể đưa ra giải pháp.
Phiên bản Bull: 3.12.1
Chỉnh sửa muộn quan trọng: scan()
gọi các chức năng khác và chúng có thể hoặc không thể gọi các chức năng khác, nhưng chúng đều là các chức năng đồng bộ hóa, vì vậy chúng chỉ gọi một chức năng khi công việc của chúng được hoàn thành, chỉ còn một cách. Ở cuối "cây", tôi gọi nó, hàm cuối cùng gọi calendarScan (), nhưng không thể có hai công việc đồng thời chạy. Nhân tiện, mọi công việc đều bắt đầu scan()
, và họ kết thúc bằngscheduleScan(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
chức năng, bạn có thể giúp đỡ?