Index: branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_list.tpl
===================================================================
diff -u -N
--- branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_list.tpl (revision 0)
+++ branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_list.tpl (revision 14880)
@@ -0,0 +1,91 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
Index: branches/5.2.x/admin/system_presets/simple/agents_agent.php
===================================================================
diff -u -N
--- branches/5.2.x/admin/system_presets/simple/agents_agent.php (revision 14879)
+++ branches/5.2.x/admin/system_presets/simple/agents_agent.php (revision 0)
@@ -1,51 +0,0 @@
- Array ('new_item', 'edit', 'delete', 'approve', 'decline', 'process', 'cancel', 'view', 'dbl-click'),
-
-// edit scheduled task
-// 'scheduled_task_edit' => Array ('select', 'cancel', 'reset_edit', 'prev', 'next'),
- );
-
- // fields to hide
- $hidden_fields = Array (
- /* 'ScheduledTaskId', 'Name', 'Type', 'Status', 'Event', 'RunInterval', 'RunMode', 'LastRunOn',
- 'LastRunStatus', 'NextRunOn', 'RunTime', */
- );
-
- // virtual fields to hide
- $virtual_hidden_fields = Array (
- );
-
- // fields to make required
- $required_fields = Array (
- /* 'ScheduledTaskId', 'Name', 'Type', 'Status', 'Event', 'RunInterval', 'RunMode', 'LastRunOn',
- 'LastRunStatus', 'NextRunOn', 'RunTime', */
- );
-
- // virtual fields to make required
- $virtual_required_fields = Array ( );
-
- // tabs during editing
- $hide_edit_tabs = Array (
- );
-
- // hide columns in grids
- $hide_columns = Array (
-// currently not in user
-// 'Default' => Array ( 'ScheduledTaskId', 'Name', 'Type', 'Status', 'Event', 'RunInterval', 'RunMode', 'LastRunOn', 'LastRunStatus', 'NextRunOn', ),
- );
\ No newline at end of file
Index: branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php
===================================================================
diff -u -N
--- branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php (revision 0)
+++ branches/5.2.x/core/units/scheduled_tasks/scheduled_tasks_config.php (revision 14880)
@@ -0,0 +1,174 @@
+ 'scheduled-task',
+ 'ItemClass' => Array ('class' => 'kDBItem', 'file' => '', 'build_event' => 'OnItemBuild'),
+ 'ListClass' => Array ('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'),
+ 'EventHandlerClass' => Array ('class' => 'ScheduledTaskEventHandler', 'file' => 'scheduled_task_eh.php', 'build_event' => 'OnBuild'),
+ 'TagProcessorClass' => Array ('class' => 'kDBTagProcessor', 'file' => '', 'build_event' => 'OnBuild'),
+
+ 'AutoLoad' => true,
+
+ 'QueryString' => Array (
+ 1 => 'id',
+ 2 => 'Page',
+ 3 => 'PerPage',
+ 4 => 'event',
+ 5 => 'mode',
+ ),
+
+ 'Hooks' => Array (
+ Array (
+ 'Mode' => hAFTER,
+ 'Conditional' => false,
+ 'HookToPrefix' => 'adm',
+ 'HookToSpecial' => '*',
+ 'HookToEvent' => Array ('OnAfterCacheRebuild'),
+ 'DoPrefix' => '',
+ 'DoSpecial' => '*',
+ 'DoEvent' => 'OnRefresh',
+ ),
+ ),
+
+ 'IDField' => 'ScheduledTaskId',
+
+ 'TableName' => TABLE_PREFIX . 'ScheduledTasks',
+
+ 'TitleField' => 'Name',
+
+ 'StatusField' => Array ('Status'),
+
+ 'TitlePresets' => Array (
+ 'default' => Array (
+ 'new_status_labels' => Array ('scheduled-task' => '!la_title_AddingScheduledTask!'),
+ 'edit_status_labels' => Array ('scheduled-task' => '!la_title_EditingScheduledTask!'),
+ 'new_titlefield' => Array ('scheduled-task' => '!la_title_NewScheduledTask!'),
+ ),
+
+ 'scheduled_task_list' => Array (
+ 'prefixes' => Array ('scheduled-task_List'), 'format' => "!la_title_ScheduledTasks!",
+ 'toolbar_buttons' => Array ('new_item', 'edit', 'delete', 'approve', 'decline', 'process', 'cancel', 'view', 'dbl-click'),
+ ),
+
+ 'scheduled_task_edit' => Array (
+ 'prefixes' => Array ('scheduled-task'), 'format' => "#scheduled-task_status# '#scheduled-task_titlefield#'",
+ 'toolbar_buttons' => Array ('select', 'cancel', 'reset_edit', 'prev', 'next'),
+ ),
+ ),
+
+ 'PermSection' => Array('main' => 'in-portal:scheduled_tasks'),
+
+ 'Sections' => Array (
+ 'in-portal:scheduled_tasks' => Array (
+ 'parent' => 'in-portal:website_setting_folder',
+ 'icon' => 'conf_agents',
+ 'label' => 'la_title_ScheduledTasks',
+ 'url' => Array('t' => 'scheduled_tasks/scheduled_task_list', 'pass' => 'm'),
+ 'permissions' => Array('view', 'add', 'edit', 'delete'),
+ 'priority' => 6,
+ 'type' => stTREE,
+ ),
+ ),
+
+ 'ListSQLs' => Array (
+ '' => ' SELECT %1$s.* %2$s FROM %1$s',
+ ),
+
+ 'ListSortings' => Array (
+ '' => Array (
+ 'Sorting' => Array ('Name' => 'asc'),
+ )
+ ),
+
+ 'Fields' => Array (
+ 'ScheduledTaskId' => Array ('type' => 'int', 'not_null' => 1, 'default' => 0),
+
+ 'Name' => Array (
+ 'type' => 'string', 'max_len' => 255,
+ 'unique' => Array (),
+ 'required' => 1, 'not_null' => 1, 'default' => ''
+ ),
+
+ 'Type' => Array (
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_User', 2 => 'la_opt_System'), 'use_phrases' => 1,
+ 'required' => 1, 'not_null' => 1, 'default' => 1
+ ),
+ 'Status' => Array (
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Active', 0 => 'la_opt_Disabled'), 'use_phrases' => 1,
+ 'required' => 1, 'not_null' => 1, 'default' => 1
+ ),
+ 'Event' => Array (
+ 'type' => 'string', 'max_len' => 255,
+ 'formatter' => 'kFormatter', 'regexp' => '/^[a-z-]*[.]{0,1}[a-z-]*:On[A-Za-z0-9]*$/',
+ 'required' => 1, 'not_null' => 1, 'default' => ''
+ ),
+ 'RunInterval' => Array ('type' => 'int', 'required' => 1, 'not_null' => 1, 'default' => 0),
+ 'RunMode' => Array (
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter', 'options' => Array (reBEFORE => 'la_opt_Before', reAFTER => 'la_opt_After'), 'use_phrases' => 1,
+ 'required' => 1, 'not_null' => 1, 'default' => 2
+ ),
+ 'LastRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
+
+ 'LastRunStatus' => Array (
+ 'type' => 'int',
+ 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Success', 0 => 'la_opt_Failed', 2 => 'la_opt_Running'), 'use_phrases' => 1,
+ 'not_null' => 1, 'default' => 1
+ ),
+
+ 'NextRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'required' => 1, 'default' => '#NOW#'),
+ 'RunTime' => Array ('type' => 'int', 'not_null' => 1, 'default' => 0),
+ 'Timeout' => Array (
+ 'type' => 'int',
+ 'min_value_inc' => 1,
+ 'not_null' => 1, 'default' => NULL
+ ),
+ 'LastTimeoutOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
+ 'SiteDomainLimitation' => Array (
+ 'type' => 'string', 'max_len' => 255,
+ 'formatter' => 'kOptionsFormatter', 'options_sql' => 'SELECT %s FROM ' . TABLE_PREFIX . 'SiteDomains ORDER BY DomainName ASC', 'option_key_field' => 'DomainId', 'option_title_field' => 'DomainName', 'multiple' => 1,
+ 'not_null' => 1, 'default' => ''
+ ),
+ ),
+
+ 'Grids' => Array (
+ 'Default' => Array (
+ 'Icons' => Array (
+ 'default' => 'icon16_item.png',
+ 0 => 'icon16_disabled.png',
+ ),
+ 'Fields' => Array (
+ 'ScheduledTaskId' => Array ('title' => 'column:la_fld_Id', 'data_block' => 'grid_checkbox_td', 'filter_block' => 'grid_range_filter', 'width' => 50, ),
+ 'Name' => Array ('filter_block' => 'grid_like_filter', 'width' => 200, ),
+ 'Type' => Array ('filter_block' => 'grid_options_filter', 'width' => 60, ),
+ 'Event' => Array ('filter_block' => 'grid_like_filter', 'width' => 280, ),
+ 'RunInterval' => Array ('filter_block' => 'grid_range_filter', 'width' => 100, ),
+ 'RunMode' => Array ('filter_block' => 'grid_options_filter', 'width' => 85, ),
+ 'LastRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
+ 'RunTime' => Array ('filter_block' => 'grid_range_filter', 'width' => 145, ),
+ 'LastRunStatus' => Array ('filter_block' => 'grid_options_filter', 'width' => 120, ),
+ 'NextRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
+ 'Status' => Array ('filter_block' => 'grid_options_filter', 'width' => 65, ),
+ 'Timeout' => Array ('filter_block' => 'grid_range_filter', 'width' => 85, ),
+ 'LastTimeoutOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
+ 'SiteDomainLimitation' => Array ('data_block' => 'grid_picker_td', 'filter_block' => 'grid_multioptions_filter', 'separator' => ', ', 'width' => 145),
+ ),
+ ),
+ ),
+ );
\ No newline at end of file
Index: branches/5.2.x/core/admin_templates/agents/agent_edit.tpl
===================================================================
diff -u -N
--- branches/5.2.x/core/admin_templates/agents/agent_edit.tpl (revision 14879)
+++ branches/5.2.x/core/admin_templates/agents/agent_edit.tpl (revision 0)
@@ -1,97 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
Index: branches/5.2.x/core/admin_templates/agents/agent_list.tpl
===================================================================
diff -u -N
--- branches/5.2.x/core/admin_templates/agents/agent_list.tpl (revision 14879)
+++ branches/5.2.x/core/admin_templates/agents/agent_list.tpl (revision 0)
@@ -1,91 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
Index: branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php
===================================================================
diff -u -N
--- branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php (revision 0)
+++ branches/5.2.x/core/units/scheduled_tasks/scheduled_task_eh.php (revision 14880)
@@ -0,0 +1,181 @@
+ Array ('self' => 'add|edit'),
+ 'OnRun' => Array ('self' => 'add|edit'),
+ );
+
+ $this->permMapping = array_merge($this->permMapping, $permissions);
+ }
+
+ /**
+ * [HOOK] Refreshes scheduled task list in database based on cached data from unit configs
+ *
+ * @param kEvent $event
+ */
+ function OnRefresh(&$event)
+ {
+ $scheduled_tasks_from_cache = $this->Application->EventManager->getScheduledTasks(true);
+
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object kDBItem */
+
+ $processed_ids = Array ();
+ $scheduled_tasks_from_db = $this->Conn->Query($object->GetSelectSQL(), 'Name');
+
+ foreach ($scheduled_tasks_from_cache as $run_mode => $events) {
+
+ foreach ($events as $scheduled_task_name => $scheduled_task_params) {
+ if ( !isset($scheduled_tasks_from_db[$scheduled_task_name]) ) {
+ $fields_hash = Array (
+ 'Event' => $scheduled_task_params['EventName'],
+ 'Name' => $scheduled_task_name,
+ 'Type' => ScheduledTask::TYPE_SYSTEM,
+ 'Status' => array_key_exists('Status', $scheduled_task_params) ? $scheduled_task_params['Status'] : STATUS_ACTIVE,
+ 'RunInterval' => $scheduled_task_params['RunInterval'],
+ 'RunMode' => $run_mode,
+ );
+
+ $object->Clear();
+ $object->SetDBFieldsFromHash($fields_hash);
+ $object->Create();
+ }
+ else {
+ $object->LoadFromHash( $scheduled_tasks_from_db[$scheduled_task_name] );
+ }
+
+ $processed_ids[] = $object->GetID();
+ }
+
+ }
+
+ // delete all non-processed scheduled tasks (ones, that were deleted from unit configs)
+ $sql = 'SELECT ' . $object->IDField . '
+ FROM ' . $object->TableName . '
+ WHERE (Type = ' . ScheduledTask::TYPE_SYSTEM . ') AND (' . $object->IDField . ' NOT IN (' . implode(',', $processed_ids) . '))';
+ $delete_ids = $this->Conn->GetCol($sql);
+
+ if ($delete_ids) {
+ $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ /* @var $temp_handler kTempTablesHandler */
+
+ $temp_handler->DeleteItems($event->Prefix, $event->Special, $delete_ids);
+ }
+
+ $this->Application->removeObject($event->getPrefixSpecial());
+ }
+
+ /**
+ * Don't allow to delete other user's messages
+ *
+ * @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
+ */
+ protected function customProcessing(&$event, $type)
+ {
+ if ($event->Name == 'OnMassDelete' && $type == 'before') {
+ if ($this->Application->isDebugMode()) {
+ // allow to delete system scheduled tasks in debug mode
+ return ;
+ }
+
+ $ids = $event->getEventParam('ids');
+ if ($ids) {
+ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
+ $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
+
+ $sql = 'SELECT ' . $id_field . '
+ FROM ' . $table_name . '
+ WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND Type <> ' . ScheduledTask::TYPE_SYSTEM;
+ $allowed_ids = $this->Conn->GetCol($sql);
+
+ $event->setEventParam('ids', $allowed_ids);
+ }
+ }
+ }
+
+ /**
+ * Cancels scheduled tasks, that are currently running
+ *
+ * @param kEvent $event
+ */
+ function OnMassCancel(&$event)
+ {
+ $ids = $this->StoreSelectedIDs($event);
+
+ if ($ids) {
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object kDBItem */
+
+ foreach ($ids as $id) {
+ $object->Load($id);
+
+ if ($object->GetDBField('LastRunStatus') == ScheduledTask::LAST_RUN_RUNNING) {
+ // only changes status, doesn't affect currency running scheduled tasks
+ $object->SetDBField('LastRunStatus', ScheduledTask::LAST_RUN_FAILED);
+ $object->Update();
+ }
+ }
+ }
+
+ $this->clearSelectedIDs($event);
+ }
+
+ /**
+ * Runs selected scheduled tasks
+ *
+ * @param kEvent $event
+ */
+ function OnRun(&$event)
+ {
+ $ids = $this->StoreSelectedIDs($event);
+
+ if ($ids) {
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object kDBItem */
+
+ $where_clause = Array (
+ $object->TableName . '.' . $object->IDField . ' IN (' . implode(',', $ids) . ')',
+ $object->TableName . '.Status = ' . STATUS_ACTIVE,
+ $object->TableName . '.LastRunStatus <> ' . ScheduledTask::LAST_RUN_RUNNING,
+ );
+
+ $sql = $object->GetSelectSQL() . '
+ WHERE (' . implode(') AND (', $where_clause) . ')';
+ $scheduled_tasks = $this->Conn->Query($sql);
+
+ foreach ($scheduled_tasks as $scheduled_task_data) {
+ $scheduled_task_data['EventName'] = $scheduled_task_data['Event'];
+ $this->Application->EventManager->runScheduledTask($scheduled_task_data);
+ }
+ }
+
+ $this->clearSelectedIDs($event);
+ }
+ }
\ No newline at end of file
Index: branches/5.2.x/core/kernel/managers/agent_manager.php
===================================================================
diff -u -N -r14879 -r14880
--- branches/5.2.x/core/kernel/managers/agent_manager.php (.../agent_manager.php) (revision 14879)
+++ branches/5.2.x/core/kernel/managers/agent_manager.php (.../scheduled_task_manager.php) (revision 14880)
@@ -1,6 +1,6 @@
registerClass('kEventManager', KERNEL_PATH . '/event_manager.php', 'EventManager', 'kiCacheable');
$this->registerClass('kHookManager', KERNEL_PATH . '/managers/hook_manager.php', null, 'kiCacheable');
- $this->registerClass('kScheduledTaskManager', KERNEL_PATH . '/managers/agent_manager.php', null, 'kiCacheable');
+ $this->registerClass('kScheduledTaskManager', KERNEL_PATH . '/managers/scheduled_task_manager.php', null, 'kiCacheable');
$this->registerClass('kRequestManager', KERNEL_PATH . '/managers/request_manager.php');
$this->registerClass('kUrlManager', KERNEL_PATH . '/managers/url_manager.php');
@@ -2616,7 +2616,7 @@
/**
- * Allows to detect if page is browsed by spider (293 agents supported)
+ * Allows to detect if page is browsed by spider (293 scheduled_tasks supported)
*
* @return bool
*/
Index: branches/5.2.x/core/kernel/managers/agent_manager.php
===================================================================
diff -u -N
--- branches/5.2.x/core/kernel/managers/agent_manager.php (revision 14879)
+++ branches/5.2.x/core/kernel/managers/agent_manager.php (revision 0)
@@ -1,266 +0,0 @@
-before = $data['EventManager.beforeRegularEvents'];
- $this->after = $data['EventManager.afterRegularEvents'];
- }
-
- /**
- * Gets object data for caching
- *
- * @return Array
- * @access public
- */
- public function getToCache()
- {
- return Array (
- 'EventManager.beforeRegularEvents' => $this->before,
- 'EventManager.afterRegularEvents' => $this->after,
- );
- }
-
- /**
- * 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 Array (
- reBEFORE => $this->before,
- reAFTER => $this->after,
- );
- }
-
- 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 . '))';
- $all_scheduled_tasks = $this->Conn->Query($sql);
-
- $scheduled_tasks = Array (
- reBEFORE => Array (),
- reAFTER => Array (),
- );
-
- foreach ($all_scheduled_tasks as $scheduled_task_data) {
- $scheduled_tasks[ $scheduled_task_data['RunMode'] ][ $scheduled_task_data['Name'] ] = Array (
- 'EventName' => $scheduled_task_data['Event'],
- 'RunInterval' => (int)$scheduled_task_data['RunInterval'],
- 'LastRunOn' => (int)$scheduled_task_data['LastRunOn'],
- 'NextRunOn' => (int)$scheduled_task_data['NextRunOn'],
- 'Status' => $scheduled_task_data['Status'],
- 'LastRunStatus' => $scheduled_task_data['LastRunStatus'],
- 'SiteDomainLimitation' => $scheduled_task_data['SiteDomainLimitation'],
- );
- }
- }
-
- return $scheduled_tasks;
- }
-
- /**
- * Returns scheduled tasks by type
- *
- * @param int $type
- * @return Array
- * @access protected
- */
- protected function &getByType($type)
- {
- if ($type == reBEFORE) {
- return $this->before;
- }
-
- return $this->after;
- }
-
- /**
- * Add new scheduled task
- *
- * @param string $short_name name to be used to store last maintenance run info
- * @param string $event_name
- * @param int $run_interval run interval in seconds
- * @param int $type before or after scheduled task
- * @param int $status
- * @access public
- */
- public function add($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE)
- {
- $scheduled_tasks =& $this->getByType($type);
-
- $scheduled_tasks[$short_name] = Array (
- 'EventName' => $event_name, 'RunInterval' => $run_interval, 'Status' => $status
- );
- }
-
- /**
- * Run registered scheduled tasks with specified event type
- *
- * @param int $event_type
- * @param bool $from_cron
- * @access public
- */
- public function runAll($event_type = reBEFORE, $from_cron = false)
- {
- if ( defined('IS_INSTALL') ) {
- return ;
- }
-
- if ( !$from_cron && $this->Application->ConfigValue('RunScheduledTasksFromCron') ) {
- // if scheduled tasks are set to run from cron
- return ;
- }
-
- $scheduled_tasks = $this->getAll();
- $events_source = $scheduled_tasks[$event_type];
-
- $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
-
- $site_helper =& $this->Application->recallObject('SiteHelper');
- /* @var $site_helper 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 = $event_data['NextRunOn'];
-
- if ($next_run && ($next_run > adodb_mktime())) {
- continue;
- }
-
- $event_data['Name'] = $short_name;
- $this->run($event_data);
- }
-
- $this->Application->StoreVar('user_id', $user_id, $user_id == USER_GUEST);
- }
-
- /**
- * Runs scheduled task based on given data
- *
- * @param Array $scheduled_task_data
- * @return bool
- * @access public
- */
- public function run($scheduled_task_data)
- {
- $event = new kEvent($scheduled_task_data['EventName']);
-
- if ( !$this->Application->prefixRegistred($event->Prefix) ) {
- // don't process scheduled tasks, left from disabled modules
- return false;
- }
-
- $start_time = adodb_mktime();
-
- // remember, when scheduled task execution started
- $fields_hash = Array (
- 'LastRunOn' => $start_time,
- 'LastRunStatus' => ScheduledTask::LAST_RUN_RUNNING,
- 'NextRunOn' => $start_time + $scheduled_task_data['RunInterval'],
- );
-
- $this->update($scheduled_task_data['Name'], $fields_hash);
-
- $event->redirect = false;
- $this->Application->HandleEvent($event);
-
- $now = adodb_mktime();
- $next_run = $scheduled_task_data['RunInterval'] ? $start_time + $scheduled_task_data['RunInterval'] : $now;
-
- while ($next_run < $now) {
- // in case event execution took longer, then RunInterval (don't use <=, because RunInterval can be 0)
- $next_run += $scheduled_task_data['RunInterval'];
- }
-
- // remember, when scheduled task execution ended
- $fields_hash = Array (
- 'NextRunOn' => $next_run,
- 'RunTime' => round(($now - $start_time) / 60),
- 'LastRunStatus' => $event->status == kEvent::erSUCCESS ? ScheduledTask::LAST_RUN_SUCCEEDED : ScheduledTask::LAST_RUN_FAILED,
- );
-
- $this->update($scheduled_task_data['Name'], $fields_hash);
-
- return true;
- }
-
- /**
- * Updates scheduled task record with latest changes about it's invocation progress
- *
- * @param string $scheduled_task_name
- * @param Array $fields_hash
- * @return void
- * @access protected
- */
- protected function update($scheduled_task_name, $fields_hash)
- {
- $this->Conn->doUpdate(
- $fields_hash,
- $this->Application->getUnitOption('scheduled-task', 'TableName'),
- 'Name = ' . $this->Conn->qstr($scheduled_task_name)
- );
- }
-}
\ No newline at end of file
Index: branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_edit.tpl
===================================================================
diff -u -N
--- branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_edit.tpl (revision 0)
+++ branches/5.2.x/core/admin_templates/scheduled_tasks/scheduled_task_edit.tpl (revision 14880)
@@ -0,0 +1,97 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
Index: branches/5.2.x/core/units/agents/agent_eh.php
===================================================================
diff -u -N
--- branches/5.2.x/core/units/agents/agent_eh.php (revision 14879)
+++ branches/5.2.x/core/units/agents/agent_eh.php (revision 0)
@@ -1,181 +0,0 @@
- Array ('self' => 'add|edit'),
- 'OnRun' => Array ('self' => 'add|edit'),
- );
-
- $this->permMapping = array_merge($this->permMapping, $permissions);
- }
-
- /**
- * [HOOK] Refreshes scheduled task list in database based on cached data from unit configs
- *
- * @param kEvent $event
- */
- function OnRefresh(&$event)
- {
- $scheduled_tasks_from_cache = $this->Application->EventManager->getScheduledTasks(true);
-
- $object =& $event->getObject( Array ('skip_autoload' => true) );
- /* @var $object kDBItem */
-
- $processed_ids = Array ();
- $scheduled_tasks_from_db = $this->Conn->Query($object->GetSelectSQL(), 'Name');
-
- foreach ($scheduled_tasks_from_cache as $run_mode => $events) {
-
- foreach ($events as $scheduled_task_name => $scheduled_task_params) {
- if ( !isset($scheduled_tasks_from_db[$scheduled_task_name]) ) {
- $fields_hash = Array (
- 'Event' => $scheduled_task_params['EventName'],
- 'Name' => $scheduled_task_name,
- 'Type' => ScheduledTask::TYPE_SYSTEM,
- 'Status' => array_key_exists('Status', $scheduled_task_params) ? $scheduled_task_params['Status'] : STATUS_ACTIVE,
- 'RunInterval' => $scheduled_task_params['RunInterval'],
- 'RunMode' => $run_mode,
- );
-
- $object->Clear();
- $object->SetDBFieldsFromHash($fields_hash);
- $object->Create();
- }
- else {
- $object->LoadFromHash( $scheduled_tasks_from_db[$scheduled_task_name] );
- }
-
- $processed_ids[] = $object->GetID();
- }
-
- }
-
- // delete all non-processed scheduled tasks (ones, that were deleted from unit configs)
- $sql = 'SELECT ' . $object->IDField . '
- FROM ' . $object->TableName . '
- WHERE (Type = ' . ScheduledTask::TYPE_SYSTEM . ') AND (' . $object->IDField . ' NOT IN (' . implode(',', $processed_ids) . '))';
- $delete_ids = $this->Conn->GetCol($sql);
-
- if ($delete_ids) {
- $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
- /* @var $temp_handler kTempTablesHandler */
-
- $temp_handler->DeleteItems($event->Prefix, $event->Special, $delete_ids);
- }
-
- $this->Application->removeObject($event->getPrefixSpecial());
- }
-
- /**
- * Don't allow to delete other user's messages
- *
- * @param kEvent $event
- * @param string $type
- * @return void
- * @access protected
- */
- protected function customProcessing(&$event, $type)
- {
- if ($event->Name == 'OnMassDelete' && $type == 'before') {
- if ($this->Application->isDebugMode()) {
- // allow to delete system scheduled tasks in debug mode
- return ;
- }
-
- $ids = $event->getEventParam('ids');
- if ($ids) {
- $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
- $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
-
- $sql = 'SELECT ' . $id_field . '
- FROM ' . $table_name . '
- WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND Type <> ' . ScheduledTask::TYPE_SYSTEM;
- $allowed_ids = $this->Conn->GetCol($sql);
-
- $event->setEventParam('ids', $allowed_ids);
- }
- }
- }
-
- /**
- * Cancels scheduled tasks, that are currently running
- *
- * @param kEvent $event
- */
- function OnMassCancel(&$event)
- {
- $ids = $this->StoreSelectedIDs($event);
-
- if ($ids) {
- $object =& $event->getObject( Array ('skip_autoload' => true) );
- /* @var $object kDBItem */
-
- foreach ($ids as $id) {
- $object->Load($id);
-
- if ($object->GetDBField('LastRunStatus') == ScheduledTask::LAST_RUN_RUNNING) {
- // only changes status, doesn't affect currency running scheduled tasks
- $object->SetDBField('LastRunStatus', ScheduledTask::LAST_RUN_FAILED);
- $object->Update();
- }
- }
- }
-
- $this->clearSelectedIDs($event);
- }
-
- /**
- * Runs selected scheduled tasks
- *
- * @param kEvent $event
- */
- function OnRun(&$event)
- {
- $ids = $this->StoreSelectedIDs($event);
-
- if ($ids) {
- $object =& $event->getObject( Array ('skip_autoload' => true) );
- /* @var $object kDBItem */
-
- $where_clause = Array (
- $object->TableName . '.' . $object->IDField . ' IN (' . implode(',', $ids) . ')',
- $object->TableName . '.Status = ' . STATUS_ACTIVE,
- $object->TableName . '.LastRunStatus <> ' . ScheduledTask::LAST_RUN_RUNNING,
- );
-
- $sql = $object->GetSelectSQL() . '
- WHERE (' . implode(') AND (', $where_clause) . ')';
- $scheduled_tasks = $this->Conn->Query($sql);
-
- foreach ($scheduled_tasks as $scheduled_task_data) {
- $scheduled_task_data['EventName'] = $scheduled_task_data['Event'];
- $this->Application->EventManager->runScheduledTask($scheduled_task_data);
- }
- }
-
- $this->clearSelectedIDs($event);
- }
- }
\ No newline at end of file
Index: branches/5.2.x/core/units/agents/agents_config.php
===================================================================
diff -u -N
--- branches/5.2.x/core/units/agents/agents_config.php (revision 14879)
+++ branches/5.2.x/core/units/agents/agents_config.php (revision 0)
@@ -1,174 +0,0 @@
- 'scheduled-task',
- 'ItemClass' => Array ('class' => 'kDBItem', 'file' => '', 'build_event' => 'OnItemBuild'),
- 'ListClass' => Array ('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'),
- 'EventHandlerClass' => Array ('class' => 'ScheduledTaskEventHandler', 'file' => 'agent_eh.php', 'build_event' => 'OnBuild'),
- 'TagProcessorClass' => Array ('class' => 'kDBTagProcessor', 'file' => '', 'build_event' => 'OnBuild'),
-
- 'AutoLoad' => true,
-
- 'QueryString' => Array (
- 1 => 'id',
- 2 => 'Page',
- 3 => 'PerPage',
- 4 => 'event',
- 5 => 'mode',
- ),
-
- 'Hooks' => Array (
- Array (
- 'Mode' => hAFTER,
- 'Conditional' => false,
- 'HookToPrefix' => 'adm',
- 'HookToSpecial' => '*',
- 'HookToEvent' => Array ('OnAfterCacheRebuild'),
- 'DoPrefix' => '',
- 'DoSpecial' => '*',
- 'DoEvent' => 'OnRefresh',
- ),
- ),
-
- 'IDField' => 'ScheduledTaskId',
-
- 'TableName' => TABLE_PREFIX . 'ScheduledTasks',
-
- 'TitleField' => 'Name',
-
- 'StatusField' => Array ('Status'),
-
- 'TitlePresets' => Array (
- 'default' => Array (
- 'new_status_labels' => Array ('scheduled-task' => '!la_title_AddingScheduledTask!'),
- 'edit_status_labels' => Array ('scheduled-task' => '!la_title_EditingScheduledTask!'),
- 'new_titlefield' => Array ('scheduled-task' => '!la_title_NewScheduledTask!'),
- ),
-
- 'scheduled_task_list' => Array (
- 'prefixes' => Array ('scheduled-task_List'), 'format' => "!la_title_ScheduledTasks!",
- 'toolbar_buttons' => Array ('new_item', 'edit', 'delete', 'approve', 'decline', 'process', 'cancel', 'view', 'dbl-click'),
- ),
-
- 'scheduled_task_edit' => Array (
- 'prefixes' => Array ('scheduled-task'), 'format' => "#scheduled-task_status# '#scheduled-task_titlefield#'",
- 'toolbar_buttons' => Array ('select', 'cancel', 'reset_edit', 'prev', 'next'),
- ),
- ),
-
- 'PermSection' => Array('main' => 'in-portal:scheduled_tasks'),
-
- 'Sections' => Array (
- 'in-portal:scheduled_tasks' => Array (
- 'parent' => 'in-portal:website_setting_folder',
- 'icon' => 'conf_agents',
- 'label' => 'la_title_ScheduledTasks',
- 'url' => Array('t' => 'agents/agent_list', 'pass' => 'm'),
- 'permissions' => Array('view', 'add', 'edit', 'delete'),
- 'priority' => 6,
- 'type' => stTREE,
- ),
- ),
-
- 'ListSQLs' => Array (
- '' => ' SELECT %1$s.* %2$s FROM %1$s',
- ),
-
- 'ListSortings' => Array (
- '' => Array (
- 'Sorting' => Array ('Name' => 'asc'),
- )
- ),
-
- 'Fields' => Array (
- 'ScheduledTaskId' => Array ('type' => 'int', 'not_null' => 1, 'default' => 0),
-
- 'Name' => Array (
- 'type' => 'string', 'max_len' => 255,
- 'unique' => Array (),
- 'required' => 1, 'not_null' => 1, 'default' => ''
- ),
-
- 'Type' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_User', 2 => 'la_opt_System'), 'use_phrases' => 1,
- 'required' => 1, 'not_null' => 1, 'default' => 1
- ),
- 'Status' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Active', 0 => 'la_opt_Disabled'), 'use_phrases' => 1,
- 'required' => 1, 'not_null' => 1, 'default' => 1
- ),
- 'Event' => Array (
- 'type' => 'string', 'max_len' => 255,
- 'formatter' => 'kFormatter', 'regexp' => '/^[a-z-]*[.]{0,1}[a-z-]*:On[A-Za-z0-9]*$/',
- 'required' => 1, 'not_null' => 1, 'default' => ''
- ),
- 'RunInterval' => Array ('type' => 'int', 'required' => 1, 'not_null' => 1, 'default' => 0),
- 'RunMode' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter', 'options' => Array (reBEFORE => 'la_opt_Before', reAFTER => 'la_opt_After'), 'use_phrases' => 1,
- 'required' => 1, 'not_null' => 1, 'default' => 2
- ),
- 'LastRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
-
- 'LastRunStatus' => Array (
- 'type' => 'int',
- 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_opt_Success', 0 => 'la_opt_Failed', 2 => 'la_opt_Running'), 'use_phrases' => 1,
- 'not_null' => 1, 'default' => 1
- ),
-
- 'NextRunOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'required' => 1, 'default' => '#NOW#'),
- 'RunTime' => Array ('type' => 'int', 'not_null' => 1, 'default' => 0),
- 'Timeout' => Array (
- 'type' => 'int',
- 'min_value_inc' => 1,
- 'not_null' => 1, 'default' => NULL
- ),
- 'LastTimeoutOn' => Array ('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => NULL),
- 'SiteDomainLimitation' => Array (
- 'type' => 'string', 'max_len' => 255,
- 'formatter' => 'kOptionsFormatter', 'options_sql' => 'SELECT %s FROM ' . TABLE_PREFIX . 'SiteDomains ORDER BY DomainName ASC', 'option_key_field' => 'DomainId', 'option_title_field' => 'DomainName', 'multiple' => 1,
- 'not_null' => 1, 'default' => ''
- ),
- ),
-
- 'Grids' => Array (
- 'Default' => Array (
- 'Icons' => Array (
- 'default' => 'icon16_item.png',
- 0 => 'icon16_disabled.png',
- ),
- 'Fields' => Array (
- 'ScheduledTaskId' => Array ('title' => 'column:la_fld_Id', 'data_block' => 'grid_checkbox_td', 'filter_block' => 'grid_range_filter', 'width' => 50, ),
- 'Name' => Array ('filter_block' => 'grid_like_filter', 'width' => 200, ),
- 'Type' => Array ('filter_block' => 'grid_options_filter', 'width' => 60, ),
- 'Event' => Array ('filter_block' => 'grid_like_filter', 'width' => 280, ),
- 'RunInterval' => Array ('filter_block' => 'grid_range_filter', 'width' => 100, ),
- 'RunMode' => Array ('filter_block' => 'grid_options_filter', 'width' => 85, ),
- 'LastRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
- 'RunTime' => Array ('filter_block' => 'grid_range_filter', 'width' => 145, ),
- 'LastRunStatus' => Array ('filter_block' => 'grid_options_filter', 'width' => 120, ),
- 'NextRunOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
- 'Status' => Array ('filter_block' => 'grid_options_filter', 'width' => 65, ),
- 'Timeout' => Array ('filter_block' => 'grid_range_filter', 'width' => 85, ),
- 'LastTimeoutOn' => Array ('filter_block' => 'grid_date_range_filter', 'width' => 145, ),
- 'SiteDomainLimitation' => Array ('data_block' => 'grid_picker_td', 'filter_block' => 'grid_multioptions_filter', 'separator' => ', ', 'width' => 145),
- ),
- ),
- ),
- );
\ No newline at end of file