Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F2115276
scheduled_task_manager.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Nov 18, 5:46 PM
Size
7 KB
Mime Type
text/x-php
Expires
Fri, Nov 20, 5:46 PM (30 m, 47 s)
Engine
blob
Format
Raw Data
Handle
992174
Attached To
rINP In-Portal
scheduled_task_manager.php
View Options
<?php
/**
* @version $Id: scheduled_task_manager.php 16884 2026-05-14 08:16:19Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined
(
'FULL_PATH'
)
or
die
(
'restricted access!'
);
class
kScheduledTaskManager
extends
kBase
implements
kiCacheable
{
/**
* Events, that should be run after parser initialization
*
* @var Array
* @access protected
*/
protected
$tasks
=
Array
();
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public
function
setFromCache
(&
$data
)
{
$this
->
tasks
=
$data
[
'EventManager.scheduledTasks'
];
}
/**
* Gets object data for caching
*
* @return Array
* @access public
*/
public
function
getToCache
()
{
return
Array
(
'EventManager.scheduledTasks'
=>
$this
->
tasks
,
);
}
/**
* Returns information about registered scheduled tasks
*
* @param bool $from_cache
* @return Array
* @access public
*/
public
function
getAll
(
$from_cache
=
false
)
{
static
$scheduled_tasks
=
null
;
if
(
$from_cache
)
{
return
$this
->
tasks
;
}
if
(
!
isset
(
$scheduled_tasks
)
)
{
$timeout_clause
=
'LastRunStatus = '
.
ScheduledTask
::
LAST_RUN_RUNNING
.
' AND Timeout > 0 AND '
.
adodb_mktime
()
.
' - LastRunOn > Timeout'
;
$sql
=
'SELECT *
FROM '
.
$this
->
Application
->
getUnitOption
(
'scheduled-task'
,
'TableName'
)
.
'
WHERE (Status = '
.
STATUS_ACTIVE
.
') AND ((LastRunStatus != '
.
ScheduledTask
::
LAST_RUN_RUNNING
.
') OR ('
.
$timeout_clause
.
'))'
;
$scheduled_tasks
=
$this
->
Conn
->
Query
(
$sql
,
'Name'
);
}
return
$scheduled_tasks
;
}
/**
* Add new scheduled task
*
* @param string $short_name name to be used to store last maintenance run info
* @param string $event_string
* @param int $run_schedule run schedule like for Cron
* @param int $status
* @access public
*/
public
function
add
(
$short_name
,
$event_string
,
$run_schedule
,
$status
=
STATUS_ACTIVE
)
{
$this
->
tasks
[
$short_name
]
=
Array
(
'Event'
=>
$event_string
,
'RunSchedule'
=>
$run_schedule
,
'Status'
=>
$status
);
}
/**
* Run registered scheduled tasks with specified event type
*
* @param bool $from_cron
* @access public
*/
public
function
runAll
(
$from_cron
=
false
)
{
if
(
defined
(
'IS_INSTALL'
)
)
{
return
;
}
$use_cron
=
$this
->
Application
->
ConfigValue
(
'RunScheduledTasksFromCron'
);
if
(
(
$use_cron
&&
!
$from_cron
)
||
(!
$use_cron
&&
$from_cron
)
)
{
// match execution place with one, set in config
return
;
}
ignore_user_abort
(
true
);
set_time_limit
(
0
);
$events_source
=
$this
->
getAll
();
$user_id
=
$this
->
Application
->
RecallVar
(
'user_id'
);
$this
->
Application
->
StoreVar
(
'user_id'
,
USER_ROOT
,
true
);
// to prevent permission checking inside events, true for optional storage
/** @var SiteHelper $site_helper */
$site_helper
=
$this
->
Application
->
recallObject
(
'SiteHelper'
);
$site_domain_id
=
$site_helper
->
getDomainByName
(
'DomainName'
,
DOMAIN
);
foreach
(
$events_source
as
$short_name
=>
$event_data
)
{
if
(
$site_domain_id
&&
$event_data
[
'SiteDomainLimitation'
]
!=
''
)
{
$site_domains
=
explode
(
'|'
,
substr
(
$event_data
[
'SiteDomainLimitation'
],
1
,
-
1
));
if
(
!
in_array
(
$site_domain_id
,
$site_domains
)
)
{
// scheduled task isn't allowed on this site domain
continue
;
}
}
// remember LastTimeoutOn only for events that are still running and will be reset
if
(
$event_data
[
'LastRunStatus'
]
==
ScheduledTask
::
LAST_RUN_RUNNING
)
{
$this
->
update
(
$short_name
,
Array
(
'LastTimeoutOn'
=>
adodb_mktime
()));
}
$next_run
=
(
int
)
$event_data
[
'NextRunOn'
];
if
(
$next_run
&&
(
$next_run
>
adodb_mktime
()))
{
continue
;
}
$event_data
[
'Name'
]
=
$short_name
;
$start_time
=
adodb_mktime
();
// Scheduled task was updated from another process.
if
(
!
$this
->
claim
(
$event_data
,
$start_time
)
)
{
continue
;
}
$this
->
run
(
$event_data
,
$start_time
);
}
$this
->
Application
->
StoreVar
(
'user_id'
,
$user_id
,
$user_id
==
USER_GUEST
);
}
/**
* Runs scheduled task based on given data
*
* @param array $scheduled_task_data Scheduled task data.
* @param integer|null $claimed_on Claimed on.
*
* @return boolean
*/
public
function
run
(
array
$scheduled_task_data
,
$claimed_on
=
null
)
{
if
(
$claimed_on
!==
null
)
{
$start_time
=
$claimed_on
;
}
else
{
$start_time
=
adodb_mktime
();
// Scheduled task was updated from another process.
if
(
!
$this
->
claim
(
$scheduled_task_data
,
$start_time
)
)
{
return
false
;
}
}
$event
=
new
kEvent
(
$scheduled_task_data
[
'Event'
]);
$event
->
redirect
=
false
;
$this
->
Application
->
HandleEvent
(
$event
);
/** @var kCronHelper $cron_helper */
$cron_helper
=
$this
->
Application
->
recallObject
(
'kCronHelper'
);
$now
=
adodb_mktime
();
$next_run
=
$cron_helper
->
getMatch
(
$scheduled_task_data
[
'RunSchedule'
],
$start_time
);
while
(
$next_run
<
$now
)
{
// in case event execution took longer, then RunSchedule (don't use <=, because RunSchedule can be 0)
$next_run
=
$cron_helper
->
getMatch
(
$scheduled_task_data
[
'RunSchedule'
],
$next_run
);
}
// remember, when scheduled task execution ended
$fields_hash
=
Array
(
'NextRunOn'
=>
$next_run
,
'RunTime'
=>
$now
-
$start_time
,
'LastRunStatus'
=>
$event
->
status
==
kEvent
::
erSUCCESS
?
ScheduledTask
::
LAST_RUN_SUCCEEDED
:
ScheduledTask
::
LAST_RUN_FAILED
,
);
$this
->
update
(
$scheduled_task_data
[
'Name'
],
$fields_hash
);
return
true
;
}
/**
* Remember scheduled task execution start time when possible.
*
* @param array $scheduled_task_data Scheduled task data.
* @param integer $start_time Start time.
*
* @return boolean
*/
protected
function
claim
(
array
$scheduled_task_data
,
$start_time
)
{
$event
=
new
kEvent
(
$scheduled_task_data
[
'Event'
]);
// Don't process scheduled tasks, left from disabled modules.
if
(
!
$this
->
Application
->
prefixRegistred
(
$event
->
Prefix
)
)
{
return
false
;
}
/** @var kCronHelper $cron_helper */
$cron_helper
=
$this
->
Application
->
recallObject
(
'kCronHelper'
);
return
$this
->
update
(
$scheduled_task_data
[
'Name'
],
array
(
'LastRunOn'
=>
$start_time
,
'LastRunStatus'
=>
ScheduledTask
::
LAST_RUN_RUNNING
,
'NextRunOn'
=>
$cron_helper
->
getMatch
(
$scheduled_task_data
[
'RunSchedule'
],
$start_time
),
),
array
(
$this
->
buildWhereClause
(
'LastRunOn'
,
$scheduled_task_data
[
'LastRunOn'
]),
'LastRunStatus = '
.
$this
->
Conn
->
qstr
(
$scheduled_task_data
[
'LastRunStatus'
]),
$this
->
buildWhereClause
(
'NextRunOn'
,
$scheduled_task_data
[
'NextRunOn'
]),
)
);
}
/**
* Builds a where clause for a given field & value.
*
* @param string $field Field.
* @param integer|null $value Value.
*
* @return string
*/
protected
function
buildWhereClause
(
$field
,
$value
)
{
return
$value
===
null
?
$field
.
' IS NULL'
:
$field
.
' = '
.
$this
->
Conn
->
qstr
(
$value
);
}
/**
* Updates scheduled task record with latest changes about its invocation progress.
*
* @param string $scheduled_task_name Scheduled task name.
* @param array $fields_hash Fields hash.
* @param array $where_clause Where clause.
*
* @return boolean
*/
protected
function
update
(
$scheduled_task_name
,
array
$fields_hash
,
array
$where_clause
=
array
())
{
array_unshift
(
$where_clause
,
'Name = '
.
$this
->
Conn
->
qstr
(
$scheduled_task_name
)
);
$this
->
Conn
->
doUpdate
(
$fields_hash
,
$this
->
Application
->
getUnitOption
(
'scheduled-task'
,
'TableName'
),
'('
.
implode
(
') AND ('
,
$where_clause
)
.
')'
);
return
$this
->
Conn
->
getAffectedRows
()
===
1
;
}
}
Event Timeline
Log In to Comment