다른 대답은 여기에 있습니다. 또한 자신이
(GUI 관리 도구에서 달력에 예약 된 모든 작업을 표시처럼) 더 복잡한 일 처리를 할 필요 발견하면 당신은 PHP의 DateTime
, DateInterval
, DatePeriod
및 관련 클래스를 사용하여 고려를해야한다는 지적 것 당신은 같은 것을 볼 것 DB 테이블을 포함하는 작업 일정 규칙이있을 수 있습니다
id - unique auto-increment
name - human-readable task name
owner - perhaps forieg key to user tables so you know who owns tasks
interval - An string interval specification as used in DateInterval
start_time - Datetime When rule goes into effect
end_time - Datetime When rule is no longer in effect
script_path - path to script of some sort of command recognized by your applcation
last_execution - Datetime for last time script was triggered
next_execution - Datetime in which you store value calculated to be next execution point
active - maybe a flag to enable/disable a rule
perhaps other admin fields like created_time, error_tracking, etc.
을 그리고 당신은 쉽게 DatePeriod는 각 테이블 행에서의 반복 수있는 개체의 컬렉션을 만들 수있다.
다음
$tasks
// have one authoritative now that you use in this script
$now = DateTime();
$now_sql = $now->format('Y-m-d H:i:s');
$sql = <<<EOT
SELECT
id,
name,
interval,
/* etc */
FROM task_rules
WHERE
active = 1
AND
(IS_NULL(start_time) OR start_time <= '{$now_sql}')
AND
(IS_NULL(end_time) OR eend_time > '{$now_sql}')
/* Add this filter if you are trying to query this table
for overdue events */
AND
next_execution <= '{$now_sql}'
/* any other filtering you might want to do */
/* Any ORDER BY and LIMIT clauses */
EOT;
$tasks = array();
//logic to read rows from DB
while ($row = /* Your DB fetch mechanism */) {
// build your task (probably could be its own class,
// perhaps saturated via DB retrieval process), but this is jist
$task = new stdClass();
$task->id = $row->id
$task->name = $row->name;
$task->interval = $row->interval;
$task->start_time = $row->start_time;
// etc. basically map DB row to an object
// start building DateTime and related object representations
// of your tasks
$task->dateInterval = new DateInterval($task->interval);
// determine start/end dates for task sequence
if(empty($task->start_time)) {
// no defined start date, so build start date from last executed time
$task->startDateTime = DateTime::createFromFormat(
'Y-m-d H:i:s',
$task->last_execution
);
} else {
// start date known, so we want to base period sequence on start date
$task->startDateTime = DateTime::createFromFormat(
'Y-m-d H:i:s',
$task->start_date
);
}
if(empty($task->end_time)) {
// No defined end. So set artificial end date based on app needs
// (like we need to show next week, month, year)
$end_datetime = clone $now;
$end_datetime->modify(+ 1 month);
$task->endDateTime = $end_datetime;
} else {
$task->endDateTime = DateTime::createFromFormat(
'Y-m-d H:i:s',
$task->end_time
);
}
$task->datePeriod = new DatePeriod(
$task->startDateTime,
$task->dateInterval,
$task->endDateTime
);
// iterate datePeriod to build array of occurences
// which is more useful than just working with Traversable
// interface of datePeriod and allows you to filter out past
// scheduled occurences
$task->future_occurrences = [];
foreach ($task->datePeriod as $occurence) {
if ($occurence < $now) {
// this is occcurrence in past, do nothing
continue;
}
$task->future_occurrences[] = $occurence;
}
$task->nextDateTime = null;
if(count($task->future_occurrences) > 0) {
$task->nextDateTime = $task->future_occurrences[0];
$task->next_execution = $task->nextDateTime->format('Y-m-d H:i:s');
}
$tasks[] = $task;
}
이 유형 PHP 날짜 시간과 함께 하나의 규칙을 나타내는 각, DatePeriod 당신이 및/또는 디스플레이 작업을 실행하는 데 사용할 수있는 구축 객체의 배열을 포함됩니다 : 즉 같은 것을 볼 수 있습니다. 예를 들어
는 :
// execute all tasks
// just using a simple loop example here
foreach($tasks as $task) {
$command = 'php ' . $task->script_path;
exec($command);
// update DB
$sql = <<<EOT
UPDATE task_rules
SET
last_execution = '{$now_sql}',
next_execution = '{$task->next_execution}'
WHERE id = {$task->id}
EOT;
// and execute using DB tool of choice
}
이 당신이 그들을 제공 얼마나 많은 세부적인 제어 및 접근성에 따라 달라집니다? 이 관리자는 PHP 파일을 업로드 할 수 있습니까? 데이터베이스 항목에서 txt 파일을 생성하고 실제로 crontab을 수정할 수 있습니다. – Blake
[PHP를 사용하여 crontab 작업을 생성, 편집 및 삭제할 수 있습니까?] (http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs) – Hackerman
@ Hackerman은 op가 비 cron 접근 방식을 원했던 것처럼 보입니다. 그럼 누가 다시 알지. – Drew