Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

class Events
{
/**
* @var string mutex to acquire
*/
const MUTEX_ID = 'rss-cron';

/**
* Defines what to do if admin menu is initialized.
Expand Down Expand Up @@ -40,6 +44,11 @@ public static function onAdminMenuInit($event)
*/
public static function onCron($event)
{
if (! Yii::$app->mutex->acquire(static::MUTEX_ID)) {
Console::stdout("RSS cron execution skipped - already running!\n");
return;
}

try {
Console::stdout("Updating RSS news feeds...\n");
$ccmsEnabled = ContentContainerModuleState::find()->
Expand All @@ -62,6 +71,8 @@ public static function onCron($event)
$event->sender->stderr($e->getMessage()."\n");
Yii::error($e);
}

Yii::$app->mutex->release(static::MUTEX_ID);
}

}
14 changes: 14 additions & 0 deletions jobs/GetFeedUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace sij\humhub\modules\rss\jobs;

use Yii;
use yii\helpers\Console;

use humhub\modules\queue\ActiveJob;
use humhub\modules\post\models\Post;
Expand Down Expand Up @@ -49,6 +50,11 @@ class GetFeedUpdates extends ActiveJob
private $newest; # newest date we are accepting
private $items; # array of sij\humhub\modules\rss\components\RssElement keyed by pubDate

/**
* @var string mutex to acquire
*/
const MUTEX_ID = 'rss-queue';

private function log($message) {
if ( $this->logFileHandle ) {
fwrite($this->logFileHandle, $message);
Expand Down Expand Up @@ -102,13 +108,15 @@ private function postMessage($message, $datePublished = false)
if ( count($oldContent) == 1 ) {
$post = Post::findOne($oldContent[0]->object_id);
$this->log("\n\n### update Post\n");
Console::stdout("RSS queue: updating post... ");
}
}

// if no previous version of the post, create a new one
if ( $post === null ) {
$post = new Post($this->space);
$this->log("\n\n### new Post\n");
Console::stdout("RSS queue: creating new post... ");
}

$post->created_by =
Expand Down Expand Up @@ -144,6 +152,7 @@ private function postMessage($message, $datePublished = false)
->query();
}

Console::stdout(Console::renderColoredString("%gdone.%n\n", 1));
}

/**
Expand Down Expand Up @@ -444,6 +453,10 @@ private function downloadNewsFeed()
*/
public function run()
{
if (! Yii::$app->mutex->acquire(static::MUTEX_ID)) {
Console::stdout("RSS queue execution skipped - already running!\n");
return;
}

####### $this->logFileHandle = fopen(dirname(__FILE__) . '/log.txt', 'w');

Expand Down Expand Up @@ -478,5 +491,6 @@ public function run()
fclose($this->logFileHandle);
}

Yii::$app->mutex->release(static::MUTEX_ID);
}
}