0% found this document useful (0 votes)
16 views3 pages

CronJob Management for Developers

Uploaded by

delgado jhonny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

CronJob Management for Developers

Uploaded by

delgado jhonny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//clase del helper

public class CronJob


{
private readonly Dictionary<string, Timer> jobs = new();
private readonly object lockObj = new();

public DateTime GetNextExecution(DateTime initialDate, string crontabExpr)


{
var schedule = [Link](crontabExpr,
[Link]);
var utcDate = [Link]();
return [Link](utcDate,
[Link]).GetValueOrDefault().ToLocalTime();
}

public string ConvertDateToExpression(DateTime initialDate)


{
int second = [Link];
int minute = [Link];
int hour = [Link];
int dayOfMonth = [Link];
int month = [Link];
int dayOfWeek = (int)[Link];

string crontab = $"{second} {minute} {hour} {dayOfMonth} {month}


{dayOfWeek}";

try
{
[Link](crontab, [Link]);
}
catch (FormatException ex)
{
[Link]("Error al analizar la expresi�n crontab: " +
[Link]);
throw;
}

return crontab;
}

public void Add(string name, string schedule, Action callback)


{
lock (lockObj)
{
if ([Link](name))
{
throw new InvalidOperationException("Ya existe un trabajo con
el mismo nombre.");
}

var scheduleParsed = [Link](schedule,


[Link]);

Timer timer = null;


TimerCallback timerCallback = _ =>
{
callback();
var nextOccurrence =
[Link]([Link],
[Link]).GetValueOrDefault();
[Link]((nextOccurrence - [Link]),
[Link](-1));
};

var firstOccurrence =
[Link]([Link],
[Link]).GetValueOrDefault();
timer = new Timer(timerCallback, null, (firstOccurrence -
[Link]), [Link](-1));

jobs[name] = timer;
}
}

public bool Remove(string name)


{
lock (lockObj)
{
if (![Link](name, out var timer))
{
return false;
}

[Link]();
[Link](name);
return true;
}
}

public bool Exists(string name)


{
lock (lockObj)
{
return [Link](name);
}
}

public Dictionary<string, Timer> List()


{
lock (lockObj)
{
return new Dictionary<string, Timer>(jobs);
}
}

public void Start()


{
// No es necesario, los temporizadores se inician autom�ticamente.
}

public void Stop()


{
lock (lockObj)
{
foreach (var timer in [Link])
{
[Link]();
}

[Link]();
}
}
}

//esto va en el program

try
{
int[] numbers = { 1, 2, 3, 4 };
foreach (int number in numbers)
{
[Link]($"Job{number}", $"*/{3 + number} * * * * *", () =>
[Link]($"Job {number} executed at:" + [Link]));
}
}catch(Exception e)
{

You might also like