2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
require_once __DIR__.'/../init.php';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-29 08:45:36 +12:00
|
|
|
use Utopia\App;
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Database\Database;
|
|
|
|
use Appwrite\Database\Validator\Authorization;
|
2019-05-09 18:54:39 +12:00
|
|
|
use Cron\CronExpression;
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\cli_set_process_title('Tasks V1 Worker');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
echo APP_NAME.' tasks worker v1 has started';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
class TasksV1
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $args = [];
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function perform()
|
|
|
|
{
|
2020-06-29 08:45:36 +12:00
|
|
|
global $consoleDB;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
/*
|
2019-05-09 18:54:39 +12:00
|
|
|
* 1. Get Original Task
|
|
|
|
* 2. Check for updates
|
|
|
|
* If has updates skip task and don't reschedule
|
|
|
|
* If status not equal to play skip task
|
|
|
|
* 3. Check next run date, update task and add new job at the given date
|
|
|
|
* 4. Execute task (set optional timeout)
|
|
|
|
* 5. Update task response to log
|
|
|
|
* On success reset error count
|
|
|
|
* On failure add error count
|
|
|
|
* If error count bigger than allowed change status to pause
|
|
|
|
*/
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
$taskId = (isset($this->args['$id'])) ? $this->args['$id'] : null;
|
2019-09-07 05:08:48 +12:00
|
|
|
$updated = (isset($this->args['updated'])) ? $this->args['updated'] : null;
|
|
|
|
$next = (isset($this->args['next'])) ? $this->args['next'] : null;
|
2020-06-20 23:20:49 +12:00
|
|
|
$delay = \time() - $next;
|
2019-09-07 05:08:48 +12:00
|
|
|
$errors = [];
|
|
|
|
$timeout = 60 * 5; // 5 minutes
|
2019-11-26 09:25:54 +13:00
|
|
|
$errorLimit = 5;
|
|
|
|
$logLimit = 5;
|
|
|
|
$alert = '';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (empty($taskId)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new Exception('Missing task $id');
|
|
|
|
}
|
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
|
|
|
|
$task = $consoleDB->getDocument($taskId);
|
|
|
|
|
2020-07-03 17:57:41 +12:00
|
|
|
Authorization::reset();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
if (\is_null($task->getId()) || Database::SYSTEM_COLLECTION_TASKS !== $task->getCollection()) {
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new Exception('Task Not Found');
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if ($task->getAttribute('updated') !== $updated) { // Task have already been rescheduled by owner
|
2019-05-09 18:54:39 +12:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if ($task->getAttribute('status') !== 'play') { // Skip task and don't schedule again
|
2019-05-09 18:54:39 +12:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reschedule
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
$cron = CronExpression::factory($task->getAttribute('schedule'));
|
|
|
|
$next = (int) $cron->getNextRunDate()->format('U');
|
2020-06-20 23:20:49 +12:00
|
|
|
$headers = (\is_array($task->getAttribute('httpHeaders', []))) ? $task->getAttribute('httpHeaders', []) : [];
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$task
|
|
|
|
->setAttribute('next', $next)
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('previous', \time())
|
2019-05-09 18:54:39 +12:00
|
|
|
;
|
|
|
|
|
2019-11-26 09:25:54 +13:00
|
|
|
ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy()); // Async task rescheduale
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$startTime = \microtime(true);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
// Execute Task
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$ch = \curl_init($task->getAttribute('httpUrl'));
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $task->getAttribute('httpMethod'));
|
|
|
|
\curl_setopt($ch, CURLOPT_POSTFIELDS, '');
|
|
|
|
\curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(APP_USERAGENT,
|
2020-06-30 23:09:28 +12:00
|
|
|
App::getEnv('_APP_VERSION', 'UNKNOWN'),
|
2020-06-29 08:45:36 +12:00
|
|
|
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
|
2020-03-02 06:05:51 +13:00
|
|
|
));
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_setopt(
|
2019-09-30 19:13:40 +13:00
|
|
|
$ch,
|
|
|
|
CURLOPT_HTTPHEADER,
|
2020-06-20 23:20:49 +12:00
|
|
|
\array_merge($headers, [
|
2020-02-17 20:16:11 +13:00
|
|
|
'X-'.APP_NAME.'-Task-ID: '.$task->getAttribute('$id', ''),
|
2019-09-07 05:08:48 +12:00
|
|
|
'X-'.APP_NAME.'-Task-Name: '.$task->getAttribute('name', ''),
|
2019-05-09 18:54:39 +12:00
|
|
|
])
|
|
|
|
);
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
|
|
|
|
\curl_setopt($ch, CURLOPT_NOBODY, true);
|
|
|
|
\curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (!$task->getAttribute('security', true)) {
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
$httpUser = $task->getAttribute('httpUser');
|
|
|
|
$httpPass = $task->getAttribute('httpPass');
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (!empty($httpUser) && !empty($httpPass)) {
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_USERPWD, "$httpUser:$httpPass");
|
|
|
|
\curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$response = \curl_exec($ch);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (false === $response) {
|
2020-06-20 23:20:49 +12:00
|
|
|
$errors[] = \curl_error($ch).'Failed to execute task';
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$code = \curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
$codeFamily = \mb_substr($code, 0, 1);
|
|
|
|
$headersSize = \curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
|
|
|
$headers = \substr($response, 0, $headersSize);
|
|
|
|
$body = \substr($response, $headersSize);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\curl_close($ch);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$totalTime = \round(\microtime(true) - $startTime, 2);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
switch ($codeFamily) {
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
break;
|
|
|
|
default:
|
2019-09-07 05:08:48 +12:00
|
|
|
$errors[] = 'Request failed with status code '.$code;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (empty($errors)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$task->setAttribute('failures', 0);
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
$alert = 'Task "'.$task->getAttribute('name').'" Executed Successfully';
|
|
|
|
} else {
|
2019-05-09 18:54:39 +12:00
|
|
|
$task
|
|
|
|
->setAttribute('failures', $task->getAttribute('failures', 0) + 1)
|
2019-11-26 09:25:54 +13:00
|
|
|
->setAttribute('status', ($task->getAttribute('failures') >= $errorLimit) ? 'pause' : 'play')
|
2019-05-09 18:54:39 +12:00
|
|
|
;
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$alert = 'Task "'.$task->getAttribute('name').'" failed to execute with the following errors: '.\implode("\n", $errors);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
$log = \json_decode($task->getAttribute('log', '{}'), true);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
if (\count($log) >= $logLimit) {
|
|
|
|
\array_pop($log);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\array_unshift($log, [
|
2019-05-09 18:54:39 +12:00
|
|
|
'code' => $code,
|
|
|
|
'duration' => $totalTime,
|
|
|
|
'delay' => $delay,
|
|
|
|
'errors' => $errors,
|
|
|
|
'headers' => $headers,
|
|
|
|
'body' => $body,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$task
|
2020-06-20 23:20:49 +12:00
|
|
|
->setAttribute('log', \json_encode($log))
|
2019-05-09 18:54:39 +12:00
|
|
|
->setAttribute('duration', $totalTime)
|
|
|
|
->setAttribute('delay', $delay)
|
|
|
|
;
|
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
if (false === $consoleDB->updateDocument($task->getArrayCopy())) {
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new Exception('Failed saving tasks to DB');
|
|
|
|
}
|
|
|
|
|
2020-07-03 17:57:41 +12:00
|
|
|
Authorization::reset();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-11-26 09:25:54 +13:00
|
|
|
// ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy()); // Sync task rescheduale
|
|
|
|
|
|
|
|
// Send alert if needed (use SMTP as default for now)
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
// ... Remove environment for this job
|
|
|
|
}
|
2019-09-07 05:08:48 +12:00
|
|
|
}
|