- created stop and count function for sics crons
This commit is contained in:
2
event.h
2
event.h
@ -69,7 +69,7 @@ typedef struct {
|
|||||||
#define TOKENGRAB 302
|
#define TOKENGRAB 302
|
||||||
#define TOKENRELEASE 303
|
#define TOKENRELEASE 303
|
||||||
#define COMLOG 304
|
#define COMLOG 304
|
||||||
#define CRONLIST 305
|
#define CRONFUNC 305
|
||||||
|
|
||||||
#line 131 "event.w"
|
#line 131 "event.w"
|
||||||
|
|
||||||
|
98
sicscron.c
98
sicscron.c
@ -9,6 +9,8 @@
|
|||||||
Mark Koennecke, November 1999
|
Mark Koennecke, November 1999
|
||||||
|
|
||||||
modified to give more error output: Mark Koennecke, December 2010
|
modified to give more error output: Mark Koennecke, December 2010
|
||||||
|
|
||||||
|
added more functionality: list, stop, count
|
||||||
------------------------------------------------------------------------*/
|
------------------------------------------------------------------------*/
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -29,9 +31,13 @@ typedef struct {
|
|||||||
Statistics *stat;
|
Statistics *stat;
|
||||||
} Cron, *pCron;
|
} Cron, *pCron;
|
||||||
|
|
||||||
|
typedef enum {cron_list, cron_stop, cron_count} CronFunc;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SConnection *pCon;
|
SConnection *pCon;
|
||||||
int dolater;
|
CronFunc func;
|
||||||
|
char *cmd;
|
||||||
|
int cnt;
|
||||||
} CronListData;
|
} CronListData;
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
@ -58,13 +64,12 @@ static void KillCron(void *pData)
|
|||||||
static int CronTask(void *pData)
|
static int CronTask(void *pData)
|
||||||
{
|
{
|
||||||
Statistics *old;
|
Statistics *old;
|
||||||
|
|
||||||
pCron self = (pCron) pData;
|
pCron self = (pCron) pData;
|
||||||
int iRet;
|
int iRet;
|
||||||
Tcl_Interp *pTcl = pServ->pSics->pTcl;
|
Tcl_Interp *pTcl = pServ->pSics->pTcl;
|
||||||
time_t now;
|
time_t now;
|
||||||
struct tm *nowtm;
|
struct tm tm;
|
||||||
char buffer[1024];
|
char datim[24];
|
||||||
|
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -79,16 +84,12 @@ static int CronTask(void *pData)
|
|||||||
MacroPop();
|
MacroPop();
|
||||||
if (iRet != TCL_OK) {
|
if (iRet != TCL_OK) {
|
||||||
if (strcmp(pTcl->result, "stop") == 0) {
|
if (strcmp(pTcl->result, "stop") == 0) {
|
||||||
snprintf(buffer,1024,"sicscron script '%s' stopped",
|
SCPrintf(self->pCon, eLogError, "sicscron script '%s' stopped", self->pCommand);
|
||||||
self->pCommand);
|
|
||||||
SCPrintf(self->pCon, eLogError, buffer);
|
|
||||||
WriteToCommandLog("SICSCRON:", buffer);
|
|
||||||
self->iEnd = 0;
|
self->iEnd = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
snprintf(buffer,1024,"ERROR in sicscron script '%s': %s", self->pCommand, pTcl->result);
|
|
||||||
SCPrintf(self->pCon, eLogError,buffer);
|
SCPrintf(self->pCon, eLogError, "ERROR in sicscron script '%s': %s", self->pCommand, pTcl->result);
|
||||||
WriteToCommandLog("SICSCRON:", buffer);
|
|
||||||
}
|
}
|
||||||
if (self->iEnd == 2) { /* dolater command */
|
if (self->iEnd == 2) { /* dolater command */
|
||||||
self->iEnd = 0;
|
self->iEnd = 0;
|
||||||
@ -98,10 +99,10 @@ static int CronTask(void *pData)
|
|||||||
if (now > self->tNext)
|
if (now > self->tNext)
|
||||||
self->tNext = now;
|
self->tNext = now;
|
||||||
}
|
}
|
||||||
/* printf("CronTask return: %d\n", self->iEnd > 0);*/
|
if (self->iEnd <= 0) {
|
||||||
if(self->iEnd <= 0){
|
tm = *localtime(&now);
|
||||||
snprintf(buffer,1024,"crontask terminating at %s on %d", ctime(&now), self->iEnd);
|
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
||||||
WriteToCommandLog("SICSCRON", buffer);
|
SCPrintf(self->pCon, eLog, "crontask'%s' terminating at %s", self->pCommand, datim);
|
||||||
}
|
}
|
||||||
return self->iEnd > 0;
|
return self->iEnd > 0;
|
||||||
}
|
}
|
||||||
@ -121,17 +122,30 @@ static void CronSignal(void *pData, int iID, void *pSigData)
|
|||||||
self->iEnd = 0;
|
self->iEnd = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (iID == CRONLIST) {
|
if (iID == CRONFUNC) {
|
||||||
data = pSigData;
|
data = pSigData;
|
||||||
if (self->iEnd == 2 && data->dolater) {
|
switch (data->func) {
|
||||||
|
case cron_list:
|
||||||
tm = *localtime(&self->tNext);
|
tm = *localtime(&self->tNext);
|
||||||
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
||||||
SCPrintf(data->pCon, eLog, "%s %s", datim, self->pCommand);
|
if (self->iInterval < 0) {
|
||||||
} else if (self->iEnd == 1 && !data->dolater) {
|
SCPrintf(data->pCon, eLog, "%s %s", datim, self->pCommand);
|
||||||
tm = *localtime(&self->tNext);
|
} else {
|
||||||
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
SCPrintf(data->pCon, eLog, "%s %8d %s", datim,
|
||||||
SCPrintf(data->pCon, eLog, "%s %8d %s", datim,
|
self->iInterval, self->pCommand);
|
||||||
self->iInterval, self->pCommand);
|
}
|
||||||
|
break;
|
||||||
|
case cron_stop:
|
||||||
|
if (strcmp(data->cmd, self->pCommand) == 0) {
|
||||||
|
self->iEnd = 0;
|
||||||
|
data->cnt++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case cron_count:
|
||||||
|
if (strcmp(data->cmd, self->pCommand) == 0) {
|
||||||
|
data->cnt++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -150,22 +164,38 @@ int MakeCron(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc == 2 && strcasecmp(argv[1], "list") == 0) {
|
if (argc >= 2 && strcasecmp(argv[1], "list") == 0) {
|
||||||
if (strcasecmp(argv[0], "dolater") == 0) {
|
data.func = cron_list;
|
||||||
data.dolater = 1;
|
SCPrintf(pCon, eLog, "Date Time Interval Command");
|
||||||
SCPrintf(pCon, eError, "Date Time Command");
|
|
||||||
} else {
|
|
||||||
data.dolater = 0;
|
|
||||||
SCPrintf(pCon, eError, "Date Time Interval Command");
|
|
||||||
}
|
|
||||||
data.pCon = pCon;
|
data.pCon = pCon;
|
||||||
TaskSignal(pServ->pTasker, CRONLIST, &data);
|
TaskSignal(pServ->pTasker, CRONFUNC, &data);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > 2 && strcasecmp(argv[1], "stop") == 0) {
|
||||||
|
data.func = cron_stop;
|
||||||
|
data.cmd = argv[2];
|
||||||
|
data.cnt = 0;
|
||||||
|
TaskSignal(pServ->pTasker, CRONFUNC, &data);
|
||||||
|
SCPrintf(pCon, eValue, "%d cron jobs stopped", data.cnt);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > 2 && strcasecmp(argv[1], "count") == 0) {
|
||||||
|
data.func = cron_count;
|
||||||
|
data.cmd = argv[2];
|
||||||
|
data.cnt = 0;
|
||||||
|
TaskSignal(pServ->pTasker, CRONFUNC, &data);
|
||||||
|
SCPrintf(pCon, eValue, "%d", data.cnt);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enough arguments? */
|
/* enough arguments? */
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
SCPrintf(pCon, eError, "ERROR: usage: %s <interval> <script>", argv[0]);
|
SCPrintf(pCon, eError, "ERROR: usage: %s <interval> <script>", argv[0]);
|
||||||
|
SCPrintf(pCon, eError, "ERROR: %s list", argv[0]);
|
||||||
|
SCPrintf(pCon, eError, "ERROR: %s stop <script>", argv[0]);
|
||||||
|
SCPrintf(pCon, eError, "ERROR: %s count <script>", argv[0]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,8 +236,8 @@ int MakeCron(SConnection * pCon, SicsInterp * pSics, void *pData,
|
|||||||
pNew->iEnd = 1;
|
pNew->iEnd = 1;
|
||||||
}
|
}
|
||||||
pNew->stat = StatisticsNew(cmd);
|
pNew->stat = StatisticsNew(cmd);
|
||||||
|
|
||||||
TaskRegisterN(pServ->pTasker,"sicscron", CronTask, CronSignal, KillCron, pNew, 1);
|
TaskRegisterN(pServ->pTasker, cmd, CronTask, CronSignal, KillCron, pNew, 1);
|
||||||
SCSendOK(pCon);
|
SCSendOK(pCon);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user