| 1 | package QBit::Cron; | ||
| 2 | $QBit::Cron::VERSION = '0.004'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use Fcntl qw(:flock); | ||
| 6 | |||
| 7 | sub check_rights {1} # Cron has full privileges | ||
| 8 | |||
| 9 | sub get_cron_methods { | ||
| 10 | my ($self) = @_; | ||
| 11 | |||
| 12 | my $methods = {}; | ||
| 13 | |||
| 14 | package_merge_isa_data( | ||
| 15 | ref($self), | ||
| 16 | $methods, | ||
| 17 | sub { | ||
| 18 | my ($package, $res) = @_; | ||
| 19 | |||
| 20 | my $pkg_methods = package_stash($package)->{'__CRON__'} || {}; | ||
| 21 | foreach my $path (keys(%$pkg_methods)) { | ||
| 22 | foreach my $method (keys(%{$pkg_methods->{$path}})) { | ||
| 23 | $methods->{$path}{$method} = $pkg_methods->{$path}{$method}; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | }, | ||
| 27 | __PACKAGE__ | ||
| 28 | ); | ||
| 29 | |||
| 30 | return $methods; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub do { | ||
| 34 | my ($self, $path, $method) = @_; | ||
| 35 | |||
| 36 | ($path, $method) = @ARGV unless defined($method); | ||
| 37 | |||
| 38 | throw gettext("Expecting 'path'") unless defined $path; | ||
| 39 | throw gettext("Expecting 'method'") unless defined $method; | ||
| 40 | |||
| 41 | $self->pre_run(); | ||
| 42 | |||
| 43 | # assume internal user (id = 0) | ||
| 44 | $self->set_option('cur_user', {id => 0}); | ||
| 45 | |||
| 46 | my $methods = $self->get_cron_methods(); | ||
| 47 | |||
| 48 | throw gettext('Method "%s" with path "%s" does not exists', $method, $path) | ||
| 49 | unless exists($methods->{$path}{$method}); | ||
| 50 | |||
| 51 | my $cron = $methods->{$path}{$method}{'package'}->new(app => $self); | ||
| 52 | my $attrs = $methods->{$path}{$method}{'attrs'}; | ||
| 53 | |||
| 54 | my $lock_name; | ||
| 55 | $lock_name = defined($attrs->{'lock'}) ? $attrs->{'lock'} : "cron__${path}__$method" | ||
| 56 | if exists($attrs->{'lock'}); | ||
| 57 | |||
| 58 | if (defined($lock_name)) { | ||
| 59 | unless ($self->get_lock($lock_name)) { | ||
| 60 | l gettext("Other %s->%s is running now, I'm exiting", $path, $method) unless $attrs->{'silent'}; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | try { | ||
| 66 | $methods->{$path}{$method}{'sub'}($cron); | ||
| 67 | } | ||
| 68 | catch { | ||
| 69 | $self->release_lock($lock_name) if defined($lock_name); | ||
| 70 | throw $_[0]; | ||
| 71 | }; | ||
| 72 | |||
| 73 | $self->release_lock($lock_name) if defined($lock_name); | ||
| 74 | |||
| 75 | $self->post_run(); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub generate_crond { | ||
| 79 | my ($self, %opts) = @_; | ||
| 80 | |||
| 81 | my $cron_pkg = ref($self); | ||
| 82 | |||
| 83 | my $methods = $self->get_cron_methods(); | ||
| 84 | |||
| 85 | print "MAILTO=\"$opts{'mail_to'}\"\n" if exists($opts{'mail_to'}); | ||
| 86 | print "CONTENT_TYPE=\"text/plain; charset=utf-8\"\n"; | ||
| 87 | print "\n"; | ||
| 88 | |||
| 89 | my $cron_cmd = 'perl' | ||
| 90 | . (exists($opts{'framework_path'}) ? " -I$opts{'framework_path'}" : '') | ||
| 91 | . (exists($opts{'application_path'}) ? " -I$opts{'application_path'}" : '') | ||
| 92 | . " -M$cron_pkg -e'$cron_pkg->new->do'"; | ||
| 93 | |||
| 94 | my ($cur_user) = getpwuid($<); | ||
| 95 | my $user = $opts{'user'} || $cur_user; | ||
| 96 | foreach my $path (sort keys(%$methods)) { | ||
| 97 | foreach my $method (sort keys(%{$methods->{$path}})) { | ||
| 98 | print join("\t", $methods->{$path}{$method}{'time'}, $user, "$cron_cmd $path $method") . "\n\n"; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub get_lock { | ||
| 104 | my ($self, $name) = @_; | ||
| 105 | |||
| 106 | $self->{'__LOCKS__'}{$name}{'file'} = "/tmp/${>}_${name}.lock"; | ||
| 107 | |||
| 108 | open($self->{'__LOCKS__'}{$name}{'fh'}, '>', $self->{'__LOCKS__'}{$name}{'file'}) | ||
| 109 | || throw gettext('Cannot create lock file "%s"', $self->{'__LOCKS__'}{$name}{'file'}); | ||
| 110 | |||
| 111 | return flock($self->{'__LOCKS__'}{$name}{'fh'}, LOCK_EX | LOCK_NB); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub release_lock { | ||
| 115 | my ($self, $name) = @_; | ||
| 116 | |||
| 117 | return unless exists($self->{'__LOCKS__'}{$name}); | ||
| 118 | |||
| 119 | flock($self->{'__LOCKS__'}{$name}{'fh'}, LOCK_UN); | ||
| 120 | close($self->{'__LOCKS__'}{$name}{'fh'}); | ||
| 121 | unlink($self->{'__LOCKS__'}{$name}{'file'}); | ||
| 122 | delete($self->{'__LOCKS__'}{$name}); | ||
| 123 | |||
| 124 | return TRUE; | ||
| 125 | } | ||
| 126 | |||
| 127 | TRUE; | ||
| 128 | |||
| 129 | __END__ |