Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing Multiple Ranges can lead to a edge-case issues #240

Closed
muhammadtalhas opened this issue Aug 6, 2020 · 0 comments
Closed

Providing Multiple Ranges can lead to a edge-case issues #240

muhammadtalhas opened this issue Aug 6, 2020 · 0 comments

Comments

@muhammadtalhas
Copy link
Contributor

Hello, I ran into this issue while scheduling a task to run every X minutes between the hours of 0-2,10-23

It showed sporadic behavior, working sometimes but not other times. The issue is in the range conversion file. I have a fix for it and Ill open a PR, just wanted to post this issue for documentation purposes. Below is a snippet of some code taken from range-conversion (I modified it a bit to show where the issue arises.)

function replaceWithRange(expression, text, init, end) {

        var numbers = [];
        var last = parseInt(end);
        var first = parseInt(init);
        console.log(`first ${first}`)
        console.log(`last ${last}`)


        if (first > last) {
            last = parseInt(init);
            first = parseInt(end);
        }

        for (var i = first; i <= last; i++) {
            numbers.push(i);
        }
        console.log(`numbers ${numbers}`)
        console.log(`expression before actual replace  ${expression}`)
        console.log(`expression after  ${expression.replace(new RegExp(text, 'gi'), numbers.join())}`)

        return expression.replace(new RegExp(text, 'gi'), numbers.join());
    }    
    
    
    
    function convertRange(expression) {
        console.log(`expression  ${expression}`)
        var rangeRegEx = /(^\d+)\-(\d+)/;
        var match = rangeRegEx.exec(expression);
        console.log(`match  ${match}`)
        console.log(`expression after exec  ${expression}`)


        while (match !== null && match.length > 0) {
            console.log(`replaceWithRange(${expression}, ${match[0]}, ${match[1]}, ${match[2]})`)
            expression = replaceWithRange(expression, match[0], match[1], match[2]);
            console.log(`expression after replace  ${expression}`)
            match = rangeRegEx.exec(expression);
        }
        console.log(`expression final  ${expression}`)
        console.log(' ')
        return expression;
    }

    
    console.log(`Original: 0-2,10-23`)
    console.log(` `)
    console.log(`Final: ${convertRange('0-2,10-23')}`)

Heres the output:

Original: 0-2,10-23
 
expression  0-2,10-23
match  0-2,0,2
expression after exec  0-2,10-23
replaceWithRange(0-2,10-23, 0-2, 0, 2)
first 0
last 2
numbers 0,1,2
expression before actual replace  0-2,10-23
expression after  0,1,2,10,1,23
expression after replace  0,1,2,10,1,23
expression final  0,1,2,10,1,23
 
Final: 0,1,2,10,1,23

You can see the output seems to not parse the second range correctly. It seems the replace function uses the g (global) option to replace the original expression everywhere its found. In our case, the original 0-2 also appears in 10-23 ( 1[0-2]3 ) and the next time the loop run, it dosen't find anything that needs to be replaced.

merencia added a commit that referenced this issue Mar 10, 2021
Fixes #240. Edge case when dealing with multiple ranges for one parameter of the cron input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants