| |||||||||
Book: The Judo Language 0.9 |
|
Schedules can not be embedded. A program can have one schedule running at a time. Each schedule is run by a timer called To abort a schedule job, run The following program emulates a count-down process, where 4 one-time events are run one second apart.
This is a simple repetitive motion:
Next, we emulate a coocoo clock that every hour it plays so many "Cukoo" sound. Lines 1 through 4 gets the time for the next hour. Line 7 gets the time for this event, and its hour attribute is used for the number of "Cucoo" sound.
Scheduled jobs should run quickly, that is, they should not run longer than the scheduled periods. If they do sometimes and mess up the operation, practical synchronization mechanism should be put in place. For instance, suppose every 5 minutes the job checks the database for unprocessed order data. If it founds some, process them. Sometimes e-commerce site may take so many orders in a short period of time, that within that 5-minute period they can not be all processed. What you can do is, for instance, use a temporary database table; when start processing, set a flag there, and reset it when done; if this flag is set, other jobs will simply bypass. The crux of this solution is, use an operation that is always short than the period to enforce the synchronization.
|
Listing 18.4 changing_time.judo |
---|
1: history = linkedList{}; 2: 3: schedule repeat 2000 4: { 5: history.add($$timer.time); 6: if $history.length() > 10 { history.remove(0); } 7: } 8: listen on 3333 title '<h1>Changing Time</h1>' 9: { 10: println <$$timer.htmlOut> '<h3>Latest Events</h3>'; 11: for x in history backward { println <$$timer.htmlOut> x, '<br>'; } 12: } |
The job itself does nothing but keeping the execution time in a list for the last 10 events (line 6). Its control panel server listens on port 3333. The control panel is typically run from a browser. The title, "Changing Time", is issued on line 8. The screen is standard above the horizontal line. If and only if both boxes are checked will the job be stopped and closed. Anything user type in to the edit box is sent to the server and becomes the content of $$timer.cmd
. What it means is totally up to your interpretation, such as a mini command system. In the control panel code, we used $$timer.htmlOut
to write out custom content that shows below the horizontal line (lines 10 and 11).
|