The tasks themselves now control whether they are executed again. To compare it to the old system, oneshot tasks now return `false` and repeating tasks return `true`. Letting the housekeeper remove the tasks makes the code simpler and removes the possibility of the task being removed while it is being executed. It does introduce a deadlock possibility if a housekeeper function is called inside a housekeeper task.
		
			
				
	
	
		
			100 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#pragma once
 | 
						|
/*
 | 
						|
 * Copyright (c) 2016 MariaDB Corporation Ab
 | 
						|
 *
 | 
						|
 * Use of this software is governed by the Business Source License included
 | 
						|
 * in the LICENSE.TXT file and at www.mariadb.com/bsl11.
 | 
						|
 *
 | 
						|
 * Change Date: 2020-01-01
 | 
						|
 *
 | 
						|
 * On the date above, in accordance with the Business Source License, use
 | 
						|
 * of this software will be governed by version 2 or later of the General
 | 
						|
 * Public License.
 | 
						|
 */
 | 
						|
 | 
						|
/**
 | 
						|
 * @file housekeeper.h A mechanism to have task run periodically
 | 
						|
 */
 | 
						|
 | 
						|
#include <maxscale/cdefs.h>
 | 
						|
#include <maxscale/dcb.h>
 | 
						|
 | 
						|
MXS_BEGIN_DECLS
 | 
						|
 | 
						|
/**
 | 
						|
 * The task callback function
 | 
						|
 *
 | 
						|
 * The parameter is the user data given to the `hktask_add` function.
 | 
						|
 *
 | 
						|
 * If the function returns true, the same task is added back to the queue and
 | 
						|
 * executed again at a later point in time. If the function returns false,
 | 
						|
 * the task is removed.
 | 
						|
 */
 | 
						|
typedef bool (*TASKFN)(void *data);
 | 
						|
 | 
						|
/**
 | 
						|
 * Initialises the housekeeper mechanism.
 | 
						|
 *
 | 
						|
 * A call to any of the other housekeeper functions can be made only if
 | 
						|
 * this function returns successfully.
 | 
						|
 *
 | 
						|
 * @return True if the housekeeper mechanism was initialized, false otherwise.
 | 
						|
 */
 | 
						|
bool hkinit();
 | 
						|
 | 
						|
/**
 | 
						|
 * Shuts down the housekeeper mechanism.
 | 
						|
 *
 | 
						|
 * Should be called @b only if @c hkinit() returned successfully.
 | 
						|
 *
 | 
						|
 * @see hkinit hkfinish
 | 
						|
 */
 | 
						|
void hkshutdown();
 | 
						|
 | 
						|
/**
 | 
						|
 * Waits for the housekeeper thread to finish. Should be called only after
 | 
						|
 * hkshutdown() has been called.
 | 
						|
 */
 | 
						|
void hkfinish();
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Add a new task
 | 
						|
 *
 | 
						|
 * The task will be first run @c frequency seconds after this call is
 | 
						|
 * made and will the be executed repeatedly every frequency seconds
 | 
						|
 * until the task is removed.
 | 
						|
 *
 | 
						|
 * Task names must be unique.
 | 
						|
 *
 | 
						|
 * @param name      Task name
 | 
						|
 * @param task      Function to execute
 | 
						|
 * @param data      Data passed to function as the parameter
 | 
						|
 * @param frequency Frequency of execution
 | 
						|
 */
 | 
						|
void hktask_add(const char *name, TASKFN func, void *data, int frequency);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Remove all tasks with this name
 | 
						|
 *
 | 
						|
 * @param name Task name
 | 
						|
 */
 | 
						|
void hktask_remove(const char *name);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Show the tasks that are scheduled for the house keeper
 | 
						|
 *
 | 
						|
 * @param pDcb The DCB to send to output
 | 
						|
 */
 | 
						|
void hkshow_tasks(DCB *pdcb);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Show tasks as JSON resource
 | 
						|
 *
 | 
						|
 * @param host Hostname of this server
 | 
						|
 *
 | 
						|
 * @return Collection of JSON formatted task resources
 | 
						|
 */
 | 
						|
json_t* hk_tasks_json(const char* host);
 | 
						|
 | 
						|
MXS_END_DECLS
 |