#!/usr/bin/perl use strict; #################### main pod documentation begin ################### =head1 NAME Zymonic - Test script to simulator a user using a process, to test locking better in the system. =head1 SYNOPSIS =head1 DESCRIPTION This script will take a user, a state zname a transition zname, and a delay. Will find a process in that state, run transition on it with a delay between. =head1 SUPPORT As in the license, Zymonic is provided without warranty or support unless purchased separately, however... If you email zymonic-support@zednax.com your issue will be noted and may receive a response. For security issues, please contact zymonic-security@zednax.com and someone will respond within 8 working hours. =head1 AUTHOR Alex Masidlover et al. CPAN ID: MODAUTHOR Zednax Limited alex.masidlover@zednax.com http://www.zednax.com =head1 COPYRIGHT This program is free software licensed under the... Zymonic Public License 1.0 The full text of the license can be found in the LICENSE file included with this module. Other licenses may be acceptable if including parts of Zymonic in larger projects, please contact Zednax for details. =head1 SEE ALSO perl(1). =cut #################### main pod documentation end ################### # modules use Data::Dumper; $Data::Dumper::Indent = 0; use Zymonic::Script; # start a zymonic script my $s = Zymonic::Script->new( args => { user => { type => 'string', required => 'true', description => 'User to simulate', }, password => { type => 'string', description => 'Password of user, if left out will be captured on running', }, process => { type => 'string', required => 'true', description => 'ZName of Process', }, state => { type => 'string', required => 'true', description => 'ZName of State Process should be in', }, transition => { type => 'string', description => 'ZName of Transition to run', }, max_delay => { type => 'int', default => 10, description => 'Maximum number of seconds to wait between runs', }, run_time => { type => 'int', default => 60, description => 'Total amount of seconds to run for', }, log_file => { type => 'string', description => 'File to log the activity to', }, }, init_zymonic_clients => 'true', zymonic_client_log_file => 'ARG:log_file', zymonic_client_debug => 'false', zymonic_client_timeout => 1000, ); main(); exit(0); #################### subroutine header begin #################### =head2 main Usage : main(); Purpose : main script functionality Returns : nothing Argument : nothing Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub main { # setup zymonic client to do the hard work $s->get_zymonic_client( $s->get_arg('user'), $s->get_arg('password'), 'capture_password' ); # start the loop my $run_time = $s->get_arg('run_time'); my $max_delay = $s->get_arg('max_delay'); my $start_time = time(); while (1) { last if ( time() - $start_time ) > $run_time; # find a process my @process_ids = get_process_ids(); my $process_id = $process_ids[ rand @process_ids ]; if ($process_id) { log_output("Got process $process_id"); # run it my $result = run_transition_on_process($process_id); log_output( Dumper($result) ); } else { my $time = localtime; log_output('No matching process found'); } # wait for some time sleep( rand($max_delay) ); } print "Done\n"; } #################### subroutine header begin #################### =head2 log_output() Usage : log_output(); Purpose : prints a message to STDOUT with timestamp Returns : nothing Argument : message to print Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub log_output { my $message = shift; my $time = localtime; print "[$time] $message\n"; } #################### subroutine header begin #################### =head2 get_process_ids() Usage : get_process_ids(); Purpose : gets list of processes in desired state Returns : list of process ids Argument : nothing Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub get_process_ids { my $results = $s->{DB}->run_query( { string => 'SELECT processid FROM zz_process WHERE process = ? ANd current_state = ? ' . 'AND (deleted IS NULL OR deleted != ?)', params => [ $s->get_arg('process'), $s->get_arg('state'), 'Y' ] } ); return map { $_->{processid} } @{$results}; } #################### subroutine header begin #################### =head2 run_transition_on_process() Usage : run_transition_on_process($process_id); Purpose : runs desired transition on process Returns : process id Argument : result Throws : nothing Comment : See Also : =cut #################### subroutine header end ################### sub run_transition_on_process { my $process_id = shift; my $client = $s->get_zymonic_client( $s->get_arg('user') ); my $process = $s->get_arg('process'); my $transition = $s->get_arg('transition'); my $response = $client->$process( 'process', { ( $transition ? ( Transition => { content => $transition } ) : () ), ZymonicHeader => { process_id => { content => $process_id } } } ); my $errors = $client->check_for_errors( $response, [], $process, $transition ); if ($errors) { return { errors => $errors }; } else { return { success => ( $transition ? $response->{$process}->{$transition} : 'true' ) }; } }