ฉันมีสองฟังก์ชั่นและscheduleScan()
scan()
scan()
สายscheduleScan()
เมื่อมีอะไรอย่างอื่นทำยกเว้นการจัดตารางการสแกนใหม่เพื่อให้สามารถกำหนดเวลาscheduleScan()
scan()
แต่มีปัญหาบางงานรันสองครั้ง
ฉันต้องการตรวจสอบให้แน่ใจว่ามีงานเพียงงานเดียวที่ดำเนินการในเวลาใดก็ตาม ฉันจะบรรลุสิ่งนั้นได้อย่างไร ฉันเชื่อว่ามีบางอย่างที่เกี่ยวข้องกับdone()
(อยู่ในการสแกน () ลบออกตอนนี้) แต่ฉันไม่สามารถหาวิธีแก้ปัญหาได้
รุ่น Bull: 3.12.1
การแก้ไขล่าช้าที่สำคัญ: scan()
เรียกใช้ฟังก์ชั่นอื่นและพวกเขาอาจหรือไม่อาจเรียกฟังก์ชั่นอื่น ๆ แต่พวกเขาทั้งหมดฟังก์ชั่นการซิงค์ดังนั้นพวกเขาจึงเรียกฟังก์ชั่นเฉพาะเมื่องานของตัวเองเสร็จสมบูรณ์มีเพียงข้างเดียว ในตอนท้ายของ "ต้นไม้" ฉันเรียกมันว่าฟังก์ชั่นสุดท้ายเรียก schedScan () แต่ไม่มีงานสองงานพร้อมกันทำงาน ทุกงานเริ่มต้น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
ฟังก์ชั่นคุณสามารถช่วยได้ไหม?