diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..72f82ba --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,30 @@ +name: test +on: + push: + branches: + - 'master' + tags-ignore: + - '*' + pull_request: +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: ['ubuntu-latest', 'macOS-latest', 'windows-latest'] + perl: [ '5.30', '5.28', '5.10' ] + exclude: + # not supported on actions-setup-perl + - os: windows-latest + perl: '5.10' + name: Perl ${{ matrix.perl }} on ${{ matrix.os }} + steps: + - uses: actions/checkout@v1 + - name: Setup perl + uses: shogo82148/actions-setup-perl@v1 + with: + perl-version: ${{ matrix.perl }} + - run: perl -V + - run: cpanm --installdeps . + - run: prove -lr --timer t xt diff --git a/.gitignore b/.gitignore index be1036b..a6cfb87 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,14 @@ cover_db/ TheSchwartz*.tar.gz MYMETA.yml MYMETA.json -META.json META.yml *.bak Notes.md +/TheSchwartz-* +/.build +/_build_params +/Build +/Build.bat +!Build/ +!META.json +!LICENSE diff --git a/Build.PL b/Build.PL index da21325..16855c4 100644 --- a/Build.PL +++ b/Build.PL @@ -1,46 +1,12 @@ -use 5.008; +# ========================================================================= +# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. +# DO NOT EDIT DIRECTLY. +# ========================================================================= + +use 5.008_001; use strict; -use warnings; -use Module::Build; -my $class = Module::Build->subclass( - class => 'My::Builder', - code => q{ - sub ACTION_authortest { - my ($self) = @_; - $self->test_files( qw< xt > ); - $self->recursive_test_files(1); - $self->depends_on('test'); - return; - } - sub ACTION_distdir { - my ($self) = @_; - $self->depends_on('authortest'); - return $self->SUPER::ACTION_distdir(); - } - } -); +use Module::Build::Tiny 0.035; -my $builder = $class->new( - module_name => 'TheSchwartz', - license => 'perl', - dist_author => 'Six Apart ', - dist_version_from => 'lib/TheSchwartz.pm', - configure_requires => { 'Module::Build' => 0 }, - build_requires => { - 'Module::Build' => 0, - 'Test::More' => 0, - 'Data::ObjectDriver' => 0.04, - 'Digest::MD5' => 0, - 'Storable' => 0, - }, - requires => { - 'Data::ObjectDriver' => 0.04, - 'Digest::MD5' => 0, - 'Storable' => 0, - }, - add_to_cleanup => [ 'TheSchwartz-*', 'tmp', 'blib', '*.bak', 'META.*' ], - script_files => ['bin/schwartzmon'], -); +Build_PL(); -$builder->create_build_script(); diff --git a/CHANGES b/Changes similarity index 77% rename from CHANGES rename to Changes index 582aca8..c609fe4 100644 --- a/CHANGES +++ b/Changes @@ -1,3 +1,27 @@ +{{$NEXT}} + +1.17 2021-12-30T13:21:45Z + + - schwartzmon: 'Not an ARRAY reference' in DBSet::add for at least v5.16.3 GH#12 (@trwww++) + +1.16 2021-07-16T07:49:43Z + + - Add ability to disable job randomization GH#9 (@jamadam++) + +1.15 2020-01-28T06:19:54Z + + - Fix tests on win32 GH#4 (@charsbar++) + +1.14 2019-10-17T17:51:40Z + + - Added missing prereq Class::Accessor::Fast as suggested by CPANTS. GH#1 (@manwar++) + +1.13 2019-08-07T16:28:14Z + + - the module maintainer has been changed to Takumi Akiyama (AKIYM) + - fix tests fail without '.' in @INC. RT #124610 + - require perl-5.8.1 or later + 1.12 Mon Mar 30 2015 - Fix replace_job hanging & postgresql issues. RT #65712 - Add strict_remove_ability to prevent auto resetting of abilities. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..32ffd2e --- /dev/null +++ b/LICENSE @@ -0,0 +1,378 @@ +This software is copyright (c) 2019 by , Six Apart Ltd, cpan@sixapart.com. All. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +Terms of the Perl programming language system itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +--- The GNU General Public License, Version 1, February 1989 --- + +This software is Copyright (c) 2019 by , Six Apart Ltd, cpan@sixapart.com. All. + +This is free software, licensed under: + + The GNU General Public License, Version 1, February 1989 + + GNU GENERAL PUBLIC LICENSE + Version 1, February 1989 + + Copyright (C) 1989 Free Software Foundation, Inc. + 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The license agreements of most software companies try to keep users +at the mercy of those companies. By contrast, our General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. The +General Public License applies to the Free Software Foundation's +software and to any other program whose authors commit to using it. +You can use it for your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Specifically, the General Public License is designed to make +sure that you have the freedom to give away or sell copies of free +software, that you receive source code or can get it if you want it, +that you can change the software or use pieces of it in new free +programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of a such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must tell them their rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any program or other work which +contains a notice placed by the copyright holder saying it may be +distributed under the terms of this General Public License. The +"Program", below, refers to any such program or work, and a "work based +on the Program" means either the Program or any work containing the +Program or a portion of it, either verbatim or with modifications. Each +licensee is addressed as "you". + + 1. You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this +General Public License and to the absence of any warranty; and give any +other recipients of the Program a copy of this General Public License +along with the Program. You may charge a fee for the physical act of +transferring a copy. + + 2. You may modify your copy or copies of the Program or any portion of +it, and copy and distribute such modifications under the terms of Paragraph +1 above, provided that you also do the following: + + a) cause the modified files to carry prominent notices stating that + you changed the files and the date of any change; and + + b) cause the whole of any work that you distribute or publish, that + in whole or in part contains the Program or any part thereof, either + with or without modifications, to be licensed at no charge to all + third parties under the terms of this General Public License (except + that you may choose to grant warranty protection to some or all + third parties, at your option). + + c) If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use + in the simplest and most usual way, to print or display an + announcement including an appropriate copyright notice and a notice + that there is no warranty (or else, saying that you provide a + warranty) and that users may redistribute the program under these + conditions, and telling the user how to view a copy of this General + Public License. + + d) You may charge a fee for the physical act of transferring a + copy, and you may at your option offer warranty protection in + exchange for a fee. + +Mere aggregation of another independent work with the Program (or its +derivative) on a volume of a storage or distribution medium does not bring +the other work under the scope of these terms. + + 3. You may copy and distribute the Program (or a portion or derivative of +it, under Paragraph 2) in object code or executable form under the terms of +Paragraphs 1 and 2 above provided that you also do one of the following: + + a) accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of + Paragraphs 1 and 2 above; or, + + b) accompany it with a written offer, valid for at least three + years, to give any third party free (except for a nominal charge + for the cost of distribution) a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of + Paragraphs 1 and 2 above; or, + + c) accompany it with the information you received as to where the + corresponding source code may be obtained. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form alone.) + +Source code for a work means the preferred form of the work for making +modifications to it. For an executable file, complete source code means +all the source code for all modules it contains; but, as a special +exception, it need not include source code for modules which are standard +libraries that accompany the operating system on which the executable +file runs, or for standard header files or definitions files that +accompany that operating system. + + 4. You may not copy, modify, sublicense, distribute or transfer the +Program except as expressly provided under this General Public License. +Any attempt otherwise to copy, modify, sublicense, distribute or transfer +the Program is void, and will automatically terminate your rights to use +the Program under this License. However, parties who have received +copies, or rights to use copies, from you under this General Public +License will not have their licenses terminated so long as such parties +remain in full compliance. + + 5. By copying, distributing or modifying the Program (or any work based +on the Program) you indicate your acceptance of this license to do so, +and all its terms and conditions. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these +terms and conditions. You may not impose any further restrictions on the +recipients' exercise of the rights granted herein. + + 7. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of the license which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +the license, you may choose any version ever published by the Free Software +Foundation. + + 8. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to humanity, the best way to achieve this is to make it +free software which everyone can redistribute and change under these +terms. + + To do so, attach the following notices to the program. It is safest to +attach them to the start of each source file to most effectively convey +the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 1, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19xx name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the +appropriate parts of the General Public License. Of course, the +commands you use may be called something other than `show w' and `show +c'; they could even be mouse-clicks or menu items--whatever suits your +program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + program `Gnomovision' (a program to direct compilers to make passes + at assemblers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +That's all there is to it! + + +--- The Artistic License 1.0 --- + +This software is Copyright (c) 2019 by , Six Apart Ltd, cpan@sixapart.com. All. + +This is free software, licensed under: + + The Artistic License 1.0 + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of +the package the right to use and distribute the Package in a more-or-less +customary fashion, plus the right to make reasonable modifications. + +Definitions: + + - "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through + textual modification. + - "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. + - "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. + - "You" is you, if you're thinking about copying or distributing this Package. + - "Reasonable copying fee" is whatever you can justify on the basis of media + cost, duplication charges, time of people involved, and so on. (You will + not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) + - "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived +from the Public Domain or from the Copyright Holder. A Package modified in such +a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided that +you insert a prominent notice in each changed file stating how and when you +changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or an + equivalent medium, or placing the modifications on a major archive site + such as ftp.uu.net, or by allowing the Copyright Holder to include your + modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict with + standard executables, which must also be provided, and provide a separate + manual page for each non-standard executable that clearly documents how it + differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where to + get the Standard Version. + + b) accompany the distribution with the machine-readable source of the Package + with your modifications. + + c) accompany any non-standard executables with their corresponding Standard + Version executables, giving the non-standard executables non-standard + names, and clearly documenting the differences in manual pages (or + equivalent), together with instructions on where to get the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this Package. You +may not charge a fee for this Package itself. However, you may distribute this +Package in aggregate with other (possibly commercial) programs as part of a +larger (possibly commercial) software distribution provided that you do not +advertise this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output +from the programs of this Package do not automatically fall under the copyright +of this Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +The End diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index 8414a56..0000000 --- a/MANIFEST +++ /dev/null @@ -1,58 +0,0 @@ -bin/schwartzmon -Build.PL -CHANGES -doc/http-mappings.txt -doc/notes.txt -doc/schema-postgres.sql -doc/schema.sql -extras/check_schwartz -extras/perl-TheSchwartz.spec -extras/thetop -lib/TheSchwartz.pm -lib/TheSchwartz/Error.pm -lib/TheSchwartz/ExitStatus.pm -lib/TheSchwartz/FuncMap.pm -lib/TheSchwartz/Job.pm -lib/TheSchwartz/JobHandle.pm -lib/TheSchwartz/Worker.pm -MANIFEST This list of files -MANIFEST.SKIP -META.yml -perltidyrc -README.md -server/bin/schwartzd -server/doc/deps.txt -server/doc/protocol.txt -server/t/00-start-ping.t -server/t/01-insert-and-get.t -server/t/lib/testlib.pl -t/05-job-ctor.t -t/api.t -t/cleanup.t -t/client-time-unsync.t -t/coalesce.t -t/dead-dbs.t -t/declined.t -t/empty-db.t -t/evenly-distribute.t -t/fail-working-multiple.t -t/funcid.t -t/grab-race.t -t/grab_and_work_on.t -t/high-funcid-starvation.t -t/insert-and-do.t -t/lib/db-common.pl -t/parallel-workers.t -t/priority.t -t/replace-abort.t -t/replace-with.t -t/retry-delay.t -t/schema-sqlite.sql -t/scoreboard.t -t/server-time.t -t/unique.t -t/work-before-funcids-exist.t -xt/perlcritic.t -xt/pod-coverage.t -xt/pod-spelling.t -xt/pod.t diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP deleted file mode 100644 index 6190fd3..0000000 --- a/MANIFEST.SKIP +++ /dev/null @@ -1,55 +0,0 @@ -MANIFEST.bak -ts1.db -ts2.db -build-stamp -install-stamp -.shipit -svn-commit.tmp - -# Avoid version control files. -\bRCS\b -\bCVS\b -,v$ -\B\.svn\b -\B\.git\b -\B\.gitignore\b - -# Avoid Makemaker generated and utility files. -\bMANIFEST\.bak -\bMakefile$ -\bblib/ -\bMakeMaker-\d -\bpm_to_blib$ - -# Avoid Module::Build generated and utility files. -\bBuild$ -\b_build/ -^Notes.md$ -\bTheSchwartz-[\d\.\_]+ - -# Avoid temp and backup files. -~$ -\.old$ -\#$ -\b\.# -\.git/ -\cover_db/ -^MYMETA.*$ - -# Avoid Devel::Cover and Devel::CoverX::Covered files. -\bcover_db\b -\bcovered\b - -# Avoid temp and backup files. -~$ -\.old$ -\#$ -\b\.# -\.bak$ -\.tmp$ -\.# -\.rej$ -\.swp$ -\.swo$ - - diff --git a/META.json b/META.json new file mode 100644 index 0000000..3127414 --- /dev/null +++ b/META.json @@ -0,0 +1,86 @@ +{ + "abstract" : "reliable job queue", + "author" : [ + ", Six Apart Ltd, cpan@sixapart.com. All" + ], + "dynamic_config" : 0, + "generated_by" : "Minilla/v3.1.12, CPAN::Meta::Converter version 2.150010", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "TheSchwartz", + "no_index" : { + "directory" : [ + "t", + "xt", + "inc", + "share", + "eg", + "examples", + "author", + "builder" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "Module::Build::Tiny" : "0.035" + } + }, + "develop" : { + "requires" : { + "Test::CPAN::Meta" : "0", + "Test::MinimumVersion::Fast" : "0.04", + "Test::PAUSE::Permissions" : "0.07", + "Test::Pod" : "1.41", + "Test::Spellunker" : "v0.2.7" + } + }, + "runtime" : { + "requires" : { + "Class::Accessor::Fast" : "0", + "Data::ObjectDriver" : "0.04", + "Digest::MD5" : "0", + "Storable" : "0", + "perl" : "5.008001" + } + }, + "test" : { + "requires" : { + "Test::More" : "0" + } + } + }, + "release_status" : "unstable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/akiym/TheSchwartz/issues" + }, + "homepage" : "https://github.com/akiym/TheSchwartz", + "repository" : { + "type" : "git", + "url" : "git://github.com/akiym/TheSchwartz.git", + "web" : "https://github.com/akiym/TheSchwartz" + } + }, + "version" : "1.17", + "x_authority" : "cpan:AKIYM", + "x_contributors" : [ + "Akira Sawada ", + "Brad Fitzpatrick ", + "Jeff Fearn ", + "Jeff Fearn ", + "Keita Jamadam Sugama ", + "Kenichi Ishigaki ", + "Mohammad S Anwar ", + "Six Apart Ltd. ", + "Takumi Akiyama ", + "Todd Wade " + ], + "x_serialization_backend" : "JSON::PP version 4.04", + "x_static_install" : 1 +} diff --git a/README.md b/README.md index 474577f..4c1ef93 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,345 @@ -TheSchwartz -============= +# NAME -**TheSchwartz** is a reliable job queue system. Your application can put jobs into the system, and your worker processes can pull jobs from the queue atomically to perform. Failed jobs can be left in the queue to retry later. +TheSchwartz - reliable job queue -**Abilities** specify what jobs a worker process can perform. Abilities are the names of *TheSchwartz::Worker* subclasses, as in the synopsis: the *MyWorker* class name is used to specify that the worker script can perform the job. When using the *TheSchwartz* client's *work* functions, the class-ability duality is used to automatically dispatch to the proper class to do the actual work. +# SYNOPSIS -TheSchwartz clients will also prefer to do jobs for unused abilities before reusing a particular ability, to avoid exhausting the supply of one kind of job while jobs of other types stack up. + # MyApp.pm + package MyApp; -Some jobs with high set-up times can be performed more efficiently if a group of related jobs are performed together. TheSchwartz offers a facility to **coalesce** jobs into groups, which a properly constructed worker can find and perform at once. For example, if your worker were delivering email, you might store the domain name from the recipient's address as the coalescing value. The worker that grabs that job could then batch deliver all the mail for that domain once it connects to that domain's mail server. + sub work_asynchronously { + my %args = @_; -INSTALLATION ------------- + my $client = TheSchwartz->new( databases => $DATABASE_INFO ); + $client->insert('MyWorker', \%args); + } -Just follow the usual procedure: - perl Build.PL - ./Build - ./Build test - ./Build install + # myworker.pl + package MyWorker; + use base qw( TheSchwartz::Worker ); -If you want to install a private copy of this module-suite in your home directory, then you should try to produce the initial Makefile with something like this command: + sub work { + my $class = shift; + my TheSchwartz::Job $job = shift; - perl Build.PL PREFIX=~/perl + print "Workin' hard or hardly workin'? Hyuk!!\n"; -See perldoc perlmodinstall for more information on installing modules. + $job->completed(); + } -SUPPORT -------- + package main; -Just follow the usual procedure: + my $client = TheSchwartz->new( databases => $DATABASE_INFO ); + $client->can_do('MyWorker'); + $client->work(); - perl Build.PL - ./Build +# DESCRIPTION -Questions, bug reports, useful code bits, and suggestions for this module should just be sent to JFEARN@cpan.org or open a ticket in the [CPAN RT](https://rt.cpan.org//Dist/Display.html?Queue=TheSchwartz) +TheSchwartz is a reliable job queue system. Your application can put jobs into +the system, and your worker processes can pull jobs from the queue atomically +to perform. Failed jobs can be left in the queue to retry later. -AVAILABILITY -------- -The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit (http://www.perl.com/CPAN/) to find a CPAN site near you. +_Abilities_ specify what jobs a worker process can perform. Abilities are the +names of `TheSchwartz::Worker` sub-classes, as in the synopsis: the `MyWorker` +class name is used to specify that the worker script can perform the job. When +using the `TheSchwartz` client's `work` functions, the class-ability duality +is used to automatically dispatch to the proper class to do the actual work. -The source is available on github (https://github.com/jfearn/TheSchwartz), patches should be sent as pull requests against this repository. +TheSchwartz clients will also prefer to do jobs for unused abilities before +reusing a particular ability, to avoid exhausting the supply of one kind of job +while jobs of other types stack up. +Some jobs with high setup times can be performed more efficiently if a group of +related jobs are performed together. TheSchwartz offers a facility to +_coalesce_ jobs into groups, which a properly constructed worker can find and +perform at once. For example, if your worker were delivering email, you might +store the domain name from the recipient's address as the coalescing value. The +worker that grabs that job could then batch deliver all the mail for that +domain once it connects to that domain's mail server. + +# USAGE + +## `TheSchwartz->new( %args )` + +Optional members of `%args` are: + +- `databases` + + An arrayref of database information. TheSchwartz workers can use multiple + databases, such that if any of them are unavailable, the worker will search for + appropriate jobs in the other databases automatically. + + Each member of the `databases` value should be a hashref containing either: + + - `dsn` + + The database DSN for this database. + + - `user` + + The user name to use when connecting to this database. + + - `pass` + + The password to use when connecting to this database. + + or + + - `driver` + + A `Data::ObjectDriver::Driver::DBI` object. + + See note below. + +- `verbose` + + A value indicating whether to log debug messages. If `verbose` is a coderef, + it is called to log debug messages. If `verbose` is not a coderef but is some + other true value, debug messages will be sent to `STDERR`. Otherwise, debug + messages will not be logged. + +- `prioritize` + + A value indicating whether to utilize the job 'priority' field when selecting + jobs to be processed. If unspecified, jobs will always be executed in a + randomized order. + +- `floor` + + A value indicating the minimum priority a job needs to be for this worker to + perform. If unspecified all jobs are considered. + +- `batch_size` + + A value indicating how many jobs should be fetched from the DB for consideration. + +- `driver_cache_expiration` + + Optional value to control how long database connections are cached for in seconds. + By default, connections are not cached. To re-use the same database connection for + five minutes, pass driver\_cache\_expiration => 300 to the constructor. Improves job + throughput in cases where the work to process a job is small compared to the database + connection set-up and tear-down time. + +- `retry_seconds` + + The number of seconds after which to try reconnecting to apparently dead + databases. If not given, TheSchwartz will retry connecting to databases after + 30 seconds. + +- `strict_remove_ability` + + By default when work\_once does not find a job it will reset current\_abilities to + all\_abilities and look for a job. Setting this option will prevent work\_once from + resetting abilities if it can't find a job for the current capabilities. + +## `$client->list_jobs( %args )` + +Returns a list of `TheSchwartz::Job` objects matching the given arguments. The +required members of `%args` are: + +- `funcname` + + the name of the function or a reference to an array of functions + +- `run_after` + + the value you want to check <= against on the run\_after column + +- `grabbed_until` + + the value you want to check <= against on the grabbed\_until column + +- `coalesce_op` + + defaults to '=', set it to whatever you want to compare the coalesce field too + if you want to search, you can use 'LIKE' + +- `coalesce` + + coalesce value to search for, if you set op to 'LIKE' you can use '%' here, + do remember that '%' searches anchored at the beginning of the string are + much faster since it is can do a btree index lookup + +- `want_handle` + + if you want all your jobs to be set up using a handle. defaults to true. + this option might be removed, as you should always have this on a Job object. + +- `jobid` + + if you want a specific job you can pass in it's ID and if it's available it + will be listed. + +It is important to remember that this function does not lock anything, it just +returns as many jobs as there is up to amount of databases \* $client->{batch\_size} + +## `$client->lookup_job( $handle_id )` + +Returns a `TheSchwartz::Job` corresponding to the given handle ID. + +## `$client->set_verbose( $verbose )` + +Sets the current logging function to `$verbose` if it's a coderef. If not a +coderef, enables debug logging to `STDERR` if `$verbose` is true; otherwise, +disables logging. + +# POSTING JOBS + +The methods of TheSchwartz clients used by applications posting jobs to the +queue are: + +## `$client->insert( $job )` + +Adds the given `TheSchwartz::Job` to one of the client's job databases. + +## `$client->insert( $funcname, $arg )` + +Adds a new job with function name `$funcname` and arguments `$arg` to the queue. + +## `$client->insert_jobs( @jobs )` + +Adds the given `TheSchwartz::Job` objects to one of the client's job +databases. All the given jobs are recorded in _one_ job database. + +## `$client->set_prioritize( $prioritize )` + +Set the `prioritize` value as described in the constructor. + +## `$client->set_floor( $floor )` + +Set the `floorset_batch_size( $batch_size )` + +Set the `batch_sizeset_strict_remove_ability( $strict_remove_ability )` + +Set the `strict_remove_abilitycan_do( $ability )` + +Adds `$ability` to the list of abilities `$client` is capable of performing. +Subsequent calls to that client's `work` methods will find jobs requiring the +given ability. + +## `$client->work_once()` + +Find and perform one job `$client` can do. + +## `$client->work_until_done()` + +Find and perform jobs `$client` can do until no more such jobs are found in +any of the client's job databases. + +## `$client->work( [$delay] )` + +Find and perform any jobs `$client` can do, forever. When no job is available, +the working process will sleep for `$delay` seconds (or 5, if not specified) +before looking again. + +## `$client->work_on($handle)` + +Given a job handle (a scalar string) _$handle_, runs the job, then returns. + +## `$client->grab_and_work_on($handle)` + +Similar to [$client->work\_on($handle)](https://metacpan.org/pod/%24client-%3Ework_on%28%24handle%29), except that the job will be grabbed +before being run. It guarantees that only one worker will work on it (at least +in the `grab_for` interval). + +Returns false if the worker could not grab the job, and true if the worker worked +on it. + +## `$client->find_job_for_workers( [$abilities] )` + +Returns a `TheSchwartz::Job` for a random job that the client can do. If +specified, the job returned matches one of the abilities in the arrayref +`$abilities`, rather than `$client`'s abilities. + +## `$client->find_job_with_coalescing_value( $ability, $coval )` + +Returns a `TheSchwartz::Job` for a random job for a worker capable of +`$ability` and with a coalescing value of `$coval`. + +## `$client->find_job_with_coalescing_prefix( $ability, $coval )` + +Returns a `TheSchwartz::Job` for a random job for a worker capable of +`$ability` and with a coalescing value beginning with `$coval`. + +Note the `TheSchwartz` implementation of this function uses a `LIKE` query to +find matching jobs, with all the attendant performance implications for your +job databases. + +## `$client->get_server_time( $driver )` + +Given an open driver _$driver_ to a database, gets the current server time from the database. + +# THE SCOREBOARD + +The scoreboards can be used to monitor what the TheSchwartz::Worker sub-classes are +currently working on. Once the scoreboard has been enabled in the workers with +`set_scoreboard` method the `thetop` utility (shipped with TheSchwartz distribution +in the `extras` directory) can be used to list all current jobs being worked on. + +## `$client->set_scoreboard( $dir )` + +Enables the scoreboard. Setting this to `1` or `on` will cause TheSchwartz to create +a scoreboard file in a location it determines is optimal. + +Passing in any other option sets the directory the TheSchwartz scoreboard directory should +be created in. For example, if you set this to `/tmp` then this would create a directory +called `/tmp/theschwartz` and a scoreboard file `/tmp/theschwartz/scoreboard.pid` in it +(where pid is the current process pid.) + +## `$client->scoreboard()` + +Returns the path to the current scoreboard file. + +## `$client->start_scoreboard()` + +Writes the current job information to the scoreboard file (called by the worker +in work\_safely before it actually starts working) + +## `$client->end_scoreboard()` + +Appends the current job duration to the end of the scoreboard file (called by +the worker in work\_safely once work has been completed) + +## `$client->clean_scoreboard()` + +Removes the scoreboard file (but not the scoreboard directory.) Automatically +called by TheSchwartz during object destruction (i.e. when the instance goes +out of scope) + +# PASSING IN AN EXISTING DRIVER + +You can pass in a existing `Data::Object::Driver::DBI` object which also allows you +to reuse exist Database handles like so: + + my $dbh = DBI->connect( $dsn, "root", "", { + RaiseError => 1, + PrintError => 0, + AutoCommit => 1, + } ) or die $DBI::errstr; + my $driver = Data::ObjectDriver::Driver::DBI->new( dbh => $dbh); + return TheSchwartz->new(databases => [{ driver => $driver }]); + +**Note**: it's important that the `RaiseError` and `AutoCommit` flags are +set on the handle for various bits of functionality to work. + +# COPYRIGHT, LICENSE & WARRANTY + +This software is Copyright 2007, Six Apart Ltd, cpan@sixapart.com. All +rights reserved. + +TheSchwartz is free software; you may redistribute it and/or modify it +under the same terms as Perl itself. + +TheSchwartz comes with no warranty of any kind. diff --git a/cpanfile b/cpanfile new file mode 100644 index 0000000..70c6439 --- /dev/null +++ b/cpanfile @@ -0,0 +1,8 @@ +requires 'Data::ObjectDriver', '0.04'; +requires 'Digest::MD5'; +requires 'Storable'; +requires 'Class::Accessor::Fast'; + +on test => sub { + requires 'Test::More'; +}; diff --git a/lib/TheSchwartz.pm b/lib/TheSchwartz.pm index 02a99ed..f89a43f 100644 --- a/lib/TheSchwartz.pm +++ b/lib/TheSchwartz.pm @@ -1,7 +1,7 @@ # $Id$ package TheSchwartz; -use 5.008; +use 5.008001; use strict; use fields qw( databases retry_seconds dead_dsns retry_at funcmap_cache @@ -10,7 +10,7 @@ use fields qw( grab_and_fetch floor batch_size strict_remove_ability ); -our $VERSION = "1.1208"; +our $VERSION = "1.18"; use Carp qw( croak ); use Data::ObjectDriver::Errors; @@ -32,6 +32,7 @@ our $T_LOST_RACE; ## Number of jobs to fetch at a time in find_job_for_workers. our $FIND_JOB_BATCH_SIZE = 50; +our $RANDOMIZE_JOBS = 1; sub new { my TheSchwartz $client = shift; @@ -406,7 +407,7 @@ sub _grab_a_job { } ## Got some jobs! Randomize them to avoid contention between workers. - my @jobs = shuffle(@_); + my @jobs = $RANDOMIZE_JOBS ? shuffle(@_) : @_; JOB: while ( my $job = shift @jobs ) { diff --git a/minil.toml b/minil.toml new file mode 100644 index 0000000..69b45a2 --- /dev/null +++ b/minil.toml @@ -0,0 +1,7 @@ +name = "TheSchwartz" +# badges = ["travis-ci.com"] +authority="cpan:AKIYM" + +module_maker="ModuleBuildTiny" +static_install = "auto" + diff --git a/bin/schwartzmon b/script/schwartzmon similarity index 99% rename from bin/schwartzmon rename to script/schwartzmon index ea6eab9..23ef88c 100755 --- a/bin/schwartzmon +++ b/script/schwartzmon @@ -249,7 +249,7 @@ package DBSet; sub new { my ( $this, $args ) = @_; my $class = ref($this) || $this; - return bless {}, $class; + return bless [], $class; } sub add { diff --git a/t/api.t b/t/api.t index 9d4d177..fb9c7b2 100644 --- a/t/api.t +++ b/t/api.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 58 * 3; diff --git a/t/cleanup.t b/t/cleanup.t index 034dc59..d50bdc0 100644 --- a/t/cleanup.t +++ b/t/cleanup.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 30; @@ -17,53 +17,61 @@ run_tests( 10, sub { my $client = test_client( dbs => ['ts1'] ); - my $dbh = DBI->connect( dsn_for("ts1"), $ENV{TS_DB_USER}, - $ENV{TS_DB_PASS} ); - $client->can_do("Worker::Fail"); - $client->can_do("Worker::Complete"); - # insert a job which will fail, then succeed. { - my $handle = $client->insert("Worker::Fail"); - isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; - - $client->work_until_done; - is( $handle->failures, 1, "job has failed once" ); - - my $min; - my $rows - = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); - is( $rows, 1, "has 1 exitstatus row" ); - - ok( $client->insert("Worker::Complete"), - "inserting to-pass job" ); - $client->work_until_done; - $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); - is( $rows, 2, "has 2 exitstatus rows" ); - ( $rows, $min ) - = $dbh->selectrow_array( - "SELECT COUNT(*), MIN(jobid) FROM error"); - is( $rows, 1, "has 1 error rows" ); - is( $min, 1, "error jobid is the old one" ); - - # wait for exit status to pass - sleep 3; - - # now make another job fail to cleanup some errors - $handle = $client->insert("Worker::Fail"); - $client->work_until_done; - - $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus"); - is( $rows, 1, "1 exit status row now" ); - - ( $rows, $min ) - = $dbh->selectrow_array( - "SELECT COUNT(*), MIN(jobid) FROM error"); - is( $rows, 1, "has 1 error row still" ); - is( $min, 3, "error jobid is only the new one" ); - + my $dbh = DBI->connect( dsn_for("ts1"), $ENV{TS_DB_USER}, + $ENV{TS_DB_PASS} ); + $client->can_do("Worker::Fail"); + $client->can_do("Worker::Complete"); + + # insert a job which will fail, then succeed. + { + my $handle = $client->insert("Worker::Fail"); + isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; + + $client->work_until_done; + is( $handle->failures, 1, "job has failed once" ); + + my $min; + my $rows + = $dbh->selectrow_array( + "SELECT COUNT(*) FROM exitstatus"); + is( $rows, 1, "has 1 exitstatus row" ); + + ok( $client->insert("Worker::Complete"), + "inserting to-pass job" ); + $client->work_until_done; + $rows = $dbh->selectrow_array( + "SELECT COUNT(*) FROM exitstatus"); + is( $rows, 2, "has 2 exitstatus rows" ); + ( $rows, $min ) + = $dbh->selectrow_array( + "SELECT COUNT(*), MIN(jobid) FROM error"); + is( $rows, 1, "has 1 error rows" ); + is( $min, 1, "error jobid is the old one" ); + + # wait for exit status to pass + sleep 3; + + # now make another job fail to cleanup some errors + $handle = $client->insert("Worker::Fail"); + $client->work_until_done; + + $rows = $dbh->selectrow_array( + "SELECT COUNT(*) FROM exitstatus"); + is( $rows, 1, "1 exit status row now" ); + + ( $rows, $min ) + = $dbh->selectrow_array( + "SELECT COUNT(*), MIN(jobid) FROM error"); + is( $rows, 1, "has 1 error row still" ); + is( $min, 3, "error jobid is only the new one" ); + + } } + $client->set_current_job(undef); + teardown_dbs('ts1'); } ); @@ -79,7 +87,7 @@ sub work { } sub keep_exit_status_for { - 1 + 1; } # keep exit status for 20 seconds after on_complete sub max_retries {0} diff --git a/t/client-time-unsync.t b/t/client-time-unsync.t index fa19a85..fd10047 100644 --- a/t/client-time-unsync.t +++ b/t/client-time-unsync.t @@ -17,7 +17,7 @@ BEGIN { } no warnings 'redefine'; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 2; diff --git a/t/coalesce.t b/t/coalesce.t index b42f732..42072d8 100644 --- a/t/coalesce.t +++ b/t/coalesce.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 14 * 3; @@ -13,30 +13,34 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my @keys = qw(foo bar baz); - my $n = 0; - for ( 1 .. 10 ) { - my $key = $keys[ $n++ % 3 ]; - my $job = TheSchwartz::Job->new( - funcname => 'Worker::CoalesceTest', - arg => { key => $key, num => $_ }, - coalesce => $key - ); - my $h = $client->insert($job); - ok( $h, "inserted $h ($_ = $key)" ); - } - - $client->reset_abilities; - $client->can_do("Worker::CoalesceTest"); - - Worker::CoalesceTest->set_client($client); - - for ( 1 .. 3 ) { + { + my @keys = qw(foo bar baz); + my $n = 0; + for ( 1 .. 10 ) { + my $key = $keys[ $n++ % 3 ]; + my $job = TheSchwartz::Job->new( + funcname => 'Worker::CoalesceTest', + arg => { key => $key, num => $_ }, + coalesce => $key + ); + my $h = $client->insert($job); + ok( $h, "inserted $h ($_ = $key)" ); + } + + $client->reset_abilities; + $client->can_do("Worker::CoalesceTest"); + + Worker::CoalesceTest->set_client($client); + + for ( 1 .. 3 ) { + my $rv = eval { $client->work_once; }; + ok( $rv, "did stuff" ); + } my $rv = eval { $client->work_once; }; - ok( $rv, "did stuff" ); + ok( !$rv, "nothing to do now" ); } - my $rv = eval { $client->work_once; }; - ok( !$rv, "nothing to do now" ); + + $client->set_current_job(undef); teardown_dbs('ts1'); } @@ -79,7 +83,7 @@ sub work { } sub keep_exit_status_for { - 20 + 20; } # keep exit status for 20 seconds after on_complete sub grab_for {10} diff --git a/t/dead-dbs.t b/t/dead-dbs.t index eee73a6..673f5b4 100644 --- a/t/dead-dbs.t +++ b/t/dead-dbs.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 6; diff --git a/t/declined.t b/t/declined.t index 8fca90d..798edfc 100644 --- a/t/declined.t +++ b/t/declined.t @@ -1,10 +1,10 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; -use Test::More tests => (5 + 21) * 3; +use Test::More tests => ( 5 + 21 ) * 3; our $decline = 1; @@ -33,6 +33,8 @@ run_tests( "didn't get a job, because job is 'held' not retrying" ); } + $client->set_current_job(undef); + teardown_dbs('ts1'); } ); @@ -76,6 +78,8 @@ run_tests( } } + $client->set_current_job(undef); + teardown_dbs('ts2'); } ); diff --git a/t/empty-db.t b/t/empty-db.t index 17353cd..6fc9bc1 100644 --- a/t/empty-db.t +++ b/t/empty-db.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 9; diff --git a/t/evenly-distribute.t b/t/evenly-distribute.t index 56533f0..9545a75 100644 --- a/t/evenly-distribute.t +++ b/t/evenly-distribute.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 12; @@ -14,46 +14,50 @@ run_tests( sub { my $client = test_client( dbs => [ 'ts1', 'ts2' ] ); - my $n_jobs = 60; - for ( 1 .. $n_jobs ) { - my $handle = $client->insert("Worker::Foo"); - die unless $handle; + { + my $n_jobs = 60; + for ( 1 .. $n_jobs ) { + my $handle = $client->insert("Worker::Foo"); + die unless $handle; + } + + my $db1 = DBI->connect( dsn_for("ts1"), $ENV{TS_DB_USER}, + $ENV{TS_DB_PASS} ); + my $db2 = DBI->connect( dsn_for("ts2"), $ENV{TS_DB_USER}, + $ENV{TS_DB_PASS} ); + die unless $db1 && $db2; + + my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job"); + my $jobs2 = $db2->selectrow_array("SELECT COUNT(*) FROM job"); + is( $jobs1 + $jobs2, $n_jobs, "inserted all $n_jobs" ); + + ok( $jobs1 > $n_jobs / 4, + "at least a quarter of jobs went to db1 ($jobs1 / $n_jobs)" ); + ok( $jobs2 > $n_jobs / 4, + "at least a quarter of jobs went to db1 ($jobs2 / $n_jobs)" ); + + my $do_jobs = int( $n_jobs / 2 ); + $client->can_do("Worker::Foo"); + for ( 1 .. $do_jobs ) { + $client->work_once + or die; + } + + my $jobs1b = $db1->selectrow_array("SELECT COUNT(*) FROM job"); + my $jobs2b = $db2->selectrow_array("SELECT COUNT(*) FROM job"); + + my $remain_jobs = $n_jobs - $do_jobs; + is( $jobs1b + $jobs2b, $remain_jobs, "expected jobs remain" ); + + # deltas: how much work gone done each + my $jobs1d = $jobs1 - $jobs1b; + my $jobs2d = $jobs2 - $jobs2b; + + # difference in work done: + my $workdiff = abs( $jobs1d - $jobs2d ); } - my $db1 = DBI->connect( dsn_for("ts1"), $ENV{TS_DB_USER}, - $ENV{TS_DB_PASS} ); - my $db2 = DBI->connect( dsn_for("ts2"), $ENV{TS_DB_USER}, - $ENV{TS_DB_PASS} ); - die unless $db1 && $db2; - - my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job"); - my $jobs2 = $db2->selectrow_array("SELECT COUNT(*) FROM job"); - is( $jobs1 + $jobs2, $n_jobs, "inserted all $n_jobs" ); - - ok( $jobs1 > $n_jobs / 4, - "at least a quarter of jobs went to db1 ($jobs1 / $n_jobs)" ); - ok( $jobs2 > $n_jobs / 4, - "at least a quarter of jobs went to db1 ($jobs2 / $n_jobs)" ); - - my $do_jobs = int( $n_jobs / 2 ); - $client->can_do("Worker::Foo"); - for ( 1 .. $do_jobs ) { - $client->work_once - or die; - } - - my $jobs1b = $db1->selectrow_array("SELECT COUNT(*) FROM job"); - my $jobs2b = $db2->selectrow_array("SELECT COUNT(*) FROM job"); - - my $remain_jobs = $n_jobs - $do_jobs; - is( $jobs1b + $jobs2b, $remain_jobs, "expected jobs remain" ); - - # deltas: how much work gone done each - my $jobs1d = $jobs1 - $jobs1b; - my $jobs2d = $jobs2 - $jobs2b; - - # difference in work done: - my $workdiff = abs( $jobs1d - $jobs2d ); + $client->set_current_job(undef); teardown_dbs( 'ts1', 'ts2' ); } diff --git a/t/fail-working-multiple.t b/t/fail-working-multiple.t index 3750fc7..bca3a03 100644 --- a/t/fail-working-multiple.t +++ b/t/fail-working-multiple.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 12; @@ -13,30 +13,34 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my $job2h; - for ( 1 .. 2 ) { - my $job = TheSchwartz::Job->new( - funcname => 'Worker::CoalesceTest', - arg => { n => $_ }, - coalesce => "a$_", - ); - my $h = $client->insert($job); - $job2h = $h if $_ == 2; - ok( $h, "inserted $h" ); - } + { + my $job2h; + for ( 1 .. 2 ) { + my $job = TheSchwartz::Job->new( + funcname => 'Worker::CoalesceTest', + arg => { n => $_ }, + coalesce => "a$_", + ); + my $h = $client->insert($job); + $job2h = $h if $_ == 2; + ok( $h, "inserted $h" ); + } + + $client->reset_abilities; + $client->can_do("Worker::CoalesceTest"); - $client->reset_abilities; - $client->can_do("Worker::CoalesceTest"); + my $job = $client->find_job_with_coalescing_prefix( + "Worker::CoalesceTest", "a1" ); + Worker::CoalesceTest->work_safely($job); - my $job = $client->find_job_with_coalescing_prefix( - "Worker::CoalesceTest", "a1" ); - Worker::CoalesceTest->work_safely($job); + # this one should have succeeded: + is( $job->handle->failures, 0, "no failures on first job" ); - # this one should have succeeded: - is( $job->handle->failures, 0, "no failures on first job" ); + # the second one should have failures: + is( $job2h->failures, 1, "1 failure on second job" ); + } - # the second one should have failures: - is( $job2h->failures, 1, "1 failure on second job" ); + $client->set_current_job(undef); teardown_dbs('ts1'); } @@ -58,7 +62,7 @@ sub work { } sub keep_exit_status_for { - 20 + 20; } # keep exit status for 20 seconds after on_complete sub grab_for {10} sub max_retries {1} diff --git a/t/funcid.t b/t/funcid.t index f363791..60510ba 100644 --- a/t/funcid.t +++ b/t/funcid.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 24; diff --git a/t/grab-race.t b/t/grab-race.t index 1071362..efcdff8 100644 --- a/t/grab-race.t +++ b/t/grab-race.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 2; diff --git a/t/grab_and_work_on.t b/t/grab_and_work_on.t index 7f7628e..4272c95 100644 --- a/t/grab_and_work_on.t +++ b/t/grab_and_work_on.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 27; @@ -13,34 +13,38 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my $available - = TheSchwartz::Job->new( funcname => 'Worker::Grabber', ); - my $grabbed_until = time + 2; - my $grabbed = TheSchwartz::Job->new( - funcname => 'Worker::Grabber', - grabbed_until => $grabbed_until, - ); - my $available_handle = $client->insert($available); - my $grabbed_handle = $client->insert($grabbed); - - $client->reset_abilities; - $client->can_do("Worker::Grabber"); - - Worker::Grabber->set_client($client); - - my $rv = $client->grab_and_work_on( $grabbed_handle->as_string ); - ok( !$rv, "we couldn't grab it" ); - is scalar $grabbed->failure_log, 0, "no errors"; - $grabbed->refresh; - is $grabbed->grabbed_until, $grabbed_until, "Still grabbed"; - - $rv = $client->grab_and_work_on( $available_handle->as_string ); - is scalar $available->failure_log, 0, "no errors"; - ok( $rv, "we worked on it" ); - - $rv = $client->grab_and_work_on( $available_handle->as_string ); - is scalar $available->failure_log, 0, "no errors"; - ok( !$rv, "There is nothing to do for it now." ); + { + my $available + = TheSchwartz::Job->new( funcname => 'Worker::Grabber', ); + my $grabbed_until = time + 2; + my $grabbed = TheSchwartz::Job->new( + funcname => 'Worker::Grabber', + grabbed_until => $grabbed_until, + ); + my $available_handle = $client->insert($available); + my $grabbed_handle = $client->insert($grabbed); + + $client->reset_abilities; + $client->can_do("Worker::Grabber"); + + Worker::Grabber->set_client($client); + + my $rv = $client->grab_and_work_on( $grabbed_handle->as_string ); + ok( !$rv, "we couldn't grab it" ); + is scalar $grabbed->failure_log, 0, "no errors"; + $grabbed->refresh; + is $grabbed->grabbed_until, $grabbed_until, "Still grabbed"; + + $rv = $client->grab_and_work_on( $available_handle->as_string ); + is scalar $available->failure_log, 0, "no errors"; + ok( $rv, "we worked on it" ); + + $rv = $client->grab_and_work_on( $available_handle->as_string ); + is scalar $available->failure_log, 0, "no errors"; + ok( !$rv, "There is nothing to do for it now." ); + } + + $client->set_current_job(undef); teardown_dbs('ts1'); } diff --git a/t/high-funcid-starvation.t b/t/high-funcid-starvation.t index 8469e9d..c879049 100644 --- a/t/high-funcid-starvation.t +++ b/t/high-funcid-starvation.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 12; diff --git a/t/insert-and-do.t b/t/insert-and-do.t index f6cc9f9..4b6398b 100644 --- a/t/insert-and-do.t +++ b/t/insert-and-do.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 26 * 3; @@ -115,6 +115,8 @@ run_tests( ); } + $client->set_current_job(undef); + teardown_dbs('ts1'); } ); @@ -168,7 +170,7 @@ sub work { } sub keep_exit_status_for { - 20 + 20; } # keep exit status for 20 seconds after on_complete sub grab_for {10} diff --git a/t/parallel-workers.t b/t/parallel-workers.t index 1834041..5300385 100644 --- a/t/parallel-workers.t +++ b/t/parallel-workers.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 2; diff --git a/t/priority.t b/t/priority.t index b6e1208..f53db08 100644 --- a/t/priority.t +++ b/t/priority.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => ( ( 31 * 3 ) + ( 16 * 3 ) + ( 12 * 3 ) ); @@ -53,6 +53,8 @@ run_tests( my $rv = eval { $client->work_once; }; ok( !$rv, "nothing to do now" ); + $client->set_current_job(undef); + teardown_dbs('ts1'); # test we get in jobid order for equal priority RT #99075 @@ -87,6 +89,8 @@ run_tests( $rv = eval { $client2->work_once; }; ok( !$rv, "nothing to do now 1-5" ); + $client2->set_current_job(undef); + teardown_dbs('ts2'); # test floor RT #50842 @@ -119,6 +123,8 @@ run_tests( $rv = eval { $client2->work_once; }; ok( !$rv, "sub-floor jobs remaining but you can't have them" ); + $client2->set_current_job(undef); + teardown_dbs('ts3'); $testnum = 0; } diff --git a/t/replace-abort.t b/t/replace-abort.t index 86d70f0..6f27dea 100644 --- a/t/replace-abort.t +++ b/t/replace-abort.t @@ -3,144 +3,170 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 13; -run_tests_pgsql(13, sub { - my $client1 = test_client(dbs => ['ts1']); - my $client2 = test_client(dbs => ['ts1']); - - my $driver = $client1->driver_for( ($client1->shuffled_databases)[0] ); - my $dbh = $driver->rw_handle; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey IN ('1','2','3','4','5');"), - 0, - 'namespace empty', - ); - - - $client1->can_do('Test::Job::Completed'); - $client2->can_do('Test::Job::Replace'); - -# job 1 - $client1->insert(TheSchwartz::Job->new( - funcname => 'Test::Job::Completed', - uniqkey => 1, - )); - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '1';"), - 1, - 'Job 1 gepostet', - ); - - -# Job 1 - $client1->work_once; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '1';"), - 0, - 'Job 1 abgearbeitet', - ); - -# Job 2 - $client2->insert(TheSchwartz::Job->new( - funcname => 'Test::Job::Replace', - uniqkey => 2, - arg => 3, - )); - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '2';"), - 1, - 'Job 2 gepostet', - ); - -# Job 2 - $client2->work_once; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '2';"), - 0, - 'Job 2 abgearbeitet', - ); - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';"), - 1, - 'Job 2 ersetzt durch Job 3', - ); - -# Job 4 - $client2->insert(TheSchwartz::Job->new( - funcname => 'Test::Job::Replace', - uniqkey => 4, - arg => 3, - )); - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';"), - 1, - 'Job 4 gepostet', - ); - -# Job 4 - $client2->work_once; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';"), - 1, - 'Job 4 abgebrochen', - ); - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';"), - 1, - 'Job 4 nicht durch Job 3 ersetzt', - ); - -# Job 3 - $client1->work_once; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';"), - 0, - 'Job 3 abgearbeitet', - ); - -# cleanup job.run_after & retry_at, so we dont have to wait - $dbh->do("UPDATE job SET run_after = 0 WHERE uniqkey = '4';"); - $client2->{retry_at} = {}; - -# Job 4 - $client2->work_once; - - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';"), - 0, - 'Job 4 abgearbeitet', - ); - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';"), - 1, - 'Job 4 ersetzt durch Job 3', - ); - -# Job 5 - $client1->work_once; - - is( - query_sql($dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';"), - 0, - 'Job 3 erneut abgearbeitet', - ); -}); - - - +run_tests_pgsql( + 13, + sub { + my $client1 = test_client( dbs => ['ts1'] ); + my $client2 = test_client( dbs => ['ts1'] ); + + { + my $driver + = $client1->driver_for( ( $client1->shuffled_databases )[0] ); + my $dbh = $driver->rw_handle; + + is( query_sql( + $dbh, + "SELECT COUNT(*) FROM job WHERE uniqkey IN ('1','2','3','4','5');" + ), + 0, + 'namespace empty', + ); + + $client1->can_do('Test::Job::Completed'); + $client2->can_do('Test::Job::Replace'); + + # job 1 + $client1->insert( + TheSchwartz::Job->new( + funcname => 'Test::Job::Completed', + uniqkey => 1, + ) + ); + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '1';" + ), + 1, + 'Job 1 gepostet', + ); + + # Job 1 + $client1->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '1';" + ), + 0, + 'Job 1 abgearbeitet', + ); + + # Job 2 + $client2->insert( + TheSchwartz::Job->new( + funcname => 'Test::Job::Replace', + uniqkey => 2, + arg => 3, + ) + ); + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '2';" + ), + 1, + 'Job 2 gepostet', + ); + + # Job 2 + $client2->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '2';" + ), + 0, + 'Job 2 abgearbeitet', + ); + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';" + ), + 1, + 'Job 2 ersetzt durch Job 3', + ); + + # Job 4 + $client2->insert( + TheSchwartz::Job->new( + funcname => 'Test::Job::Replace', + uniqkey => 4, + arg => 3, + ) + ); + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';" + ), + 1, + 'Job 4 gepostet', + ); + + # Job 4 + $client2->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';" + ), + 1, + 'Job 4 abgebrochen', + ); + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';" + ), + 1, + 'Job 4 nicht durch Job 3 ersetzt', + ); + + # Job 3 + $client1->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';" + ), + 0, + 'Job 3 abgearbeitet', + ); + + # cleanup job.run_after & retry_at, so we dont have to wait + $dbh->do("UPDATE job SET run_after = 0 WHERE uniqkey = '4';"); + $client2->{retry_at} = {}; + + # Job 4 + $client2->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '4';" + ), + 0, + 'Job 4 abgearbeitet', + ); + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';" + ), + 1, + 'Job 4 ersetzt durch Job 3', + ); + + # Job 5 + $client1->work_once; + + is( query_sql( + $dbh, "SELECT COUNT(*) FROM job WHERE uniqkey = '3';" + ), + 0, + 'Job 3 erneut abgearbeitet', + ); + + } + + $client1->set_current_job(undef); + $client2->set_current_job(undef); + + teardown_dbs('ts1'); + } +); # TheSchwartz Worker/Jobs package Test::Job::Completed; @@ -148,7 +174,7 @@ package Test::Job::Completed; use base qw(TheSchwartz::Worker); sub work { - my ($client, $job) = @_; + my ( $client, $job ) = @_; $job->completed; } sub max_retries { 10; } @@ -158,11 +184,13 @@ package Test::Job::Replace; use base qw(TheSchwartz::Worker); sub work { - my ($client, $job) = @_; - $job->replace_with(TheSchwartz::Job->new( - funcname => 'Test::Job::Completed', - uniqkey => $job->arg, - )); + my ( $client, $job ) = @_; + $job->replace_with( + TheSchwartz::Job->new( + funcname => 'Test::Job::Completed', + uniqkey => $job->arg, + ) + ); } sub max_retries { 10; } diff --git a/t/replace-with.t b/t/replace-with.t index f9e7978..ba09972 100644 --- a/t/replace-with.t +++ b/t/replace-with.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 30; @@ -13,24 +13,30 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my $handle = $client->insert( "Worker::Foo", { cluster => 'all' } ); - ok($handle); + { + my $handle + = $client->insert( "Worker::Foo", { cluster => 'all' } ); + ok($handle); - my $job = Worker::Foo->grab_job($client); - ok( $job, "no addition jobs to be grabbed" ); + my $job = Worker::Foo->grab_job($client); + ok( $job, "no addition jobs to be grabbed" ); - Worker::Foo->work_safely($job); + Worker::Foo->work_safely($job); - $client->can_do("Worker::Foo"); - $client->work_until_done; # should process 5 jobs. + $client->can_do("Worker::Foo"); + $client->work_until_done; # should process 5 jobs. - # finish a job by replacing it with nothing - $handle - = $client->insert( "Worker::Foo", { cluster => 'gibberish' } ); - ok( $handle->is_pending, "job is still pending" ); - $job = $handle->job; - $job->replace_with(); - ok( !$handle->is_pending, "job no longer pending" ); + # finish a job by replacing it with nothing + $handle + = $client->insert( "Worker::Foo", + { cluster => 'gibberish' } ); + ok( $handle->is_pending, "job is still pending" ); + $job = $handle->job; + $job->replace_with(); + ok( !$handle->is_pending, "job no longer pending" ); + } + + $client->set_current_job(undef); teardown_dbs('ts1'); } diff --git a/t/retry-delay.t b/t/retry-delay.t index 27c5663..96afdae 100644 --- a/t/retry-delay.t +++ b/t/retry-delay.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 24; @@ -42,6 +42,8 @@ run_tests( is( $handle->exit_status, 0, "job succeeded" ); } + $client->set_current_job(undef); + teardown_dbs('ts1'); } ); @@ -63,7 +65,7 @@ sub work { } sub keep_exit_status_for { - 20 + 20; } # keep exit status for 20 seconds after on_complete sub max_retries {2} diff --git a/t/scoreboard.t b/t/scoreboard.t index c19a9d8..d0429d4 100644 --- a/t/scoreboard.t +++ b/t/scoreboard.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use Test::More tests => 30; @@ -33,15 +33,16 @@ run_tests( pass => $ENV{TS_DB_PASS}, prefix => $pfx, } - } @$dbs + } @$dbs ] ); my $sb_file = $client->scoreboard; { - ( undef, my ( $sb_dir, $sb_name ) ) + my ( $sb_volume, $sb_dir, $sb_name ) = File::Spec->splitpath($sb_file); - ok( -e $sb_dir, "Looking for dir $sb_dir" ); + ok( -e File::Spec->catpath( $sb_volume, $sb_dir ), + "Looking for dir $sb_dir" ); } { @@ -62,7 +63,7 @@ run_tests( my %info = map { chomp; /^([^=]+)=(.*)$/ } ; close(FH); - ok( $info{pid} == $$, 'Has our PID' ); + ok( $info{pid} == $$, 'Has our PID' ); ok( $info{funcname} eq 'Worker::Addition', 'Has our funcname' ); ok( $info{started} =~ /\d+/, 'Started time is a number' ); ok( $info{started} <= time, 'Started time is in the past' ); @@ -76,6 +77,8 @@ run_tests( 'Scoreboard file goes away when worker finishes' ); } + $client->set_current_job(undef); + teardown_dbs('ts1'); } ); diff --git a/t/server-time.t b/t/server-time.t index 870ab8e..1ef1d42 100644 --- a/t/server-time.t +++ b/t/server-time.t @@ -3,7 +3,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 6; @@ -13,11 +13,16 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my $driver - = $client->driver_for( ( $client->shuffled_databases )[0] ); - isa_ok $driver, 'Data::ObjectDriver::Driver::DBI'; + { + my $driver + = $client->driver_for( ( $client->shuffled_databases )[0] ); + isa_ok $driver, 'Data::ObjectDriver::Driver::DBI'; - cmp_ok $client->get_server_time($driver), '>', 0, 'got server time'; + cmp_ok $client->get_server_time($driver), '>', 0, + 'got server time'; + } + + $client->set_current_job(undef); teardown_dbs('ts1'); } diff --git a/t/unique.t b/t/unique.t index f0bcea5..e013a32 100644 --- a/t/unique.t +++ b/t/unique.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 18; diff --git a/t/work-before-funcids-exist.t b/t/work-before-funcids-exist.t index 9c06e43..6468df3 100644 --- a/t/work-before-funcids-exist.t +++ b/t/work-before-funcids-exist.t @@ -4,7 +4,7 @@ use strict; use warnings; -require 't/lib/db-common.pl'; +require './t/lib/db-common.pl'; use TheSchwartz; use Test::More tests => 6; @@ -14,15 +14,19 @@ run_tests( sub { my $client = test_client( dbs => ['ts1'] ); - my $handle = $client->insert("Worker::Dummy"); - ok( $handle, "inserted job" ); + { + my $handle = $client->insert("Worker::Dummy"); + ok( $handle, "inserted job" ); - $client->can_do("Worker::Dummy"); - $client->can_do("Worker::Dummy2"); - $client->can_do("Worker::Dummy3"); - $client->work_until_done; + $client->can_do("Worker::Dummy"); + $client->can_do("Worker::Dummy2"); + $client->can_do("Worker::Dummy3"); + $client->work_until_done; - ok( !$handle->is_pending, "job is done" ); + ok( !$handle->is_pending, "job is done" ); + } + + $client->set_current_job(undef); teardown_dbs('ts1'); }