I'm new on the list so double Hi for all,

>From my point of view is better to fetch mail using 'cron' + 'fetchmail' or 
>'getmail'.
But, the question is worth the trouble (statusd_dgs.lua by Jeremy Hankins is a 
good example
on this). I will draw some answers, actually, I only tried the third answer:

Lets assume:  defaults = { update_interval = 4*1000, foo_interval = 2.5*60*1000 
}
              local settings = table.join( ... )


1-) A simple statement accumulating time running in some foo variable:

        local foo = settings.update_interval  --> foo is actually a global 
variable
        
        if [ foo >= foo_interval ] then
                 fetch_something()
                 foo = settings.update_interval  --> If its done, re-init foo
        else
                 foo = foo + settings.update_interval  --> Acummulate time 
running
        end

-- The bad thing about this is that the real foo_interval is not exactly 2.5 
minutes for some
-- possible cases. Also, an update_interval bigger than foo_interval will make 
useless the
-- foo_interval, but could work for some body. Lets Move on.


2-) A function to calculate internal times:

        local init_time = os.time()*1000   --> A fallacy to calculate 
update_interval milliseconds
        local new_update = settings.update_interval
-->     A declaration like this must be done at the top of the script.

        function check_foo()
                local time = os.time()*1000 - init_time
                if time == foo_interval then
                        fetch_something()
                        init_time = init_time + foo_interval
                        new_update = settings.update_interval --> Re-init 
normal update_interval
                        return
                elseif ( time + settings.update_interval > foo_interval ) and
                        ( foo_interval - time < settings.update_interval) then
                        new_update = foo_interval - time
                        return
                end
        end
        
        function update_foo()
                check_foo()
                show_foo()
                foo_timer:set(new_update, update_foo)  --> Internal 
'update_interval' is new_update
        end

-- As I understand, the statements order doesn't affect the performance. Both 
statements must be
-- checked every cycle. This order is the best bet only when 
math.mod(foo_interval/update_interval) = 0
-- Again, this only works for foo_interval values bigger than update_interval 
[...]
-- To solve this we need another statement, and this function is a bug itself.  
Lets move on. 


3-) A boolean variable saying when to fetch_something(). I mean, two states ~ 
two time intervals:

        function update_foo()
                local fetch = [some condition] and fetch_something() or false 
                foo_timer:set( ... )  --> fetch_something() controls the actual 
timer.
        end
        
-- Here, [some condition] depends on your code (I mean, taste).
-- May be this is the better way. But quite obscure-bizarre for some people.

(You will see it implemented on statusd_flashing.lua if Tuomo accepts to put it 
on-line)


* If you are asking for some deep (de profundis) hacks on ioncore...
  I'd like to hear too if its fair and clean to do this from "inside".


Is this code useful for you? Bye!

-- 
Sorry, I can't find a GH --quiet option"
Mario G.H.

Reply via email to