-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- System management functions
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------



--
-- Gets the next tick's identifier
--

CREATE OR REPLACE FUNCTION sys.get_tick( )
	RETURNS BIGINT
	STRICT STABLE
	SECURITY INVOKER
AS $$
	SELECT next_tick FROM sys.status;
$$ LANGUAGE SQL;



--
-- Registers a new ticker task
--

CREATE OR REPLACE FUNCTION sys.register_ticker_task( t_name TEXT )
	RETURNS INT
	STRICT VOLATILE
	SECURITY DEFINER
AS $$
	INSERT INTO sys.ticker ( task_name , status )
		VALUES ( $1 , 'RUNNING' )
		RETURNING id;
$$ LANGUAGE SQL;

GRANT EXECUTE ON FUNCTION sys.register_ticker_task( TEXT ) TO :dbuser;



--
-- Indicates that a ticker task was started automatically
--
-- Parameters:
--	task_id		Task identifier
--

CREATE OR REPLACE FUNCTION sys.set_task_started( task_id INT )
		RETURNS VOID
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	t_name	TEXT;
BEGIN
	SELECT INTO t_name task_name FROM sys.ticker
		WHERE id = task_id AND status = 'AUTO' FOR UPDATE;
	IF FOUND
	THEN
		UPDATE sys.ticker SET status = 'RUNNING' , auto_start = NULL
			WHERE id = task_id;
		PERFORM sys.write_log( 'Ticker' , 'INFO'::log_level , 'Scheduled task ''' || t_name
			|| ''' has been enabled' );
	END IF;
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.set_task_started( INT ) TO :dbuser;



--
-- Starts or stops a task
--
-- Parameters:
--	admin_id	Administrator identifier
--	task_id		Task identifier
--	running		Whether the task should be started or stopped
--

CREATE OR REPLACE FUNCTION sys.set_task_running( admin_id INT , task_id INT , running BOOLEAN )
		RETURNS VOID
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	n_stat	ticker_task_status;
	t_name	TEXT;
BEGIN
	n_stat := ( CASE WHEN running THEN 'RUNNING' ELSE 'STOPPED' END );
	SELECT INTO t_name task_name FROM sys.ticker
		WHERE id = task_id AND status <> n_stat FOR UPDATE;
	IF FOUND
	THEN
		UPDATE sys.ticker SET status = n_stat , auto_start = NULL
			WHERE id = task_id;
		PERFORM admin.write_log( admin_id , 'INFO'::log_level , 'Ticker task ''' || t_name
			|| ''' changed to status ' || n_stat );
	END IF;
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.set_task_running( INT , INT , BOOLEAN ) TO :dbuser;



--
-- Schedules a task to start automatically at a later date
--
-- Parameters:
--	admin_id	Administrator identifier
--	task_id		Task identifier
--	delay		Delay, in seconds, before the task starts
--
-- Returns:
--	start_at	Time and date at which the task will start
--

CREATE OR REPLACE FUNCTION sys.schedule_task( admin_id INT , task_id INT , delay BIGINT , OUT start_at TIMESTAMP WITHOUT TIME ZONE )
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	t_name	TEXT;
BEGIN
	start_at := now() + ( delay || 's' )::INTERVAL;

	SELECT INTO t_name task_name FROM sys.ticker
		WHERE id = task_id FOR UPDATE;
	IF NOT FOUND
	THEN
		RETURN;
	END IF;
	
	UPDATE sys.ticker SET status = 'AUTO' , auto_start = start_at WHERE id = task_id;
	PERFORM admin.write_log( admin_id , 'INFO'::log_level , 'Ticker task ''' || t_name
			|| ''' scheduled to start ' || start_at );
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.schedule_task( INT , INT , BIGINT ) TO :dbuser;















