Writing Perl plugins for Disbatch 4
Copyright (c) 2016 by Ashley Willis.
For a simple example, see lib/Disbatch/Plugin/Demo.pm.
Requirements
-
Two subroutines:
newandrun-
new({workerthread => $workerthread, task => $doc})$workerthreadis aDisbatchobject using thepluginMongoDB user and role. This gives access to the Disbatch subroutines, such aslogger,mongo, and the various collection helper subs (nodes,queues,tasks), with whatever MongoDB access permissionspluginhas.$docis the task's full document from MongoDB, where$doc->{_id}and$doc->{queue}areMongoDB::OIDobjects. -
run()This must return a HASH, and the HASH should contain the keys
status,stdout, andstderr.The value of
statusmust be a positive integer, where1indicates success, and generally2to indicate failure. If not, it will be set as2.The values of
stdoutandstderrshould be simple scalars (strings orundef), and will be forced to be strings.
-
Task Params
Anything for a particular task can be here. For email migrations, we typically
have the following key names: client, migration, user1, user2, and
commands.
clientdefines the client name that the user is part of, as some plugins work for multiple clientsmigrationis a string to identify a group of migration tasks. You can also use queues alone for this purpose.user1anduser2identify the source and destination email accounts. Rarely will they differ, outside of testing.commandsis a string where each character signifies a step in the migration process, or*to signify all the standard steps needed. For each step,commandsis checked against a regex. An array of commands with descriptive names could also be used.
The params object may also contain additional name/value pairs for special
options.
Recommendations
-
finish()As shown in
lib/Disbatch/Plugin/Demo.pm, there is afinishsubroutine, which handles all the finalization of the task and returning the task'sstatus,stdout, andstderr. The finalization is typically saving a report for this task in thereportscollection. In the event of an error, a command's step will set the status to2to indicate failure, callfinish(), and return the result. If the end ofrun()is reached, thenfinish()will be called (at the beginning ofrun(), the status is set to1to indicate success). -
Reports
A report typically contains the important identifying params of the task (
migration,user1,user2, andcommands), as well as the task and queue ids, the start and end times of the task, the plugin version used, the status of the task, a count of any errors encounted, and a simple string identifying an error which caused a failure of a task.This is written to the
reportscollection, so thepluginMongoDB user and role needs to have theinsertpermission. -
Accessing and updating other MongoDB collections
You may need to find, update, or insert documents in other collections. The
pluginrole by default can read all collections in the database it has access to. Addinsert,update, and possiblycreateIndexpermissions to the role as appropriate.Within the plugin, you can access these collections with the following:
$self->{workerthread}->mongo->get_collection($name)