/***************************************************************************
                Monitoring
                -----------------------------------------
	begin      : 2015/05/14
	copyright  : (C) 2011 SolutionBox Inc.
	author     : Service 1 Team
	email      : svc1@solbox.com
	version    : 3.1.0

    CopyRight(C) 2011 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#include "rc_cmove.h"
#include "monitoring.h"
#include "dbconnpool.h"
#include "workpool.h"
#include "jobmanager.h"
#include "Logger.h"


#define  MONITORING_SLEEP	(1000*1000) // microsecond

monitoring *monitoring::m_inst = NULL;

monitoring::monitoring()
	: m_exit(false), m_pjobs(NULL)
{
	pthread_mutex_init(&m_mutex, NULL);
}

monitoring::~monitoring()
{
	pthread_mutex_destroy(&m_mutex);
}

void* monitoring::monitoringfn( void* pdata )
{
	monitoring* pObject = reinterpret_cast<monitoring *>(pdata);
	ostringstream msg;

	//pthread_detach(pthread_self());
	
	msg << "monitoring::monitoringfn Start";
	_LOGACOUT(LDEV2, msg);
	while( pObject->m_exit == false )
	{
		msg << "monitoring::monitoringfn";
		_LOGACOUT(LDBG, msg);
		// pool status 
		if (masterdbpool::getInstance())
			masterdbpool::getInstance()->printstatus("monitoring");

		//slavedbpool::getInstance()->printstatus("monitoring(Slave)");
		if (workpool::getInstance())
			workpool::getInstance()->printstatus("monitoring");

		if( CLogger::GetInstance()->GetLogLevel() >= LDEV1 )
			workpool::getInstance()->printstatusex("monitoring-DEBUG");
			
		// job status
		if (pObject->m_exit == false)
		{
			pObject->printjobstatus("monitoring");
			solusleep(MONITORING_SLEEP);
		}	
	}

	msg << "monitoring::monitoringfn End";
	_LOGACOUT(LDEV2, msg);
	
	return 0;
}

bool monitoring::init()
{
	
	// Monitoring thread
	int nRet = pthread_create(&m_thread, 0, monitoring::monitoringfn, this);
	if( nRet )
	{
		cerr << "Thread create failed.: errno: " << errno << endl;
		return false;
	}
	sleep(0);

	return true;
}

void monitoring::destory()
{
	m_exit = true;
	pthread_join(m_thread, NULL);
}

monitoring* monitoring::getInstance()
{
	if(monitoring::m_inst == NULL)
	{
		monitoring::m_inst = new monitoring();
	}

	return monitoring::m_inst;
}

void monitoring::release()
{		
	if( monitoring::m_inst != NULL)
	{
		monitoring::m_inst->destory();		
		delete monitoring::m_inst;
		monitoring::m_inst = NULL;
	}
}

void monitoring::printjobstatus(string prefixed)
{
	if (m_exit) return;

	ostringstream msg;
	if( m_pjobs == NULL )
	{
		msg << "monitoring - Work has not been set yet.";
		_LOGACOUT(LDBG, msg);

		return;
	}

	monlock();
	vector<jobmanager *>::iterator it;
	if (m_pjobs->size())
	{
		for (it = m_pjobs->begin(); it < m_pjobs->end(); it++)
		{
			if (m_exit == false && it != m_pjobs->end())
			{
				if ((*it)->isexit() == false)
					(*it)->printstatus(prefixed);
			}
		}

	}
	monunlock();
}
