मेरे पास दो कार्य हैं, scheduleScan()
और scan()
।
scan()
scheduleScan()
जब कोई नया स्कैन शेड्यूल करने के अलावा और कुछ नहीं होता है , तो कॉल scheduleScan()
कर सकते हैं scan()
। लेकिन एक समस्या है, कुछ नौकरियां दो बार चलती हैं।
मैं यह सुनिश्चित करना चाहता हूं कि किसी भी समय केवल एक ही कार्य संसाधित किया जा रहा है। मैं उसे कैसे प्राप्त कर सकता हूं? मेरा मानना है कि इसके साथ कुछ करना है done()
, (यह स्कैन में था), अब हटा दिया गया है, लेकिन मैं एक समाधान के साथ नहीं आ सका।
बैल संस्करण: 3.12.1
महत्वपूर्ण देर से संपादित: scan()
दूसरे कार्यों को कॉल करता है और वे अन्य कार्यों को कॉल कर सकते हैं या नहीं भी कर सकते हैं, लेकिन वे सभी सिंक फ़ंक्शन हैं, इसलिए वे केवल एक फ़ंक्शन को कॉल करते हैं जब उनके स्वयं के काम पूरे हो जाते हैं, केवल एक ही रास्ता है। "ट्री" के अंत में, मैं इसे कॉल करता हूं, अंतिम फ़ंक्शन शेड्यूलस्कैन () कहता है, लेकिन दो एक साथ चलने वाले काम नहीं हो सकते हैं। हर एक काम शुरू होता है 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
फ़ंक्शन खोजने में असमर्थ हूं , क्या आप मदद कर सकते हैं?