Module API

Several standard modules are included with NiDB, but a framework and API exist to allow developers to create their own modules.

Below is a sample Perl module with the basic layout for creating a module. The module will also need to be registered with NiDB and setup in cron if the module should run automatically.


# ------------------------------------------------------------------------------
# Copyright (C) 2004-2013
# Gregory A Book (,
# Olin Neuropsychiatry Research Center, Hartford Hospital
# ------------------------------------------------------------------------------
# GPLv3 License:
# 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 3 of the License, 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
# 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, see (
# ------------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# This is a sample program which will do nothing
# -----------------------------------------------------------------------------

# perl modules to include
use strict;
use warnings;
use Mysql;

# include the API functions and configuration file
require '';
my %config = do '';

# debugging
our $debug = 0;
our $dev = 0;

# database variables
our $mysqldevhost = $config{mysqldevhost};
our $mysqldevdatabase = $config{mysqldevdatabase};
our $mysqldevuser = $config{mysqldevuser};
our $mysqldevpassword = $config{mysqldevpassword};
our $mysqlhost = $config{mysqlhost};
our $mysqldatabase = $config{mysqldatabase};
our $mysqluser = $config{mysqluser};
our $mysqlpassword = $config{mysqlpassword};
our $db;

# email parameters (SMTP through TLS)
our $emailusername = $config{emailusername};
our $emailpassword = $config{emailpassword};
our $emailserver = $config{emailserver};
our $emailport = $config{emailport};
our $adminemail = $config{adminemail};

# script specific information
our $scriptname = "nothing";
our $lockfileprefix = "nothing";	# lock files will be numbered lock.1, lock.2 ...
our $lockfile = "";			# lockfile name created for this instance of the program
our $log;				# logfile handle created for this instance of the program
our $numinstances = 1;			# number of times this program can be run concurrently

# setup the directories to be used
our $logdir		= $config{logdir};	# where the log files are kept
our $lockdir		= $config{lockdir};	# where the lock files are
our $scriptdir		= $config{scriptdir};	# where this program and others are run from
our $incomingdir	= $config{incomingdir};	# data is pulled from the dicom server and placed in this directory
our $packageimportdir	= $config{packageimportdir};	# import packages are placed here prior to import
our $archivedir		= $config{archivedir};	# this is the final place for zipped/recon/formatted subject directories
our $backupdir		= $config{backupdir};	# backup directory. all handled data is copied here
our $ftpdir		= $config{ftpdir};	# local FTP directory
our $mountdir		= $config{mountdir};	# directory in which all NFS directories are mounted
our $problemdir		= $config{problemdir};	# place to dump non-dicom, unreadble or corrupt dicom files

# ------------- end variable declaration --------------------------------------
# -----------------------------------------------------------------------------

# check if this program can run or not
if (CheckNumLockFiles($lockfileprefix, $lockdir) >= $numinstances) {
	print "Can't run, too many of me already running\n";
else {
	my $logfilename;
	($lockfile, $logfilename) = CreateLockFile($lockfileprefix, $lockdir, $numinstances);
	#my $logfilename = "$lockfile";
	$logfilename = "$logdir/$scriptname" . CreateLogDate() . ".log";
	open $log, '> ', $logfilename;
	my $x = DoNothing();
	close $log;
	if (!$x) { unlink $logfilename; } # delete the logfile if nothing was actually done
	print "Done. Deleting $lockfile\n";
	unlink $lockfile;


# ----------------------------------------------------------
# --------- DoNothing --------------------------------------
# ----------------------------------------------------------
sub DoNothing {
	my $time = CreateCurrentDate();
	WriteLog("$scriptname Running... Current Time is $time");

	# connect to the database

	# do any kind of processing here...

	return 1; # return 1 if you want the log file to be kept, otherwise its deleted

Leave a Reply

Your email address will not be published. Required fields are marked *


1 × = five

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>