| 1 | package Hopkins; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use version 0.77; | ||
| 6 | |||
| 7 | our $VERSION = version->declare('0.9.2'); | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Hopkins - complete multiqueue job scheduling and execution system | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Hopkins is, in simplest terms, a better cron. In depth, | ||
| 16 | though, Hopkins is an extensible application geared toward | ||
| 17 | the management, scheduling, and execution of both inline | ||
| 18 | perl as well as external binaries. | ||
| 19 | |||
| 20 | Hopkins's advantages include: | ||
| 21 | |||
| 22 | =over 4 | ||
| 23 | |||
| 24 | =item * simple | ||
| 25 | |||
| 26 | There are many job management systems out there, varying in | ||
| 27 | complexity. Hopkins was designed to be simple to understand | ||
| 28 | and simple to configure. | ||
| 29 | |||
| 30 | =item * agnostic implementation | ||
| 31 | |||
| 32 | While Hopkins is written in perl and will dynamically load, | ||
| 33 | instantiate, and execute any object that provides a "run" | ||
| 34 | method, it does not require your class to be aware of its | ||
| 35 | environment. In fact, Hopkins does not require your task | ||
| 36 | to be written in perl at all. | ||
| 37 | |||
| 38 | =item * live, extensible configuration | ||
| 39 | |||
| 40 | Hopkins ships with Hopkins::Config::XML, which allows your | ||
| 41 | queues and tasks to be defined entirely in XML, validated | ||
| 42 | before load via XML Schema. However, Hopkins::Config may | ||
| 43 | be subclassed to provide configuration any way you like. | ||
| 44 | |||
| 45 | Hopkins also provides a mechanism by which your config may | ||
| 46 | be scanned for changes. For example, Hopkins::Config::XML | ||
| 47 | periodically checks the configuration file for changes and | ||
| 48 | validates the XML before replacing the existing config. | ||
| 49 | |||
| 50 | =item * multiple queues | ||
| 51 | |||
| 52 | Hopkins supports an infinite number of queues that each | ||
| 53 | have their own concurrency limits and behaviors upon task | ||
| 54 | failure. Per-queue concurrencies allow you to define serial | ||
| 55 | queues (concurrency=1) or worker queues (concurrency > 1). | ||
| 56 | |||
| 57 | Each queue's behavior on task failure is configurable. For | ||
| 58 | example, queues may be configured to halt upon task failure, | ||
| 59 | stopping the execution of queued tasks until a human has a | ||
| 60 | chance to examine the failure. | ||
| 61 | |||
| 62 | =item * multiple schedules per task | ||
| 63 | |||
| 64 | Each configured task may have a number of schedules defined, | ||
| 65 | including none. | ||
| 66 | |||
| 67 | =item * output/execution logging via DBI | ||
| 68 | |||
| 69 | Each queued task records state information to a configurable | ||
| 70 | database backend. Information stored includes enqueue time, | ||
| 71 | time to execute, execution time, completion time, status | ||
| 72 | flags, as well as all output generated on stdout and stderr. | ||
| 73 | |||
| 74 | =item * disconnected operation | ||
| 75 | |||
| 76 | If the database backend ever becomes unavailable, Hopkins | ||
| 77 | will continue to run, queueing up database requests. | ||
| 78 | |||
| 79 | =item * extensible | ||
| 80 | |||
| 81 | Hopkins supports loadable plugins which may hook into the | ||
| 82 | running system by making use of POE. Two plugins include | ||
| 83 | a web-based user interface (Hopkins::Plugin::HMI) as well | ||
| 84 | as a SOAP-based interface (Hopkins::Plugin::RPC). | ||
| 85 | |||
| 86 | =item * log4perl | ||
| 87 | |||
| 88 | Hopkins makes use of the excellent Log::Log4perl module for | ||
| 89 | its logging, allowing you to direct task execution errors | ||
| 90 | and other output to the appropriate parties with ease. | ||
| 91 | |||
| 92 | =item * durability | ||
| 93 | |||
| 94 | Queue (both task and database) contents are written to disk | ||
| 95 | upon entering/exiting the queue. Thus, if the daemon were | ||
| 96 | to be restarted for any reason, it will continue processing | ||
| 97 | from where it left off. | ||
| 98 | |||
| 99 | =item * reliability | ||
| 100 | |||
| 101 | Hopkins has been used in an environment handling > 60 tasks | ||
| 102 | in four separate queues and does not eat resources. I make | ||
| 103 | no promises as to its scalability beyond that. | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | See L<Hopkins::Manual::Configuration::XML> for information | ||
| 108 | on configuring Hopkins using the default XML configuration. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | #sub POE::Kernel::TRACE_REFCNT () { 1 } | ||
| 113 | |||
| 114 | use base 'Class::Accessor::Fast'; | ||
| 115 | |||
| 116 | use POE qw(Component::JobQueue Wheel::Run); | ||
| 117 | |||
| 118 | use Class::Accessor::Fast; | ||
| 119 | use POE::API::Peek; | ||
| 120 | |||
| 121 | use DateTime; | ||
| 122 | use Log::Log4perl; | ||
| 123 | use Log::Log4perl::Level; | ||
| 124 | |||
| 125 | use Hopkins::Manager; | ||
| 126 | |||
| 127 | __PACKAGE__->mk_accessors(qw(conf l4pconf scan poll manager)); | ||
| 128 | |||
| 129 | { | ||
| 130 | package DateTime::Duration; | ||
| 131 | |||
| 132 | sub stringify | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my @units = $self->in_units('days', 'hours', 'minutes', 'seconds'); | ||
| 137 | |||
| 138 | join ' ', map { my $v = shift @units; $v ? $v . $_ : () } qw(d h m s); | ||
| 139 | } | ||
| 140 | |||
| 141 | use overload '""' => \&stringify; | ||
| 142 | } | ||
| 143 | |||
| 144 | # prevent perl from bitching and complaining about prototype | ||
| 145 | # mismatches and constant subroutine redefinitions. the | ||
| 146 | # warnings pragma doesn't prevent ALL of them from spewing, | ||
| 147 | # so we have to get raunchy with perl by defining them at | ||
| 148 | # runtime with a localized no-op warn handler. | ||
| 149 | |||
| 150 | { | ||
| 151 | local $SIG{__WARN__} = sub { 1 }; | ||
| 152 | |||
| 153 | # forcefully disable the unavoidable debugging output | ||
| 154 | # from several of the POE components. | ||
| 155 | |||
| 156 | eval q/ | ||
| 157 | sub POE::Wheel::SocketFactory::DEBUG () { 0 } | ||
| 158 | sub POE::Kernel::alias { (shift->alias_list(@_))[0] } | ||
| 159 | /; | ||
| 160 | } | ||
| 161 | |||
| 162 | =head1 METHODS | ||
| 163 | |||
| 164 | =head2 new | ||
| 165 | |||
| 166 | instantiates a new Hopkins object. the Hopkins constructor | ||
| 167 | accepts a hash of options. currently supported options are: | ||
| 168 | |||
| 169 | =over 4 | ||
| 170 | |||
| 171 | =item conf | ||
| 172 | |||
| 173 | path to the hopkins XML configuration file | ||
| 174 | |||
| 175 | =item lp4conf | ||
| 176 | |||
| 177 | path to the log4perl configuration file | ||
| 178 | |||
| 179 | =item scan | ||
| 180 | |||
| 181 | configuration scan resolution (in seconds) | ||
| 182 | |||
| 183 | =item poll | ||
| 184 | |||
| 185 | scheduler poll resolution (in seconds) | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub new | ||
| 192 | { | ||
| 193 | my $proto = shift; | ||
| 194 | my $opts = shift; | ||
| 195 | |||
| 196 | # defaults | ||
| 197 | |||
| 198 | $opts->{conf} ||= [ XML => { file => '/etc/hopkins/hopkins.xml' } ]; | ||
| 199 | $opts->{lp4conf} ||= '/etc/hopkins/log4perl.conf'; | ||
| 200 | $opts->{scan} ||= 30; | ||
| 201 | $opts->{poll} ||= 30; | ||
| 202 | |||
| 203 | # make me majikal wif ur holy waterz. plzkthx. | ||
| 204 | |||
| 205 | my $self = $proto->SUPER::new($opts); | ||
| 206 | |||
| 207 | # initialize log4perl using the contents of the supplied | ||
| 208 | # configuration file. | ||
| 209 | # | ||
| 210 | # after initialization (which may have failed), we'll | ||
| 211 | # create a logger and associated appender for logging | ||
| 212 | # all error level log messages to stderr. this allows | ||
| 213 | # logging error messages to the console using log4perl | ||
| 214 | # regardless of the configuration that the user loads. | ||
| 215 | |||
| 216 | eval { Log::Log4perl->init_and_watch($opts->{l4pconf}, $opts->{scan}) }; | ||
| 217 | |||
| 218 | my $l4perr = $@; | ||
| 219 | my $logger = Log::Log4perl->get_logger('hopkins'); | ||
| 220 | my $layout = new Log::Log4perl::Layout::PatternLayout '%X{session}: %p: %m%n'; | ||
| 221 | |||
| 222 | my $appender = new Log::Log4perl::Appender | ||
| 223 | 'Log::Log4perl::Appender::Screen', | ||
| 224 | name => 'stderr', | ||
| 225 | stderr => 1; | ||
| 226 | |||
| 227 | $appender->layout($layout); | ||
| 228 | $appender->threshold($ERROR); | ||
| 229 | $logger->add_appender($appender); | ||
| 230 | |||
| 231 | Hopkins->log_error("unable to load log4perl configuration file: $l4perr") | ||
| 232 | if $l4perr; | ||
| 233 | |||
| 234 | $self->manager(new Hopkins::Manager { hopkins => $self }); | ||
| 235 | |||
| 236 | return $self; | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 run | ||
| 240 | |||
| 241 | start the hopkins daemon. this method will never return. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub run { POE::Kernel->run } | ||
| 246 | |||
| 247 | =head2 is_session_running | ||
| 248 | |||
| 249 | returns a truth value indicating whether or not a session | ||
| 250 | exists with the specified alias. | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | sub is_session_active | ||
| 255 | { | ||
| 256 | my $self = shift; | ||
| 257 | my $name = shift; | ||
| 258 | |||
| 259 | my $api = new POE::API::Peek; | ||
| 260 | my @sessions = map { POE::Kernel->alias($_) } $api->session_list; | ||
| 261 | |||
| 262 | #Hopkins->log_debug("checking for session $name"); | ||
| 263 | |||
| 264 | return scalar grep { $name eq $_ } @sessions; | ||
| 265 | } | ||
| 266 | |||
| 267 | =head2 get_running_sessions | ||
| 268 | |||
| 269 | returns a list of currently active session aliases | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub get_running_sessions | ||
| 274 | { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | my $api = new POE::API::Peek; | ||
| 278 | |||
| 279 | return map { POE::Kernel->alias($_) } $api->session_list; | ||
| 280 | } | ||
| 281 | |||
| 282 | =head2 parse_datetime | ||
| 283 | |||
| 284 | DateTime::Format::ISO8601->parse_datetime wrapper that traps | ||
| 285 | exceptions. this really shouldn't be necessary. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub parse_datetime | ||
| 290 | { | ||
| 291 | my $self = shift; | ||
| 292 | |||
| 293 | my $date; | ||
| 294 | |||
| 295 | eval { $date = DateTime::Format::ISO8601->parse_datetime(@_) }; | ||
| 296 | |||
| 297 | return $@ ? undef : $date; | ||
| 298 | } | ||
| 299 | |||
| 300 | =head2 get_logger | ||
| 301 | |||
| 302 | returns a Log::Log4perl logger for the current session. the | ||
| 303 | get_logger expects the POE kernel to be passed to it. if no | ||
| 304 | POE::Kernel is passed, it will default to $poe_kernel. | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | my $loggers = {}; | ||
| 309 | |||
| 310 | sub get_logger | ||
| 311 | { | ||
| 312 | my $self = shift; | ||
| 313 | my $kernel = shift || $poe_kernel; | ||
| 314 | my $alias = $kernel->alias; | ||
| 315 | my $session = 'hopkins' . ($alias ? '.' . $alias : ''); | ||
| 316 | my $name = lc ((caller(2))[3]); | ||
| 317 | |||
| 318 | $alias = 'unknown' if not defined $alias; | ||
| 319 | |||
| 320 | if (not exists $loggers->{$name}) { | ||
| 321 | $loggers->{$name} = Log::Log4perl->get_logger($name); | ||
| 322 | } | ||
| 323 | |||
| 324 | Log::Log4perl::MDC->put('session', $session); | ||
| 325 | |||
| 326 | return $loggers->{$name}; | ||
| 327 | } | ||
| 328 | |||
| 329 | sub get_worker_logger | ||
| 330 | { | ||
| 331 | my $self = shift; | ||
| 332 | my $task = shift; | ||
| 333 | my $kernel = shift || $poe_kernel; | ||
| 334 | my $alias = $kernel->alias; | ||
| 335 | my $session = 'hopkins' . ($alias ? '.' . $alias : ''); | ||
| 336 | my $name = "hopkins.task.$task"; | ||
| 337 | |||
| 338 | $alias = 'unknown' if not defined $alias; | ||
| 339 | |||
| 340 | if (not exists $loggers->{$name}) { | ||
| 341 | $loggers->{$name} = Log::Log4perl->get_logger($name); | ||
| 342 | } | ||
| 343 | |||
| 344 | Log::Log4perl::MDC->put('session', $session); | ||
| 345 | Log::Log4perl::MDC->put('task', $task); | ||
| 346 | |||
| 347 | return $loggers->{$name}; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub log_debug { return shift->get_logger->debug(@_) } | ||
| 351 | sub log_info { return shift->get_logger->info(@_) } | ||
| 352 | sub log_warn { return shift->get_logger->warn(@_) } | ||
| 353 | sub log_error { return shift->get_logger->error(@_) } | ||
| 354 | sub log_fatal { return shift->get_logger->fatal(@_) } | ||
| 355 | |||
| 356 | sub log_worker_stdout { return shift->get_worker_logger(shift)->info(@_) } | ||
| 357 | sub log_worker_stderr { return shift->get_worker_logger(shift)->warn(@_) } | ||
| 358 | |||
| 359 | =head1 BUGS | ||
| 360 | |||
| 361 | this is my first foray into POE territory. the way the | ||
| 362 | system is architected may be horribly inefficient, cause | ||
| 363 | cancer, or otherwise be a general nuisance to its intended | ||
| 364 | user(s). my bad. | ||
| 365 | |||
| 366 | =head1 AUTHOR | ||
| 367 | |||
| 368 | Mike Eldridge <diz@cpan.org> | ||
| 369 | |||
| 370 | =head1 LICENSE | ||
| 371 | |||
| 372 | Copyright (c) 2010 Mike Eldridge. All rights reserved. | ||
| 373 | |||
| 374 | This program is free software; you may redistribute it | ||
| 375 | and/or modify it under the same terms as Perl itself. | ||
| 376 | |||
| 377 | =head1 HOME | ||
| 378 | |||
| 379 | http://github.com/tripside/hopkins | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | 1; |