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

From EPrints Documentation
Jump to: navigation, search
(Step 4: Email the report to the repository admins)
(Note buggy behaviour)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
[[Category:EPrints_Bazaar]]
 
[[Category:EPrints_Bazaar]]
 +
'''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.
 
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.
Line 86: Line 87:
  
 
         my $repository = $self->{repository};
 
         my $repository = $self->{repository};
 
 
        my $receiver = $repository->user(7);
 
        return unless( defined $receiver );
 
 
    
 
    
 
         my $mail_body = $self->generate_content();
 
         my $mail_body = $self->generate_content();

Latest revision as of 13:44, 14 May 2015

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.