2022-04-18 08:34:32 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Event;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Resque;
|
|
|
|
use ResqueScheduler;
|
|
|
|
use Utopia\Database\Document;
|
|
|
|
|
|
|
|
class Func extends Event
|
|
|
|
{
|
|
|
|
protected string $jwt = '';
|
|
|
|
protected string $type = '';
|
|
|
|
protected string $data = '';
|
2022-04-19 04:21:45 +12:00
|
|
|
protected ?Document $function = null;
|
|
|
|
protected ?Document $execution = null;
|
2022-04-18 08:34:32 +12:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(Event::FUNCTIONS_QUEUE_NAME, Event::FUNCTIONS_CLASS_NAME);
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Sets function document for the function event.
|
|
|
|
*
|
|
|
|
* @param \Utopia\Database\Document $function
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function setFunction(Document $function): self
|
|
|
|
{
|
|
|
|
$this->function = $function;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Returns set function document for the function event.
|
|
|
|
*
|
|
|
|
* @return null|\Utopia\Database\Document
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function getFunction(): ?Document
|
|
|
|
{
|
|
|
|
return $this->function;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Sets execution for the function event.
|
|
|
|
*
|
|
|
|
* @param \Utopia\Database\Document $execution
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function setExecution(Document $execution): self
|
|
|
|
{
|
|
|
|
$this->execution = $execution;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Returns set execution for the function event.
|
|
|
|
*
|
|
|
|
* @return null|\Utopia\Database\Document
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function getExecution(): ?Document
|
|
|
|
{
|
|
|
|
return $this->execution;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Sets type for the function event.
|
|
|
|
*
|
|
|
|
* @param string $type Can be `schedule`, `event` or `http`.
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function setType(string $type): self
|
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Returns set type for the function event.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Sets custom data for the function event.
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function setData(string $data): self
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Returns set custom data for the function event.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function getData(): string
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Sets JWT for the function event.
|
|
|
|
*
|
|
|
|
* @param string $jwt
|
|
|
|
* @return self
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function setJWT(string $jwt): self
|
|
|
|
{
|
|
|
|
$this->jwt = $jwt;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Returns set JWT for the function event.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function getJWT(): string
|
|
|
|
{
|
|
|
|
return $this->jwt;
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Executes the function event and sends it to the functions worker.
|
|
|
|
*
|
|
|
|
* @return string|bool
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function trigger(): string|bool
|
|
|
|
{
|
|
|
|
return Resque::enqueue($this->queue, $this->class, [
|
|
|
|
'project' => $this->project,
|
|
|
|
'user' => $this->user,
|
|
|
|
'function' => $this->function,
|
|
|
|
'execution' => $this->execution,
|
|
|
|
'type' => $this->type,
|
|
|
|
'jwt' => $this->jwt,
|
|
|
|
'payload' => $this->payload,
|
|
|
|
'data' => $this->data
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
/**
|
|
|
|
* Schedules the function event and schedules it in the functions worker queue.
|
|
|
|
*
|
|
|
|
* @param \DateTime|int $at
|
|
|
|
* @return void
|
|
|
|
* @throws \Resque_Exception
|
|
|
|
* @throws \ResqueScheduler_InvalidTimestampException
|
|
|
|
*/
|
2022-04-18 08:34:32 +12:00
|
|
|
public function schedule(DateTime|int $at): void
|
|
|
|
{
|
|
|
|
ResqueScheduler::enqueueAt($at, $this->queue, $this->class, [
|
|
|
|
'project' => $this->project,
|
|
|
|
'user' => $this->user,
|
|
|
|
'function' => $this->function,
|
|
|
|
'execution' => $this->execution,
|
|
|
|
'type' => $this->type,
|
|
|
|
'payload' => $this->payload,
|
|
|
|
'data' => $this->data
|
|
|
|
]);
|
|
|
|
}
|
2022-04-19 04:21:45 +12:00
|
|
|
}
|