File Coverage

blib/lib/TaskForest/TimeDependency.pm
Criterion Covered Total %
statement 54 55 98.1
branch 9 12 75.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 2 2 100.0
total 76 82 92.6


line stmt bran cond sub pod time code
1             ################################################################################
2             #
3             # $Id: TimeDependency.pm 211 2009-05-25 06:05:50Z aijaz $
4             #
5             ################################################################################
6              
7             =head1 NAME
8              
9             TaskForest::TimeDependency - A time constraint on a job
10              
11             =head1 SYNOPSIS
12              
13             use TaskForest::TimeDependency;
14              
15             # Assume it is now 20:55 (8:55 pm) in Chicago
16              
17             $td = TaskForest::TimeDependency->new(
18             start => '21:00',
19             tz => 'America/Chicago',
20             );
21              
22             $a = $td->check(); # $a == 0, $a->{status} eq 'Waiting'
23              
24             # 5 minutes go by
25              
26             # $a->{status} is still 'Waiting', but after
27              
28             $a = $td->check(); # now $a == 1 and $a->{status} is now 'Success'
29              
30             =head1 DOCUMENTATION
31              
32             If you're just looking to use the taskforest application, the only
33             documentation you need to read is that for TaskForest. You can do this
34             either of the two ways:
35              
36             perldoc TaskForest
37              
38             OR
39              
40             man TaskForest
41              
42             =head1 DESCRIPTION
43              
44             A TimeDependency is an object that a job depends on. It has a time
45             (and time zone) associated with it. Just as a job can depend on
46             another job, a job can also depend on a TimeDependency. The check()
47             function is used to determine whether or not a time dependency has
48             been met.
49              
50             =head1 METHODS
51              
52             =cut
53              
54             package TaskForest::TimeDependency;
55              
56 93     93   37568 use strict;
  93         170  
  93         3253  
57 93     93   462 use warnings;
  93         174  
  93         2532  
58 93     93   1561 use Data::Dumper;
  93         41384  
  93         4690  
59 93     93   140343 use DateTime;
  93         17123160  
  93         7845  
60 93     93   1145 use Carp;
  93         209  
  93         10649  
61 93     93   88191 use TaskForest::LocalTime;
  93         277  
  93         11154  
62              
63             BEGIN {
64 93     93   639 use vars qw($VERSION);
  93         185  
  93         3660  
65 93     93   53048 $VERSION = '1.30';
66             }
67              
68              
69             # ------------------------------------------------------------------------------
70             =pod
71              
72             =over 4
73              
74             =item new()
75              
76             Usage : my $td = TaskForest::TimeDependency->new();
77             Purpose : The TimeDependency constructor creates a simple
78             TimeDependency data structure. Other classes will set
79             and examine status and return code.
80             Returns : Self
81             Argument : Attributes as a hash. If a single scalar is provided,
82             then that is considered to be a DateTime object -
83             essentially a copy constructor.
84             Throws : "TimeDependency does not have a start/end time"
85              
86             =back
87              
88             =cut
89              
90             # ------------------------------------------------------------------------------
91             sub new {
92 633     633 1 2728 my $arg = shift;
93 633   33     4999 my $class = (ref $arg) || $arg;
94              
95             # initialize object with default atttributes
96 633         3311 my $self = {
97             start => '',
98             tz => '',
99             rc => '', # exit code
100             status => 'Waiting',
101             };
102              
103 633         1502 my $dt;
104 633 100       4220 if (scalar(@_) == 1) {
105             # assume it's a DateTime object. We really should die
106             # here if ref($dt) ne 'DateTime'
107             #
108 459         1826 $dt = shift;
109 459 50       2143 if (ref($dt) ne 'DateTime') {
110 0         0 croak "Non-DateTime object passed to TaskForest::TimeDependency::new(): ", ref($dt);
111             }
112 459         1577 $self->{start} = sprintf("%02d:%02d", $dt->hour, $dt->minute);
113             }
114             else {
115 174         613 my %args = @_;
116              
117             # Set the attributes passed in
118             #
119 174         689 foreach my $key (keys %args) {
120 348         956 $self->{$key} = $args{$key};
121             }
122              
123 174 50       832 croak "TimeDependency does not have a start time" unless $self->{start};
124 174 50       601 croak "TimeDependency does not have a time zone" unless $self->{tz};
125              
126             # create a DateTime object
127             #
128 174         946 $dt = DateTime->from_epoch(epoch => &TaskForest::LocalTime::epoch());
129 174         64591 $dt->set_time_zone($self->{tz});
130 174         54007 my ($hour, $min) = split(/:/, $self->{start});
131 174         1056 $dt->set(hour => $hour);
132 174         141763 $dt->set(minute => $min);
133             #$dt->set_time_zone($self->{tz});
134             }
135              
136             # set the ep attribute to the epoch value of the DateTime object.
137 633         120240 $self->{ep} = $dt->epoch;
138              
139 633         20094 bless $self, $class;
140 633         3608 return $self;
141             }
142              
143             # ------------------------------------------------------------------------------
144             =pod
145              
146             =over 4
147              
148             =item check()
149              
150             Usage : $td->check();
151             Purpose : Checks to see whether the time dependency has been met
152             or not.
153             Returns : 1 if it has been met. 0 otherwise.
154             Argument : None
155             Throws : Nothing
156              
157             =back
158              
159             =cut
160              
161             # ------------------------------------------------------------------------------
162             sub check {
163 504     504 1 6586 my $self = shift;
164 504         31042 my $now = &TaskForest::LocalTime::epoch();
165              
166             # If it's already marked as having been met, we just return 1;
167             #
168 504 100       2469 if ($self->{status} eq 'Success') {
169 375         2076 return 1;
170             }
171              
172             # If it has been met, set status variables and return 1 or 0
173             #
174 129 100       550 if ($now >= $self->{ep}) {
175 128         349 $self->{rc} = 0;
176 128         270 $self->{status} = 'Success';
177 128         1769 return 1;
178             }
179             else {
180 1         3 $self->{rc} = 1;
181 1         1 $self->{status} = 'Waiting';
182 1         3 return 0;
183             }
184             }
185            
186              
187             1;