Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CRON Question
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01518445
Message ID:
01518488
Views:
54
>>>>>>I need to implement a schedule-based process using a CRON. Anyone know of a function or any code where I can pass in a CRON and get back a bool if it's time to run?
>>>>>
>>>>>I thought CRON was a Unix daemon for task scheduling. What do you mean by 'pass in a CRON' ?
>>>>
>>>>It is a unix thing but for some reason we're using it here in our C# apps. So I have to use it.
>>>>
>>>>I want to pass in a CRON expression, ie "* * * * * " or something like it, and get back a bool if the expression evaluates to DateTime.Now or past DateTime.Now.
>>>
>>>Take a look at Quartz.NET - http://quartznet.sourceforge.net/ - it's an open-source library that makes it really easy to schedule tasks to be run. The format you're using looks very similar to what Quartz.NET uses so if you're lucky they're compatible. I've used this library for a few projects and it's worked really nicely.
>>
>>If it has millisecond resolution (as stated) then it won't be compatible with the UNIX CRON format....
>>But I still don't really understand what Kevin needs to do when he's 'de-coded' the cron table info....
>
>I have a scheduler type application that needs to run some processes at known intervals. So the CRON would be used to specify when the processes need to execute.

So you would either need to scan the cron table once a minute to discover processes that need the be run at that particluar minute or to scan the table to discover when the *next* process should run and 'sleep' until that was due. The best approach would depend on the sort of scheduling you are likely to encounter. If it's 'once a day' type stuff then the latter approach would be much more efficient (but you need to file-watch the table to pick up changes). But if this is all you need then you can take a simpler approach to parsing a cron entry. e.g:
            string sampleCronString = "1,3,7,12-15 07 * * *";
            string[] fields = sampleCronString.Split(' ');
            DateTime currentTime = new DateTime(2011, 07, 13, 07, 4, 0);
            bool timeIsFlagged = IsValueFlagged(fields[0], currentTime.Minute) && IsValueFlagged(fields[1], currentTime.Hour);
With:
       bool IsValueFlagged(string s, int i)
        {
            List<string> s2 = s.Split(',').ToList();

            foreach (string s3 in s2)
            {
                if (s3=="*") return true;

                if (s3.Contains('-'))
                {
                    string[] s4 = s3.Split('-');
                    int start = int.Parse(s4[0]);
                    int end = int.Parse(s4[1]);
                    if (i >= start && i <= end)
                        return true;
                }
                else
                {
                    //Single item
                    if (i == int.Parse(s3)) return true;
                }
            }
            return false;
        }
Just remember that when you get to day level you need to check both the day of month *and* day of week
Previous
Reply
Map
View

Click here to load this message in the networking platform