/** * @param string $task_name * @param array $data * @return array|bool */ public function createTask($task_name, $data = array()) { if (!$task_name) { $this->errors[] = 'Can not to create task. Empty task name has been given.'; } //first of all needs to define recipient count $this->load->model('sale/customer'); $this->load->model('setting/store'); $store_info = $this->model_setting_store->getStore((int) $this->session->data['current_store_id']); if ($store_info) { $store_name = $store_info['store_name']; } else { $store_name = $this->config->get('store_name'); } //get URIs of recipients if ($data['protocol'] == 'email') { list($uris, $subscribers) = $this->_get_email_list($data); $task_controller = 'task/sale/contact/sendEmail'; //if message does not contains html-tags replace breaklines to <br> $decoded = html_entity_decode($data['message'], ENT_QUOTES, 'UTF-8'); if ($decoded == strip_tags($decoded)) { $data['message'] = nl2br($data['message']); } } elseif ($data['protocol'] == 'sms') { list($uris, $subscribers) = $this->_get_phone_list($data); $task_controller = 'task/sale/contact/sendSms'; } if (!$uris) { $this->errors[] = 'No recipients!'; return false; } //numbers of emails per task step $divider = 10; //timeout in seconds for one email send $time_per_send = 4; $steps_count = ceil(sizeof($uris) / $divider); $tm = new ATaskManager(); //create new task $task_id = $tm->addTask(array('name' => $task_name, 'starter' => 1, 'created_by' => $this->user->getId(), 'status' => 1, 'start_time' => date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'))), 'last_time_run' => '0000-00-00 00:00:00', 'progress' => '0', 'last_result' => '1', 'run_interval' => '0', 'max_execution_time' => sizeof($uris) * $time_per_send * 2)); if (!$task_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } $tm->updateTaskDetails($task_id, array('created_by' => $this->user->getId(), 'settings' => array('recipients_count' => sizeof($uris), 'sent' => 0))); //create steps for sending $k = 0; $sort_order = 1; while ($steps_count > 0) { $uri_list = array_slice($uris, $k, $divider); $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => $sort_order, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => $time_per_send * $divider * 2, 'controller' => $task_controller, 'settings' => array('to' => $uri_list, 'subject' => $data['subject'], 'message' => $data['message'], 'store_name' => $store_name, 'subscribers' => $subscribers))); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { // get eta in seconds $this->eta[$step_id] = $time_per_send * $divider; } $steps_count--; $k = $k + $divider; $sort_order++; } $task_details = $tm->getTaskById($task_id); if ($task_details) { foreach ($this->eta as $step_id => $eta) { $task_details['steps'][$step_id]['eta'] = $eta; //remove settings from output json array. We will take it from database on execution. $task_details['steps'][$step_id]['settings'] = array(); } return $task_details; } else { $this->errors[] = 'Can not to get task details for execution'; $this->errors = array_merge($this->errors, $tm->errors); return false; } }
/** * @param string $task_name * @param array $data * @return array|bool */ public function createBackupTask($task_name, $data = array()) { if (!$task_name) { $this->errors[] = 'Can not to create task. Empty task name given'; } //NOTE: remove temp backup dir before process to prevent progressive increment of directory date if some backup-steps will be failed $bkp = new ABackup('manual_backup'); $bkp->removeBackupDirectory(); unset($bkp); $tm = new ATaskManager(); //1. create new task $task_id = $tm->addTask(array('name' => $task_name, 'starter' => 1, 'created_by' => $this->user->getId(), 'status' => 1, 'start_time' => date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'))), 'last_time_run' => '0000-00-00 00:00:00', 'progress' => '0', 'last_result' => '0', 'run_interval' => '0', 'max_execution_time' => '0')); if (!$task_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } //create step for table backup if ($data['table_list']) { //calculate estimate time for dumping of tables // get sizes of tables $table_list = array(); foreach ($data['table_list'] as $table) { if (!is_string($table)) { continue; } // clean $table_list[] = $this->db->escape($table); } $sql = "SELECT SUM(data_length + index_length - data_free) AS 'db_size'\n\t\t\t\t\tFROM information_schema.TABLES\n\t\t\t\t\tWHERE information_schema.TABLES.table_schema = '" . DB_DATABASE . "'\n\t\t\t\t\t\tAND TABLE_NAME IN ('" . implode("','", $data['table_list']) . "')\t"; if ($prefix_len) { $sql .= " AND TABLE_NAME like '" . DB_PREFIX . "%'"; } $result = $this->db->query($sql); $db_size = $result->row['db_size']; //size in bytes $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => 1, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => ceil($db_size / 2794843) * 4, 'controller' => 'task/tool/backup/dumptables', 'settings' => array('table_list' => $data['table_list'], 'sql_dump_mode' => $data['sql_dump_mode']))); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { // get eta in seconds. 2794843 - "bytes per seconds" of dumping for Pentium(R) Dual-Core CPU E5200 @ 2.50GHz × 2 $this->eta[$step_id] = ceil($db_size / 2794843) * 4; $this->est_backup_size += ceil($db_size * 1.61); // size of sql-file of output } } //create step for content-files backup if ($data['backup_code']) { //calculate estimate time for copying of code $dirs_size = $this->getCodeSize(); $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => 2, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => ceil($dirs_size / 28468838), 'controller' => 'task/tool/backup/backupCodeFiles', 'settings' => array('interrupt_on_step_fault' => false))); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { //// get eta in seconds. 28468838 - "bytes per seconds" of copiing of files for SATA III hdd $this->eta[$step_id] = ceil($dirs_size / 28468838); $this->est_backup_size += $dirs_size; } } //create step for content-files backup if ($data['backup_content']) { //calculate estimate time for copying of content files $dirs_size = $this->getContentSize(); $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => 3, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => ceil($dirs_size / 28468838), 'controller' => 'task/tool/backup/backupContentFiles', 'settings' => array('interrupt_on_step_fault' => false))); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { //// get eta in seconds. 28468838 - "bytes per seconds" of copiing of files for SATA III hdd $this->eta[$step_id] = ceil($dirs_size / 28468838); $this->est_backup_size += $dirs_size; } } //create last step for compressing backup if ($data['compress_backup']) { $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => 4, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => ceil($this->est_backup_size / 18874368), 'controller' => 'task/tool/backup/compressbackup')); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { //// get eta in seconds. 18874368 - "bytes per seconds" of gz-compression, level 1 on // AMD mobile Athlon XP2400+ 512 MB RAM Linux 2.6.12-rc4 gzip 1.3.3 $this->eta[$step_id] = ceil($this->est_backup_size / 18874368); } } $task_details = $tm->getTaskById($task_id); if ($task_details) { foreach ($this->eta as $step_id => $eta) { $task_details['steps'][$step_id]['eta'] = $eta; } return $task_details; } else { $this->errors[] = 'Can not to get task details for execution'; $this->errors = array_merge($this->errors, $tm->errors); return false; } }