| 1 | package Beam::Runner; | ||
| 2 | 1 | our $VERSION = '0.016'; | |
| 3 | # ABSTRACT: Configure, list, document, and execute runnable task objects | ||
| 4 | |||
| 5 | #pod =head1 SYNOPSIS | ||
| 6 | #pod | ||
| 7 | #pod beam run <container> <task> [<args...>] | ||
| 8 | #pod beam list | ||
| 9 | #pod beam list <container> | ||
| 10 | #pod beam help <container> <task> | ||
| 11 | #pod beam help | ||
| 12 | #pod | ||
| 13 | #pod =head1 DESCRIPTION | ||
| 14 | #pod | ||
| 15 | #pod This distribution is an execution and organization system for runnable | ||
| 16 | #pod objects (tasks). This allows you to prepare a list of runnable tasks in | ||
| 17 | #pod configuration files and then execute them. This also allows easy | ||
| 18 | #pod discovery of configuration files and objects, and allows you to document | ||
| 19 | #pod your objects for your users. | ||
| 20 | #pod | ||
| 21 | #pod =head2 Tasks | ||
| 22 | #pod | ||
| 23 | #pod A task is an object that consumes the L<Beam::Runnable> role. This role | ||
| 24 | #pod requires only a C<run()> method be implemented in the class. This | ||
| 25 | #pod C<run()> method should accept all the arguments given on the command | ||
| 26 | #pod line. It can parse GNU-style options out of this array using | ||
| 27 | #pod L<Getopt::Long/GetOptionsFromArray>. | ||
| 28 | #pod | ||
| 29 | #pod Task modules can compose additional roles to easily add more features, | ||
| 30 | #pod like adding a timeout with L<Beam::Runnable::Timeout::Alarm>. | ||
| 31 | #pod | ||
| 32 | #pod Task modules are expected to have documentation that will be displayed | ||
| 33 | #pod by the C<beam list> and C<beam help> commands. The C<beam list> command | ||
| 34 | #pod will display the C<NAME> section of the documentation, and the C<beam | ||
| 35 | #pod help> command will display the C<NAME>, C<SYNOPSIS>, C<DESCRIPTION>, | ||
| 36 | #pod C<ARGUMENTS>, C<OPTIONS>, C<ENVIRONMENT>, and C<SEE ALSO> sections of | ||
| 37 | #pod the documentation. | ||
| 38 | #pod | ||
| 39 | #pod =head2 Configuration Files | ||
| 40 | #pod | ||
| 41 | #pod The configuration file is a L<Beam::Wire> container file that describes | ||
| 42 | #pod objects. Some of these objects are marked as executable tasks by | ||
| 43 | #pod consuming the L<Beam::Runnable> role. | ||
| 44 | #pod | ||
| 45 | #pod The container file can have a special entry called C<$summary> which | ||
| 46 | #pod has a short summary that will be displayed when using the C<beam list> | ||
| 47 | #pod command. | ||
| 48 | #pod | ||
| 49 | #pod Here's an example container file that has a summary, configures | ||
| 50 | #pod a L<DBIx::Class> schema (using the schema class for CPAN Testers: | ||
| 51 | #pod L<CPAN::Testers::Schema>), and configures a runnable task called | ||
| 52 | #pod C<to_metabase> located in the class | ||
| 53 | #pod C<CPAN::Testers::Backend::Migrate::ToMetabase>: | ||
| 54 | #pod | ||
| 55 | #pod # migrate.yml | ||
| 56 | #pod $summary: Migrate data between databases | ||
| 57 | #pod | ||
| 58 | #pod _schema: | ||
| 59 | #pod $class: CPAN::Testers::Schema | ||
| 60 | #pod $method: connect_from_config | ||
| 61 | #pod | ||
| 62 | #pod to_metabase: | ||
| 63 | #pod $class: CPAN::Testers::Backend::Migrate::ToMetabase | ||
| 64 | #pod schema: | ||
| 65 | #pod $ref: _schema | ||
| 66 | #pod | ||
| 67 | #pod For more information about container files, see L<the Beam::Wire | ||
| 68 | #pod documentation|Beam::Wire>. | ||
| 69 | #pod | ||
| 70 | #pod =head1 QUICKSTART | ||
| 71 | #pod | ||
| 72 | #pod Here's a short tutorial for getting started with C<Beam::Runner>. If you | ||
| 73 | #pod want to try it yourself, start with an empty directory. | ||
| 74 | #pod | ||
| 75 | #pod =head2 Create a Task | ||
| 76 | #pod | ||
| 77 | #pod To create a task, make a Perl module that uses the L<Beam::Runnable> role | ||
| 78 | #pod and implements a C<run> method. For an example, let's create a task that | ||
| 79 | #pod prints C<Hello, World!> to the screen. | ||
| 80 | #pod | ||
| 81 | #pod package My::Runnable::Greeting; | ||
| 82 | #pod use Moo; | ||
| 83 | #pod with 'Beam::Runnable'; | ||
| 84 | #pod sub run { | ||
| 85 | #pod my ( $self, @args ) = @_; | ||
| 86 | #pod print "Hello, World!\n"; | ||
| 87 | #pod } | ||
| 88 | #pod 1; | ||
| 89 | #pod | ||
| 90 | #pod If you're following along, save this in the | ||
| 91 | #pod C<lib/My/Runnable/Greeting.pm> file. | ||
| 92 | #pod | ||
| 93 | #pod =head2 Create a Configuration File | ||
| 94 | #pod | ||
| 95 | #pod Now that we have a task to run, we need to create a configuration file | ||
| 96 | #pod (or a "container"). The configuration file is a YAML file that describes | ||
| 97 | #pod all the tasks we can run. Let's create an C<etc> directory and name our | ||
| 98 | #pod container file C<etc/greet.yml>. | ||
| 99 | #pod | ||
| 100 | #pod Inside this file, we define our task. We have to give our task a simple | ||
| 101 | #pod name, like C<hello>. Then we have to say what task class to run (in our case, | ||
| 102 | #pod C<My::Runnable::Greeting>). | ||
| 103 | #pod | ||
| 104 | #pod hello: | ||
| 105 | #pod $class: My::Runnable::Greeting | ||
| 106 | #pod | ||
| 107 | #pod =head2 Run the Task | ||
| 108 | #pod | ||
| 109 | #pod Now we can run our task. Before we do, we need to tell C<Beam::Runner> where | ||
| 110 | #pod to find our code and our configuration by setting some environment variables: | ||
| 111 | #pod | ||
| 112 | #pod $ export PERL5LIB=lib:$PERL5LIB | ||
| 113 | #pod $ export BEAM_PATH=etc | ||
| 114 | #pod | ||
| 115 | #pod The C<PERL5LIB> environment variable adds directories for C<perl> to search | ||
| 116 | #pod for modules (like our task module). The C<BEAM_PATH> environment variable | ||
| 117 | #pod adds directories to search for configuration files (like ours). | ||
| 118 | #pod | ||
| 119 | #pod To validate that our environment variables are set correctly, we can list the | ||
| 120 | #pod tasks: | ||
| 121 | #pod | ||
| 122 | #pod $ beam list | ||
| 123 | #pod greet | ||
| 124 | #pod - hello -- My::Runnable::Greeting | ||
| 125 | #pod | ||
| 126 | #pod The C<beam list> command looks through our C<BEAM_PATH> directory, opens | ||
| 127 | #pod all the configuration files it finds, and lists all the | ||
| 128 | #pod L<Beam::Runnable> objects inside (helpfully giving us the module name for us | ||
| 129 | #pod to find documentation). | ||
| 130 | #pod | ||
| 131 | #pod Then, to run the command, we use C<beam run> and give it the configuration file | ||
| 132 | #pod (C<greet>) and the task (C<hello>): | ||
| 133 | #pod | ||
| 134 | #pod $ beam run greet hello | ||
| 135 | #pod Hello, World! | ||
| 136 | #pod | ||
| 137 | #pod =head2 Adding Documentation | ||
| 138 | #pod | ||
| 139 | #pod Part of the additional benefits of defining tasks in L<Beam::Runnable> modules | ||
| 140 | #pod is that the C<beam help> command will show the documentation for the task. To | ||
| 141 | #pod do this, we must add documentation to our module. | ||
| 142 | #pod | ||
| 143 | #pod This documentation is done as L<POD|perlpod>, Perl's system of documentation. | ||
| 144 | #pod Certain sections of the documentation will be shown: C<NAME>, C<SYNOPSIS>, | ||
| 145 | #pod C<DESCRIPTION>, C<ARGUMENTS>, C<OPTIONS>, and C<SEE ALSO>. | ||
| 146 | #pod | ||
| 147 | #pod =head1 NAME | ||
| 148 | #pod | ||
| 149 | #pod My::Runnable::Greeting - Greet the user | ||
| 150 | #pod | ||
| 151 | #pod =head1 SYNOPSIS | ||
| 152 | #pod | ||
| 153 | #pod beam run greet hello | ||
| 154 | #pod | ||
| 155 | #pod =head1 DESCRIPTION | ||
| 156 | #pod | ||
| 157 | #pod This task greets the user warmly and then exits. | ||
| 158 | #pod | ||
| 159 | #pod =head1 ARGUMENTS | ||
| 160 | #pod | ||
| 161 | #pod No arguments are allowed during a greeting. | ||
| 162 | #pod | ||
| 163 | #pod =head1 OPTIONS | ||
| 164 | #pod | ||
| 165 | #pod Greeting warmly is the only option. | ||
| 166 | #pod | ||
| 167 | #pod =head1 SEE ALSO | ||
| 168 | #pod | ||
| 169 | #pod L<Beam::Runnable> | ||
| 170 | #pod | ||
| 171 | #pod If we add this documentation to our C<lib/My/Runnable/Greeting.pm> file, | ||
| 172 | #pod we can then run C<beam help> to see the documentation: | ||
| 173 | #pod | ||
| 174 | #pod $ beam help greet hello | ||
| 175 | #pod NAME | ||
| 176 | #pod My::Runnable::Greeting - Greet the user | ||
| 177 | #pod | ||
| 178 | #pod SYNOPSIS | ||
| 179 | #pod beam run greet hello | ||
| 180 | #pod | ||
| 181 | #pod DESCRIPTION | ||
| 182 | #pod This task greets the user warmly and then exits. | ||
| 183 | #pod | ||
| 184 | #pod ARGUMENTS | ||
| 185 | #pod No arguments are allowed during a greeting. | ||
| 186 | #pod | ||
| 187 | #pod OPTIONS | ||
| 188 | #pod Greeting warmly is the only option. | ||
| 189 | #pod | ||
| 190 | #pod SEE ALSO | ||
| 191 | #pod Beam::Runnable | ||
| 192 | #pod | ||
| 193 | #pod The C<beam list> command will also use our new documentation to show the C<NAME> | ||
| 194 | #pod section: | ||
| 195 | #pod | ||
| 196 | #pod $ beam list | ||
| 197 | #pod greet | ||
| 198 | #pod - hello -- My::Runnable::Greeting - Greet the user | ||
| 199 | #pod | ||
| 200 | #pod =head2 Going Further | ||
| 201 | #pod | ||
| 202 | #pod For more information on how to use the configuration file to create more | ||
| 203 | #pod complex objects like database connections, see | ||
| 204 | #pod L<Beam::Wire::Help::Config>. | ||
| 205 | #pod | ||
| 206 | #pod To learn how to run your tasks using a distributed job queue to | ||
| 207 | #pod parallelize and improve performance, see L<Beam::Minion>. | ||
| 208 | #pod | ||
| 209 | #pod =head1 SEE ALSO | ||
| 210 | #pod | ||
| 211 | #pod L<beam>, L<Beam::Runnable>, L<Beam::Wire> | ||
| 212 | #pod | ||
| 213 | #pod =cut | ||
| 214 | |||
| 215 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 216 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | 1 | 1; | |
| 221 | |||
| 222 | __END__ | ||