...
 
Commits (3)
<?php
namespace Minds\Controllers\Cli;
use DateTime;
use Elasticsearch\ClientBuilder;
use Minds\Cli;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Entities;
use Minds\Helpers\Flags;
use Minds\Interfaces;
use Minds\Core\Rewards\Contributions\UsersIterator;
class BoostCampaigns extends Cli\Controller implements Interfaces\CliControllerInterface
{
public function help($command = null)
{
$this->out('Syntax usage: cli trending <type>');
}
public function exec()
{
}
public function start()
{
error_reporting(E_ALL);
ini_set('display_errors', 1);
$offset = $this->getOpt('offset') ?? null;
$type = $this->getOpt('type') ?? 'newsfeed';
$ownerGuid = $this->getOpt('ownerGuid') ?? null;
/** @var Core\Boost\Campaigns\Manager $manager */
$manager = Di::_()->get('Boost\Campaigns\Manager');
$iterator = (new Core\Boost\Campaigns\Iterator())
->setOffset($offset)
->setState(Core\Boost\Campaigns\Campaign::CREATED_STATUS)
->setOwnerGuid($ownerGuid)
->setType($type);
foreach ($iterator as $campaign) {
try {
$manager->start($campaign);
} catch (\Exception $e) {
error_log(get_class($e) . ': ' . $e->getMessage());
continue;
}
}
}
}
<?php
/**
* @author: eiennohi.
*/
namespace Minds\Core\Boost\Campaigns;
use Minds\Core\Di\Di;
class Iterator implements \Iterator
{
/** @var Manager */
protected $manager;
protected $limit = 12;
protected $from = 0;
protected $offset = null;
protected $sort = 'asc';
protected $type = 'newsfeed';
protected $ownerGuid = null;
protected $state = null;
/** @var array */
protected $list = null;
public function __construct(
$manager = null
)
{
$this->manager = $manager ?: Di::_()->get('Boost\Campaigns\Manager');
}
/**
* @param int $limit
* @return Iterator
*/
public function setLimit(int $limit)
{
$this->limit = $limit;
return $this;
}
/**
* @param int $from
* @return Iterator
*/
public function setFrom(int $from)
{
$this->from = $from;
return $this;
}
/**
* @return int|null
*/
public function getOffset()
{
return $this->offset;
}
/**
* @param int|null $offset
* @return Iterator
*/
public function setOffset($offset)
{
$this->offset = $offset;
return $this;
}
/**
* @param string $sort
* @return Iterator
*/
public function setSort(string $sort)
{
$this->sort = $sort;
return $this;
}
/**
* @param mixed $type
* @return Iterator
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @param null $ownerGuid
* @return Iterator
*/
public function setOwnerGuid($ownerGuid)
{
$this->ownerGuid = $ownerGuid;
return $this;
}
/**
* @param mixed $state
* @return Iterator
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
public function getList()
{
$response = $this->manager->getList([
'limit' => $this->limit,
'from' => $this->from,
'offset' => $this->offset,
'type' => $this->type,
'owner_guid' => $this->ownerGuid,
'state' => $this->state,
]);
$this->offset = $response->getPagingToken();
$this->list = $response;
}
public function current()
{
return current($this->list);
}
public function next()
{
next($this->list);
}
public function key()
{
return key($this->list);
}
public function valid()
{
if (!$this->list) {
return false;
}
return key($this->list) !== null;
}
public function rewind()
{
if ($this->list) {
reset($this->list);
}
$this->getList();
}
}
......@@ -13,10 +13,10 @@ use Minds\Entities\User;
class Manager
{
/** @var Repository */
/** @var Repository */
protected $repository;
/** @var ElasticRepository */
/** @var ElasticRepository */
protected $elasticRepository;
/** @var Delegates\CampaignUrnDelegate */
......@@ -387,19 +387,23 @@ class Manager
throw new CampaignException('Campaign should be in [approved] state in order to complete it');
}
// Update
// Check for completion
$campaign
->setCompletedTimestamp(time() * 1000);
if (time() * 1000 >= $campaign->getEnd() || $campaign->getImpressionsMet() >= $campaign->getImpressions()) {
// Update
// Write
$campaign
->setCompletedTimestamp(time() * 1000);
$this->repository->update($campaign);
$this->elasticRepository->update($campaign);
// Write
// Budget update
$this->repository->update($campaign);
$this->elasticRepository->update($campaign);
$this->budgetDelegate->onStateChange($campaign);
// Budget update
$this->budgetDelegate->onStateChange($campaign);
}
return $campaign;
}
......
<?php
namespace Minds\Core\Queue\Runners;
use Minds\Core\Boost\Campaigns\Manager;
use Minds\Core\Queue;
use Minds\Core\Queue\Interfaces;
use Minds\Core\Di\Di;
class BoostCampaignChecker implements Interfaces\QueueRunner
{
public function run()
{
$client = Queue\Client::Build();
$client->setQueue("BoostCampaignChecker")
->receive(function ($msg) {
$data = $msg->getData();
$campaignRef = $data['campaignRef'];
echo "Checking boost campaign {$campaignRef->getUrn()}\n";
/** @var Manager $manager */
$manager = Di::_()->get('Boost\Campaigns\Manager');
$manager->complete($campaignRef);
});
}
}
......@@ -157,3 +157,13 @@ numprocs=4
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
[program:minds-boost-campaing-checker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/Minds/engine/cli.php QueueRunner run --runner=BoostCampaignChecker
autostart=true
autorestart=true
numprocs=2
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0