kkavitha has asked for the wisdom of the Perl Monks concerning the following question:

I have to add the timerservice in existing perl project. That timerservice is must took one entry at runtime, and update the entry with timestamp in one text file if entry not exists. And, then it should update the timestamp for every one hour while project is running.

Example: entry: pdt1 In text file, pdt1 02/08/09 16:07:55

While project is running, first that will make a entry as like the above in text file. Then should update the timestamp for every one hour in that file

Tried with subroutine by calling &serviceTimer($entry) in starting of the project. But it is not allowing to run other modules. Because, serviceTimer subroutine should run independently until project execution is stop.

If anyone have ideas/knowldge on this, Couls you please share the same.

Thanks in advance, Kavitha

Replies are listed 'Best First'.
Re: How to add timer
by Anonymous Monk on Sep 02, 2009 at 10:53 UTC
      Thanks for your reply.

      But for my case, we are having products. Already it is running by cron for each product.It can run by on-demand as manual for single product. Both are used to create the datewise entry for each product in product based file.

      While cron and ondemand are trying to run for same product at same time, the datewise entry gets duplicated two times. Both are independent process. To avoid the duplicate entry,it should not allow to run for same product at same time.

      Tried with one common file in which add the entry of product with timestamp once it is started by cron or ondemand. That should be update every one hour until it is stopped. I written as subroutine.

      How to make that subroutine to run without disturbing other functions of same module until it is stopped?

        Add a check for that.
Re: How to add timer
by stevieb (Canon) on Sep 02, 2009 at 12:47 UTC

    I made a weak q&d attempt using fork (I've never used it before), but perhaps it may provide an idea. It would also be worth looking at modules that could help with this as others have suggested.

    It's ugly, but I had to try out fork sometime ;)

    #!/usr/bin/perl use strict; use warnings; my $pid = fork(); my $doing_something = 1; while ( $doing_something ) { if ( $pid == 0 ) { # we're the child do_time_function(); sleep 10; } } print "I'm the parent, and I can do other stuff " . "while the child is waiting to update the " . "time stamp...\n"; sub do_time_function { print "blah, timestamp updated\n";

    Steve

Re: How to add timer
by ig (Vicar) on Sep 02, 2009 at 21:40 UTC

    You could use alarm to send SIGALRM after an hour and in you alarm handler update the timestamp and set another alarm.

    If your objective is only to prevent multiple concurrent executions of your program, then you might find File::Pid or one of the other pid modules does what you need.

Re: How to add timer
by Marshall (Canon) on Sep 02, 2009 at 21:59 UTC
    I'd like to hear more about what it is that you are going to do with this hour by hour logging info?
    I have to add the timerservice in existing perl project
    jumps right away to an implementation. This can become quite complex and I suggest backing up and starting anew with an end objective in mind, eg the desired user report that this will be used for.

    Before going further, I would suggest that whatever the implementation, that you have an easier to use format for the log entries. "pdt1 2009/08/30 16:07:55", YYYY/MM/DD and use leading zero's. The reason for this order is so that a simple default sort on date/time will produce the right order without heroics. Since you are designing the log file output format, make it easy for you to deal with!

    Past that, it appears that you have 3 states that are potentially relevant: I just started, I am still running now, I just stopped. While I am on this subject, it could be that you want the PID of each pdt1 process?

    What would "some instance of my program was alive at this particular time which is 12 minutes past the hour, each hour" tell you? Usually not much.

    Anyway, before going further into implementation, I would like to hear more about what kind of "end product" you intend to produce (presumably a report). What is going to be in that report?