FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libfossscheduler.c File Reference

Scheduler API for agents. More...

#include "libfossscheduler.h"
#include "libfossdb.h"
#include "fossconfig.h"
#include <stdio.h>
#include <getopt.h>
#include <libgen.h>
#include <glib.h>
Include dependency graph for libfossscheduler.c:

Go to the source code of this file.

Functions

void fo_heartbeat ()
 Internal function to send a heartbeat to the scheduler along with the number of items processed. More...
 
void fo_check_agentdb (PGconn *db_conn)
 Checks that the agent is already in the agent table. More...
 
void fo_scheduler_heart (int i)
 This function must be called by agents to let the scheduler know they are alive and how many items they have processed. More...
 
void fo_scheduler_connect_conf (int *argc, char **argv, PGconn **db_conn, char **db_conf)
 Establish a connection between an agent and the scheduler. More...
 
void fo_scheduler_connect (int *argc, char **argv, PGconn **db_conn)
 Establish a connection between an agent and the scheduler. More...
 
void fo_scheduler_connect_dbMan (int *argc, char **argv, fo_dbManager **dbManager)
 Make a connection from an agent to the scheduler and create a DB manager as well. More...
 
void fo_scheduler_disconnect (int retcode)
 Disconnect the scheduler connection. More...
 
char * fo_scheduler_next ()
 Get the next data to process from the scheduler. More...
 
char * fo_scheduler_current ()
 Get the last read string from the scheduler. More...
 
void fo_scheduler_set_special (int option, int value)
 Sets something special about the agent within the scheduler. More...
 
int fo_scheduler_get_special (int option)
 Gets if a particular special attribute is set in the scheduler. More...
 
int fo_scheduler_jobId ()
 Gets the id of the job that the agent is running. More...
 
int fo_scheduler_userID ()
 Gets the id of the user that created the job that the agent is running. More...
 
int fo_scheduler_groupID ()
 Gets the id of the group that created the job that the agent is running. More...
 
char * fo_sysconfig (const char *sectionname, const char *variablename)
 gets a system configuration variable from the configuration data. More...
 

Variables

volatile gint items_processed
 The number of items processed by the agent.
 
volatile int alive
 If the agent has updated with a hearbeat.
 
char buffer [2048]
 The last thing received from the scheduler.
 
int valid
 If the information stored in buffer is valid.
 
int sscheduler
 Whether the agent was started by the scheduler.
 
int userID
 The id of the user that created the job.
 
int groupID
 The id of the group of the user that created the job.
 
int jobId
 The id of the job.
 
char * module_name = NULL
 The name of the agent.
 
static const char * sql_check
 
static const char * sql_insert
 
fo_confsysconfig = NULL
 
char * sysconfigdir = NULL
 
GRegex * fo_conf_parse
 Regex for parsing.
 
GRegex * fo_conf_replace
 Regex for replace.
 
int agent_verbose
 Common verbose flags for the agents, this is used so that the scheduler can change the verbose level for a particular agent. All agents should use this flag for verbose instead of one declared within the agent. This can be set by the scheduler to enable different levels of verbose. More...
 

Detailed Description

Scheduler API for agents.

Definition in file libfossscheduler.c.

Function Documentation

◆ fo_check_agentdb()

void fo_check_agentdb ( PGconn *  db_conn)

Checks that the agent is already in the agent table.

This uses the VERSION and COMMIT_HASH in the system configuration information to determine if a new agent record needs to be created for this agent in the database.

Definition at line 93 of file libfossscheduler.c.

◆ fo_heartbeat()

void fo_heartbeat ( )

Internal function to send a heartbeat to the scheduler along with the number of items processed.

Note
Agents should NOT call this function directly.
This is the alarm SIGALRM function.
Returns
void
Todo:
These functions are not safe for a signal handler

Definition at line 75 of file libfossscheduler.c.

◆ fo_scheduler_connect()

void fo_scheduler_connect ( int *  argc,
char **  argv,
PGconn **  db_conn 
)

Establish a connection between an agent and the scheduler.

Steps taken by this function:

  • initialize memory associated with agent connection
  • send "SPAWNED" to the scheduler
  • receive the number of items between notifications
  • check the nfs mounts for the agent
  • set up the heartbeat()

Making a call to this function should be the first thing that an agent does after parsing its command line arguments.

If the database connection passed is NULL, then this will not return a database connection, and will not check the agent's database record.

Parameters
argcpointer to the number of arguments passed to the agent
argvthe command line arguments for the agent
db_connpointer to the location for the database connection
Returns
void

Definition at line 319 of file libfossscheduler.c.

◆ fo_scheduler_connect_conf()

void fo_scheduler_connect_conf ( int *  argc,
char **  argv,
PGconn **  db_conn,
char **  db_conf 
)

Establish a connection between an agent and the scheduler.

Parameters
[in]argcCommand line agrument count
[in]argvCommand line agrument vector
[out]db_connDB Connection
[out]db_confDB conf file
See also
fo_scheduler_connect()

Definition at line 169 of file libfossscheduler.c.

◆ fo_scheduler_connect_dbMan()

void fo_scheduler_connect_dbMan ( int *  argc,
char **  argv,
fo_dbManager **  dbManager 
)

Make a connection from an agent to the scheduler and create a DB manager as well.

Parameters
[out]dbManagerNew DB manager

Definition at line 329 of file libfossscheduler.c.

◆ fo_scheduler_current()

char* fo_scheduler_current ( )

Get the last read string from the scheduler.

Returns
Returns the string buffer if it is valid. If it is not valid, return NULL. The buffer is not valid if the last received data from the scheduler was a command, rather than data to operate on.

Definition at line 458 of file libfossscheduler.c.

◆ fo_scheduler_disconnect()

void fo_scheduler_disconnect ( int  retcode)

Disconnect the scheduler connection.

Making a call to this function should be the last thing that an agent does before exiting. Any error reporting to stdout or stderr will not work after this function has finished execution.

Parameters
retcodeReturn code to the scheduler

Definition at line 346 of file libfossscheduler.c.

◆ fo_scheduler_get_special()

int fo_scheduler_get_special ( int  option)

Gets if a particular special attribute is set in the scheduler.

Possible Options:

  • SPECIAL_NOKILL : the agent will not be killed if it stops updating status
  • SPECIAL_EXCLUSIVE: the agent cannot run simultaneously with any other agent
  • SPECIAL_NOEMAIL : the scheduler will not send notification emails for this agent
  • SPECIAL_LOCAL : the agent is required to run on the same machine as the scheduler
Parameters
optionthe relevant option to the get the value of
Returns
if the value of the special option was true

Definition at line 490 of file libfossscheduler.c.

◆ fo_scheduler_groupID()

int fo_scheduler_groupID ( )

Gets the id of the group that created the job that the agent is running.

Returns
the group id

Definition at line 527 of file libfossscheduler.c.

◆ fo_scheduler_heart()

void fo_scheduler_heart ( int  i)

This function must be called by agents to let the scheduler know they are alive and how many items they have processed.

Parameters
iThis is the number of itmes processed since the last call to fo_scheduler_heart()
Returns
void

Definition at line 152 of file libfossscheduler.c.

◆ fo_scheduler_jobId()

int fo_scheduler_jobId ( )

Gets the id of the job that the agent is running.

Returns
the job id

Definition at line 507 of file libfossscheduler.c.

◆ fo_scheduler_next()

char* fo_scheduler_next ( )

Get the next data to process from the scheduler.

It is the job of the agent to decide how this string is interpreted.

Steps taken by this function:

  • get the next line from the scheduler
    • if the scheduler has paused this agent this will block till unpaused
  • check for "CLOSE" from scheduler, return NULL if received
  • check for "VERBOSE" from scheduler
    • if this is received turn the verbose flag to whatever is specified
    • a new line must be received, perform same task (i.e. recursive call)
  • check for "END" from scheduler, if received print OK and recurse
    • this is used to simplify communications within the scheduler
  • return whatever has been received
Returns
char* for the next thing to analyze, NULL if there is nothing left in this job, in which case the agent should close

Definition at line 400 of file libfossscheduler.c.

◆ fo_scheduler_set_special()

void fo_scheduler_set_special ( int  option,
int  value 
)

Sets something special about the agent within the scheduler.

Possible Options: SPECIAL_NOKILL: instruct the scheduler to not kill the agent

Parameters
optionthe option to set
valuewhether to set the option to true or false

Definition at line 472 of file libfossscheduler.c.

◆ fo_scheduler_userID()

int fo_scheduler_userID ( )

Gets the id of the user that created the job that the agent is running.

Returns
the user id

Definition at line 517 of file libfossscheduler.c.

◆ fo_sysconfig()

char* fo_sysconfig ( const char *  sectionname,
const char *  variablename 
)

gets a system configuration variable from the configuration data.

This function should be called after fo_scheduler_connect has been called. This is because the configuration data it not loaded until after that.

Parameters
sectionnamethe group of the variable
variablenamethe name of the variable
Returns
the value of the variable

Definition at line 542 of file libfossscheduler.c.

Variable Documentation

◆ agent_verbose

int agent_verbose

Common verbose flags for the agents, this is used so that the scheduler can change the verbose level for a particular agent. All agents should use this flag for verbose instead of one declared within the agent. This can be set by the scheduler to enable different levels of verbose.

Global verbose flags that agents should use instead of specific verbose flags. This is used by the scheduler to turn verbose on a particular agent on during run time. When the verbose flag is turned on by the scheduler the on_verbose function will be called. If nothing needs to be done when verbose is turned on, simply pass NULL to scheduler_connect

Definition at line 64 of file libfossscheduler.c.

◆ sql_check

const char* sql_check
static
Initial value:
= "\
SELECT * FROM agent \
WHERE agent_name = '%s' AND agent_rev='%s.%s'"

Check for an agent in DB

Definition at line 39 of file libfossscheduler.c.

◆ sql_insert

const char* sql_insert
static
Initial value:
= "\
INSERT INTO agent (agent_name, agent_rev, agent_desc) \
VALUES ('%s', '%s.%s', '%s')"

Insert new agent in DB

Definition at line 44 of file libfossscheduler.c.

◆ sysconfig

fo_conf* sysconfig = NULL

System configuration settings

Definition at line 49 of file libfossscheduler.c.

◆ sysconfigdir

char* sysconfigdir = NULL

System configuration directory

Definition at line 51 of file libfossscheduler.c.