Skip to content

Commit

Permalink
Use Pusher to update tests and status
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Aug 31, 2018
1 parent 4455dd1 commit bc92da3
Show file tree
Hide file tree
Showing 25 changed files with 9,665 additions and 549 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [1.0.0] - 2018-08-28
### Added
- Pusher broadcasting
- Ability to disble pooling
### Breaking Changes
- Add PUSHER_CLUSTER=<cluster name> to your .env file
- Update your root.yml according to config/tddd/root.yml, regarding the pooling key

## [0.9.9] - 2017-12-08
### Added
- Toggle enabled/disabled projects
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"guzzlehttp/guzzle": "~6.3",
"jolicode/jolinotif": "~1.2",
"doctrine/dbal": "~2.5",
"symfony/yaml": "~3.2"
"symfony/yaml": "~3.2",
"pusher/pusher-php-server": "~3.0"
},

"require-dev": {
Expand Down
44 changes: 43 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"bootstrap": "^4.0.0-beta",
"cross-env": "^5.0.1",
"csspin": "^1.1.4",
"jquery": "^3.1.1",
"laravel-mix": "^1.0",
"less": "^2.7.2",
"lodash": "^4.17.4",
"node-sass": "^4.5.3",
"popper.js": "^1.12.5",
"pusher-js": "^4.3.0",
"vue": "^2.1.10",
"vuex": "^2.4.0",
"webpack-livereload-plugin": "^0.11.0"
},
"dependencies": {
"csspin": "^1.1.4"
}
"dependencies": {}
}
16 changes: 15 additions & 1 deletion src/config/tddd/root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ names:
watcher: TDDD - Watcher
worker: TDDD - Worker
regex_file_matcher: "/([A-Za-z0-9\\/._-]+)(?::| on line )([1-9][0-9]*)/"
poll_interval: 300 # milliseconds
poll:
enabled: false
interval: 300 # milliseconds
tmp_dir: "/var/tmp/"
show_progress: false
cache:
event_timeout: 10 # seconds
instance: 'cache'
code:
path: "{{ config('tddd-base.user_home') }}/code"
coverage:
path: coverage # under /public
broadcasting:
enabled: true
pusher:
key: "{{ config('broadcasting.connections.pusher.key') }}"
secret: "{{ config('broadcasting.connections.pusher.secret') }}"
app_id: "{{ config('broadcasting.connections.pusher.app_id') }}"
cluster: "{{ env('PUSHER_CLUSTER') }}"
channel_name: "tddd"
cache:
enabled: true
instance: 'cache'
23 changes: 23 additions & 0 deletions src/package/Data/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@

namespace PragmaRX\Tddd\Package\Data\Models;

use PragmaRX\Tddd\Package\Support\Constants;
use PragmaRX\Tddd\Package\Events\DataUpdated;
use PragmaRX\Tddd\Package\Data\Repositories\Data as DataRepository;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent
{
private function broadcastDataUpdated()
{
if (app(DataRepository::class)->projectSha1HasChanged()) {
broadcast(new DataUpdated());
}
}

/**
* Save the model to the database.
*
* @param array $options
* @return bool
*/
public function save(array $options = [])
{
parent::save($options);

$this->broadcastDataUpdated();
}

/**
* Get the connection of the entity.
*
Expand Down
2 changes: 0 additions & 2 deletions src/package/Data/Repositories/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ public function getJavascriptClientData()

'test_id' => request()->get('test_id'),

'poll_interval' => config('tddd.root.poll_interval'),

'root' => config('tddd.root'),
];

Expand Down
51 changes: 45 additions & 6 deletions src/package/Data/Repositories/Support/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PragmaRX\Tddd\Package\Data\Models\Project;
use PragmaRX\Tddd\Package\Data\Models\Test;
use PragmaRX\Tddd\Package\Support\Constants;

trait Projects
{
Expand Down Expand Up @@ -54,8 +55,6 @@ public function deleteMissingProjects($projects)
*/
public function getProjectTests($project_id = null)
{
$tests = [];

$order = "(case
when state = 'running' then 1
when state = 'failed' then 2
Expand All @@ -74,11 +73,23 @@ public function getProjectTests($project_id = null)
$query->where('project_id', $project_id);
}

foreach ($query->get() as $test) {
$tests[] = $this->getTestInfo($test);
}
return collect($query->get())->map(function ($test) {
return $this->getTestInfo($test);
});
}

return collect($tests);
/**
* Get all projects.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getProjectsAndCacheResult()
{
$projects = $this->getProjects();

app('tddd.cache')->put(Constants::CACHE_PROJECTS_KEY, sha1($projects->toJson()));

return $projects;
}

/**
Expand All @@ -97,6 +108,34 @@ public function getProjects()
});
}

/**
* Get a SHA1 for all projects.
*
* @return boolean
*/
public function projectSha1HasChanged()
{
$currentSha1 = sha1($projectsJson = $this->getProjects()->toJson());

$oldSha1 = app('tddd.cache')->get(Constants::CACHE_PROJECTS_KEY);

if ($hasChanged = $currentSha1 != $oldSha1) {
app('tddd.cache')->put(Constants::CACHE_PROJECTS_KEY, sha1($projectsJson));
}

return $hasChanged;
}

/**
* Get a SHA1 for all projects.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getProjectsSha1()
{
return sha1($this->getProjects()->toJson());
}

/**
* The the project state.
*
Expand Down
9 changes: 9 additions & 0 deletions src/package/Data/Repositories/Support/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@ public function addTestToQueue($test, $force = false)

QueueModel::updateOrCreate(['test_id' => $test->id]);

// After queueing, if it's the only one, it may take the test and run it right away,
// so we must wait a little for it to happen
sleep(1);

// We then get a fresh model, which may have a different state now
$test = $test->fresh();

if ($force || !in_array($test->state, [Constants::STATE_RUNNING, Constants::STATE_QUEUED])) {
$test->state = Constants::STATE_QUEUED;

$test->timestamps = false;

$test->save();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/package/Data/Repositories/Support/Runs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PragmaRX\Tddd\Package\Data\Repositories\Support;

use Illuminate\Support\Facades\DB as Database;
use PragmaRX\Tddd\Package\Data\Models\Run;

trait Runs
Expand All @@ -12,7 +11,7 @@ trait Runs
*/
public function clearRuns()
{
Database::statement('delete from tddd_runs');
Run::truncate();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/package/Events/DataUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PragmaRX\Tddd\Package\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class DataUpdated implements ShouldBroadcast
{
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('tddd');
}

public function broadcastAs()
{
return 'tddd:data-updated';
}
}
2 changes: 1 addition & 1 deletion src/package/Http/Controllers/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function index()
public function data()
{
return $this->success([
'projects' => $this->dataRepository->getProjects(),
'projects' => $this->dataRepository->getProjectsAndCacheResult(),
]);
}
}
Loading

0 comments on commit bc92da3

Please sign in to comment.