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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
# 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::TaskMgr;
use strict;
use base qw( MT::ErrorHandler );
use MT::Task;
use Fcntl qw( :DEFAULT :flock );
use Symbol;
our ( %Tasks, $inst );
sub instance {
$inst ||= new MT::TaskMgr;
}
sub new {
my $mgr = bless {}, shift;
$mgr->init();
return $mgr;
}
sub init {
my $mgr = shift;
return if $mgr->{initialized};
%Tasks = %{ MT->registry("tasks") || {} };
MT->run_callbacks( 'tasks', \%Tasks );
$mgr->{initialized} = 1;
}
sub run_tasks {
my $mgr = shift;
my (@tasks_to_run) = @_;
if ( !ref($mgr) ) {
$mgr = $mgr->instance;
}
@tasks_to_run = keys %Tasks unless @tasks_to_run;
if ( $mgr->{running} ) {
warn "Attempt to recursively invoke TaskMgr.";
return;
}
local $mgr->{running} = 1;
# Secure lock before running tasks
my $unlock;
unless ( $unlock = $mgr->_lock() ) {
MT->log(
{ class => 'system',
category => 'tasks',
level => MT::Log::ERROR(),
message => MT->translate(
"Unable to secure lock for executing system tasks. Make sure your TempDir location ([_1]) is writable.",
MT->config->TempDir
)
}
);
return;
}
eval {
my $app = MT->instance;
$app->run_callbacks('PeriodicTask');
require MT::Log;
require MT::Session;
my @completed;
foreach my $task_name (@tasks_to_run) {
my $task = $Tasks{$task_name} or next;
if ( ref $task eq 'HASH' ) {
$task->{key} ||= $task_name;
$task = $Tasks{$task_name} = MT::Task->new($task);
}
my $name = $task->label();
my $sess = MT::Session->load(
{ id => 'Task:' . $task->key,
kind => 'PT'
}
);
next if $sess && ( $sess->start + $task->frequency > time );
if ( !$sess ) {
$sess = MT::Session->new;
$sess->id( 'Task:' . $task->key );
$sess->kind('PT');
}
# Run this task
my $status;
eval {
local $app->{session} = $sess;
$status = $task->run;
};
if ($@) {
my $err = $@;
$app->log(
{ class => 'system',
category => 'tasks',
level => MT::Log::ERROR(),
message => $app->translate(
"Error during task '[_1]': [_2]",
$name, $err
),
metadata => MT::Util::log_time() . ' '
. $app->translate(
"Error during task '[_1]': [_2]",
$name, $err
)
}
);
}
else {
push @completed, $name
if ( defined $status )
&& ( $status ne '' )
&& ( $status > 0 );
}
$sess->start(time);
$sess->save;
}
if (@completed) {
$app->log(
{ class => 'system',
category => 'tasks',
level => MT::Log::INFO(),
message => $app->translate("Scheduled Tasks Update"),
metadata => MT::Util::log_time() . ' '
. $app->translate("The following tasks were run:")
. ' '
. join ", ",
@completed
}
);
}
};
$unlock->();
}
sub _lock {
my $mgr = shift;
my $cfg = MT->config;
# It's unwise to ignore locking for task manager; NoLocking should be
# limited to the DBM driver.
#if ($cfg->NoLocking) {
# ## If the user doesn't want locking, don't try to lock anything.
# ## Safe for tasks??
# return sub { };
#}
my $temp_dir = $cfg->TempDir;
my $mt_dir = MT->instance->{mt_dir};
$mt_dir =~ s/[^A-Za-z0-9]+/_/g;
my $lock_name = "mt-tasks-$mt_dir.lock";
require File::Spec;
$lock_name = File::Spec->catfile( $temp_dir, $lock_name );
if ( $cfg->UseNFSSafeLocking ) {
require Sys::Hostname;
my $hostname = Sys::Hostname::hostname();
my $lock_tmp = $lock_name . '.' . $hostname . '.' . $$;
my $max_lock_age = 60; ## no. of seconds til we break the lock
my $tries = 10; ## no. of seconds to keep trying
my $lock_fh = gensym();
open $lock_fh, ">$lock_tmp" or return;
select( ( select($lock_fh), $| = 1 )[0] ); ## Turn off buffering
my $got_lock = 0;
for ( 0 .. $tries - 1 ) {
print $lock_fh $$, "\n"; ## Update modified time on lockfile
if ( link( $lock_tmp, $lock_name ) ) {
$got_lock++;
last;
}
elsif ( ( stat $lock_tmp )[3] > 1 ) {
## link() failed, but the file exists--we got the lock.
$got_lock++;
last;
}
else {
## Couldn't get a lock; if the lock is too old, break it.
my $lock_age = ( stat $lock_name )[10];
unlink $lock_name if time - $lock_age > $max_lock_age;
}
sleep 1;
}
close $lock_fh;
unlink $lock_tmp;
return unless $got_lock;
return sub { unlink $lock_name };
}
else {
my $lock_fh = gensym();
sysopen $lock_fh, $lock_name, O_RDWR | O_CREAT, 0666
or return;
my $lock_flags = LOCK_EX;
unless ( flock $lock_fh, $lock_flags ) {
close $lock_fh;
return;
}
return sub { close $lock_fh; unlink $lock_name };
}
}
1;
__END__
=head1 NAME
MT::TaskMgr - MT class for controlling the execution of system tasks.
=head1 SYNOPSIS
MT::TaskMgr->run_tasks;
=head1 DESCRIPTION
C<MT::TaskMgr> defines a simple framework for the execution of a group of
runnable tasks (individually declared as C<MT::Task> objects). Each task
is executed according to their defined frequency. Tasks that fail are logged
to MT's log table.
=head1 ABOUT TASKS
Movable Type, being a publishing framework, can benefit greatly by having
a system of tasks that can be run "offline". Unfortunately, many MT users
don't have the luxury of scheduling these tasks using "cron" or other similar
facilities some servers provide. To satisfy everyone, the task framework
introduced here allows MT and third-party plugins to register tasks that
can be executed whenever the task subsystem is invoked. This can happen
a number of ways:
=over 4
=item * By a script: tools/run-periodic-tasks
For those that do have a "cron" system, they can continue to run the
C<tools/run-periodic-tasks> script provided with Movable Type. This script
now invokes the task subsystem to execute B<all> available tasks instead of
just the one for publishing scheduled posts.
=item * By fetching an activity feed
With the activity feeds MT serves, it will invoke the task subsystem first,
then return the feed. This allows users without access to cron service to
run scheduled tasks. Note however, that this mode is reliant upon the feed
being pulled by some client. If the feed is not being accessed, then the
tasks won't run either. A user can utilize a feed-reading online service to
achieve "24x7" task service to keep their tasks running smoothly.
=item * Other requests
Some tasks, such as the expiration of junk records, may be conditionally
executed. Junk feedback record expiration is a MT-defined task that executes
when tasks are run, and also when a new feedback record is scored as junk.
=back
Tasks are an excellent way to maximize MT performance and user experience.
For example, a plugin that may need to retrieve or synchronize data with a
remote server may choose to operate from a cache that is periodically kept
up to date using a registered task.
=head1 METHODS
=head2 MT::TaskMgr->new
Constructs the MT::TaskMgr singleton instance.
=head2 MT::TaskMgr->init
Initializes the MT::TaskMgr instance, pulling tasks are defined in
the MT registry. It also runs a callback 'tasks' after gathering
this list.
=head2 MT::TaskMgr->run_tasks
Runs all available pending tasks. If an instance of the TaskMgr is already
found to be running (through use of a physical file lock mechanism), the
process will abort.
=head2 MT::TaskMgr->instance
Returns the TaskMgr singleton.
=head1 CALLBACKS
=head2 PeriodicTask
Prior to running any registered tasks, this callback is issued to allow
any registered MT plugins to add additional tasks to the list or simply
as a way to signal tasks are about to start. This callback sends no
parameters, but it is possible to retrieve the active I<MT::TaskMgr>
instance using the I<instance> method.
=head2 tasks(\%tasks)
Upon initialization of the TaskMgr instance, the list of MT tasks are
gathered from the MT registry. This hashref of tasks is then passed to
the 'tasks' callback, giving plugins a chance to manipulate the task
metadata before being used.
=head1 AUTHOR & COPYRIGHTS
Please see the I<MT> manpage for author, copyright, and license information.
=cut
|