File: Task.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (158 lines) | stat: -rw-r--r-- 3,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::Task;

use strict;
use base qw(MT::ErrorHandler);

sub new {
    my $class = shift;
    my ($this) = ref $_[0] ? @_ : {@_};
    bless $this, $class;
    $this->init();
    $this;
}

sub init {
}

sub key {
    my $task = shift;
    $task->{key} = shift if @_;
    $task->{key};
}

sub name {
    my $task = shift;
    $task->{name} = shift if @_;
    $task->{name} || $task->{key};
}

sub label {
    my $task  = shift;
    my $label = $task->{label};
    $label = $label->() if ref($label) eq 'CODE';
    return $label || $task->name();
}

sub frequency {
    my $task = shift;
    $task->{frequency} = shift if @_;

    # default to daily run
    my $freq = $task->{frequency} || 60 * 60 * 24;
    if ( $freq =~ m/^\s*sub\s*\{/s ) {
        $freq = MT->handler_to_coderef($freq);
        $freq = $freq->();
    }
    $freq;
}

sub run {
    my $task = shift;
    my $code = $task->{code} || $task->{handler};
    if ( ref $code ne 'CODE' ) {
        $code = $task->{code} = MT->handler_to_coderef($code);
    }
    if ( ref $code eq 'CODE' ) {
        return $code->($task);
    }
    0;
}

1;
__END__

=head1 NAME

MT::Task - Movable Type class for registering runnable tasks.

=head1 SYNOPSIS

    package MyPlugin;

    MT->add_task(new MT::Task({
        name => "My Task",
        key => "Task001",
        frequency => 3600, # at most, once per hour
        code => \&runner
    }));

=head1 DESCRIPTION

An I<MT::Task> object is used to define a runnable task that can be executed
by Movable Type. The base object defines common characteristics of all tasks
as well as the method to invoke it.

Normally, a plugin will construct an I<MT::Task> object and pass it
to the C<add_task> method of the I<MT> class:

    MT->add_task(new MT::Task({
        # arguments; see listing of arguments below
    }));

=head1 METHODS

=head2 MT::Task->new(\%params)

The following are a list of parameters that can be specified when constructing
an I<MT::Task> object.

=over 4

=item * key

Defines a unique string for the task. This is used to manage a session record
for the task. The task session record is kept to record the last time the
task was executed. Session records are stored in the C<mt_session> table with
a 'kind' column value of 'PT' (periodic task).

=item * name

The name of the task. This is optional, but is used in recording error log
records when the task fails.

=item * frequency

The frequency is a duration of time expressed in seconds. This defines how
often the task is to be run. There is no guarantee that the task will be
run this often; only that it will not be executed more than once for that
period of time.

=item * code

The code to be executed when the task is invoked by I<MT::TaskMgr>. This
is a code reference and the routine is given the I<MT::Task> instance
as a parameter.

=back

=head2 $task->init

Initializes a I<MT::Task> instance.

=head2 $task->run

Called when running the task.

=head2 $task->key([$key])

Gets or sets the unique key registered for the task.

=head2 $task->name([$name])

Gets or sets the name registered for the task.

=head2 $task->frequency([$frequency])

Gets or sets the execution frequency for the task (in seconds). The default frequency is 86400 which is daily.

=head1 AUTHOR & COPYRIGHTS

Please see the I<MT> manpage for author, copyright, and license information.

=cut