Difference between revisions of "Cron Events and Simple email Reports"

From EPrints Documentation
Jump to: navigation, search
(Note buggy behaviour)
m
 
Line 8: Line 8:
 
In the lib/plugins/EPrints/Plugin/Event directory create your main plugin with the following:
 
In the lib/plugins/EPrints/Plugin/Event directory create your main plugin with the following:
  
  Package EPrints::Plugin::Event::EPrintsReportEx;
+
<source lang=perl>
 
+
package EPrints::Plugin::Event::EPrintsReportEx;
  use EPrints::Plugin::Event;
+
 
 
+
use EPrints::Plugin::Event;
  @ISA = qw( EPrints::Plugin::Event );
+
 
 
+
@ISA = qw( EPrints::Plugin::Event );
  sub action_send_email  
+
 
  {
+
sub action_send_email  
        my ( $self ) = @_;
+
{
       
+
    my ( $self ) = @_;
  }
+
 
 
+
}
  sub generate_content
+
 
  {
+
sub generate_content
        my ( $self ) = @_;
+
{
       
+
    my ( $self ) = @_;
  }
+
 
 
+
}
  1;
+
 
 +
1;
 +
</source>
  
 
= Step 2: Create your package =
 
= Step 2: Create your package =
Line 34: Line 36:
 
= Step 3: Search for EPrints Added in the Last Day =  
 
= Step 3: Search for EPrints Added in the Last Day =  
  
In your event fill the generate_content routine with the following:
+
In your event fill the <code>generate_content</code> routine with the following:
 +
 
 +
<source lang=perl>
 +
my $repository = $self->{repository};
 +
my $xml = $repository->xml;
 +
 
 +
my $content = $xml->create_document_fragment();
 +
my $h1 = $xml->create_element("h1");
 +
$content->appendChild($h1);
 +
$h1->appendChild($repository->make_text("EPrints Added in the Last Day"));
 +
 
 +
my $ds = $repository->dataset( "eprint" );
 +
 
 +
my $date = time() - 86400; # 1 day ago
 +
$date = EPrints::Time::iso_date( $date ) . " 00:00:00";
  
        my $repository = $self->{repository};
+
my $eprint_section;
        my $xml = $repository->xml;
+
 
 
+
$ds->search(
        my $content = $xml->create_document_fragment();
+
    custom_order => "-datestamp",
        my $h1 = $xml->create_element("h1");
+
)->map(sub {
        $content->appendChild($h1);
+
    my( undef, undef, $item ) = @_;
        $h1->appendChild($repository->make_text("EPrints Added in the Last Day"));
+
    return if(!defined $item);
 
 
        my $ds = $repository->dataset( "eprint" );
 
 
 
        my $date = time() - 86400; # 1 day ago
 
        $date = EPrints::Time::iso_date( $date ) . " 00:00:00";
 
 
 
        my $eprint_section;
 
 
 
        $ds->search(
 
                custom_order => "-datestamp",
 
        )->map(sub {
 
                my( undef, undef, $item ) = @_;
 
                return if(!defined $item);
 
 
    
 
    
                my $ep_date = $item->get_value("datestamp");
+
    my $ep_date = $item->get_value("datestamp");
 
    
 
    
                last if($ep_date lt $date);
+
    last if($ep_date lt $date);
 
    
 
    
                if (!defined $eprint_section) {
+
    if( !defined $eprint_section )
                        $eprint_section = $xml->create_document_fragment();
+
    {
                }
+
        $eprint_section = $xml->create_document_fragment();
                $eprint_section->appendChild($item->render_value("title"));
+
    }
                $eprint_section->appendChild($item->render_citation_link());
+
    $eprint_section->appendChild( $item->render_value("title") );
                $eprint_section->appendChild($repository->make_text(" ("));
+
    $eprint_section->appendChild( $item->render_citation_link() );
                $eprint_section->appendChild($item->render_value("eprint_status"));
+
    $eprint_section->appendChild( $repository->make_text(" (") );
                $eprint_section->appendChild($repository->make_text(")"));
+
    $eprint_section->appendChild( $item->render_value("eprint_status") );
                $eprint_section->appendChild($xml->create_element("br"));
+
    $eprint_section->appendChild( $repository->make_text(")") );
                $eprint_section->appendChild($xml->create_element("br"));
+
    $eprint_section->appendChild( $xml->create_element("br") );
 +
    $eprint_section->appendChild( $xml->create_element("br") );
 
    
 
    
        });
+
});
 
    
 
    
        if (defined $eprint_section) {
+
if( defined $eprint_section ) {
                $content->appendChild($eprint_section);
+
    $content->appendChild($eprint_section);
        } else {
+
} else {
                $content->appendChild($repository->make_text("Nothing has happened!"));
+
    $content->appendChild($repository->make_text("Nothing has happened!"));
        }
+
}
 
+
 
        return $content;
+
return $content;
 +
</source>
  
 
=Step 4: Email the report to the repository admins=
 
=Step 4: Email the report to the repository admins=
  
In the action_send_email sub routine, add the following code:
+
In the <code>action_send_email</code> sub routine, add the following code:
 +
 
 +
<source lang=perl>
 +
my $repository = $self->{repository};
 +
 
 +
my $mail_body = $self->generate_content();
 +
my $subject = "id/report_email_title";
 +
 
 +
my $ds = $repository->dataset( "user" );
 +
 
 +
$ds->search(
 +
    filters => [
 +
        { meta_fields => [qw( usertype )], value => "admin" },
 +
    ],
 +
)->map(sub {
 +
    my( undef, undef, $user ) = @_;
 +
    $user->mail( $subject, $mail_body,undef,undef );
 +
});
  
        my $repository = $self->{repository};
+
return 1;
 
+
</source>
        my $mail_body = $self->generate_content();
 
 
 
        my $subject = "id/report_email_title";
 
 
 
        my $ds = $repository->dataset( "user" );
 
 
 
        $ds->search(
 
                filters => [
 
                        { meta_fields => [qw( usertype )], value => "admin" },
 
                ],
 
        )->map(sub {
 
                my( undef, undef, $user ) = @_;
 
                $user->mail( $subject, $mail_body,undef,undef );
 
        });
 
 
 
        return 1;
 
  
 
= Step 5: Set up the crontab to email you this report =
 
= Step 5: Set up the crontab to email you this report =
Line 111: Line 117:
 
To enable cron we need to add the following to the enable method:
 
To enable cron we need to add the following to the enable method:
  
        EPrints::DataObj::EventQueue->create_unique( $repository, {
+
<source lang=perl>
                pluginid => "Event",
+
EPrints::DataObj::EventQueue->create_unique( $repository, {
                action => "cron",
+
    pluginid => "Event",
                params => ["0,15,30,45,60 * * * *",
+
    action => "cron",
                        "Event::EPrintsReportEx",
+
    params => [
                        "action_send_email",
+
        "0,15,30,45,60 * * * *",
                ],
+
        "Event::EPrintsReportEx",
        });
+
        "action_send_email",
 +
    ],
 +
});
 +
</source>
  
 
This will send you an update email every 15 minutes, note that EPrints uses the standard cron syntax so you can change this as you will.  
 
This will send you an update email every 15 minutes, note that EPrints uses the standard cron syntax so you can change this as you will.  
Line 126: Line 135:
 
We also need to disable this event again when the package is removed, to do this add the following to the disable method:
 
We also need to disable this event again when the package is removed, to do this add the following to the disable method:
  
        my $event = EPrints::DataObj::EventQueue->new_from_hash( $repository, {
+
<source lang=perl>
                pluginid => "Event",
+
my $event = EPrints::DataObj::EventQueue->new_from_hash( $repository, {
                action => "cron",
+
    pluginid => "Event",
                params => ["0,15,30,45,60 * * * *",
+
    action => "cron",
                        "Event::EPrintsReportEx",
+
    params => [
                        "action_send_email",
+
        "0,15,30,45,60 * * * *",
                ],
+
        "Event::EPrintsReportEx",
        });
+
        "action_send_email",
        $event->delete if (defined $event);
+
    ],
 +
});
 +
$event->delete if (defined $event);
 +
</source>
  
 
Note that the 2 events and cron syntax's MUST match.
 
Note that the 2 events and cron syntax's MUST match.

Latest revision as of 09:52, 29 January 2025

NOTE On EPrints v3.3.14 and lower, day-of-week or day-of-month events may actually run on a daily basis. See: https://github.com/eprints/eprints/issues/313 for details and patches.

In this exercise we are going to create an Event plug-in which will run every day and produce a report of all the EPrints which have been added in the last day.

Step 1: Create the Event Plug-In

In the lib/plugins/EPrints/Plugin/Event directory create your main plugin with the following:

package EPrints::Plugin::Event::EPrintsReportEx;

use EPrints::Plugin::Event;

@ISA = qw( EPrints::Plugin::Event );

sub action_send_email 
{
    my ( $self ) = @_;

}

sub generate_content
{
    my ( $self ) = @_;

}

1;

Step 2: Create your package

Create your package and don't forget to enable the Event Plugin according to http://wiki.eprints.org/w/My_First_Bazaar_Package

Step 3: Search for EPrints Added in the Last Day

In your event fill the generate_content routine with the following:

my $repository = $self->{repository};
my $xml = $repository->xml;

my $content = $xml->create_document_fragment();
my $h1 = $xml->create_element("h1");
$content->appendChild($h1);
$h1->appendChild($repository->make_text("EPrints Added in the Last Day"));

my $ds = $repository->dataset( "eprint" );

my $date = time() - 86400; # 1 day ago
$date = EPrints::Time::iso_date( $date ) . " 00:00:00";

my $eprint_section;

$ds->search(
    custom_order => "-datestamp",
)->map(sub {
    my( undef, undef, $item ) = @_;
    return if(!defined $item);
   
    my $ep_date = $item->get_value("datestamp");
  
    last if($ep_date lt $date);
  
    if( !defined $eprint_section )
    {
        $eprint_section = $xml->create_document_fragment();
    }
    $eprint_section->appendChild( $item->render_value("title") );
    $eprint_section->appendChild( $item->render_citation_link() );
    $eprint_section->appendChild( $repository->make_text(" (") );
    $eprint_section->appendChild( $item->render_value("eprint_status") );
    $eprint_section->appendChild( $repository->make_text(")") );
    $eprint_section->appendChild( $xml->create_element("br") );
    $eprint_section->appendChild( $xml->create_element("br") );
  
});
  
if( defined $eprint_section ) {
    $content->appendChild($eprint_section);
} else {
    $content->appendChild($repository->make_text("Nothing has happened!"));
}

return $content;

Step 4: Email the report to the repository admins

In the action_send_email sub routine, add the following code:

my $repository = $self->{repository};

my $mail_body = $self->generate_content();
my $subject = "id/report_email_title";

my $ds = $repository->dataset( "user" );

$ds->search(
    filters => [
        { meta_fields => [qw( usertype )], value => "admin" },
    ],
)->map(sub {
    my( undef, undef, $user ) = @_;
    $user->mail( $subject, $mail_body,undef,undef );
});

return 1;

Step 5: Set up the crontab to email you this report

In order to set up the cron event we need to add some code to an EPMC screen related to this package, follow the instructions @ http://wiki.eprints.org/w/XML_manipulation_of_the_EPrints_Workflow_using_a_Bazaar_Package on creating an EPMC screen with and enable and disable method.

To enable cron we need to add the following to the enable method:

EPrints::DataObj::EventQueue->create_unique( $repository, {
    pluginid => "Event",
    action => "cron",
    params => [
        "0,15,30,45,60 * * * *",
        "Event::EPrintsReportEx",
        "action_send_email",
    ],
});

This will send you an update email every 15 minutes, note that EPrints uses the standard cron syntax so you can change this as you will.

A guide on using cron can be found @ http://en.wikipedia.org/wiki/Cron

We also need to disable this event again when the package is removed, to do this add the following to the disable method:

my $event = EPrints::DataObj::EventQueue->new_from_hash( $repository, {
    pluginid => "Event",
    action => "cron",
    params => [
        "0,15,30,45,60 * * * *",
        "Event::EPrintsReportEx",
        "action_send_email",
    ],
});
$event->delete if (defined $event);

Note that the 2 events and cron syntax's MUST match.

Step 6 - Package Test and Wait for Email

Finish up, package and enable your code. Note that we have not defined any nice phrases here that will be needed to make a proper package.