Skip to content
Browse files

[issue #132] Convert action=wipe into Action/Page class, renamed to "…

…cleanup"

- Just like a few others, this should be an Api only, so we use the same
  page-wrapper with try-catch as GetrunPage etc.

- Remove cronjob.txt

- Update README
  • Loading branch information...
1 parent d16fd64 commit 076edf3bd334ea4243ec0d3c9c7b312d3e261a5b @Krinkle Krinkle committed Mar 31, 2012
Showing with 125 additions and 67 deletions.
  1. +4 −3 README.md
  2. +0 −1 config/cronjob.txt
  3. +77 −0 inc/actions/CleanupAction.php
  4. +0 −61 inc/actions/wipe.php
  5. +2 −0 inc/init.php
  6. +40 −0 inc/pages/CleanupPage.php
  7. +1 −1 inc/pages/GetrunPage.php
  8. +1 −1 inc/pages/SaverunPage.php
View
7 README.md
@@ -67,8 +67,9 @@ TestSwarm only supports Apache and MySQL as servers.
4. Copy the ./config/.htaccess-sample to ./htaccess. If needed change the
RewriteBase to match the contextpath configuration.
-5. Load the cronjob (changing the URL to point to your site).
- `crontab << config/cronjob.txt`
+5. Create an entry to your crontab for action=cleanup. This performs various
+ cleaning duties such as making timed-out runs available again for testing.
+ `* * * * * curl -s http://path/to/testswarm/index.php?action=cleanup > /dev/null`
6. Currently the server must be run in Apache (it uses a .htaccess file).
@@ -87,7 +88,7 @@ TestSwarm only supports Apache and MySQL as servers.
Get involed
---------------------
-You're welcome to use the Github issue tracker to start discussions.
+You're welcome to use the GitHub issue tracker to start discussions.
There is also a mailing list at Google Groups available:
View
1 config/cronjob.txt
@@ -1 +0,0 @@
-* * * * * curl -s http://localhost/?state=wipe > /dev/null
View
77 inc/actions/CleanupAction.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * "Cleanup" action (previously action=wipe)
+ *
+ * @since 0.1.0
+ * @package TestSwarm
+ */
+
+class CleanupAction extends Action {
+
+ public function doAction() {
+ $browserInfo = $this->getContext()->getBrowserInfo();
+ $db = $this->getContext()->getDB();
+ $request = $this->getContext()->getRequest();
+
+ // Reset runs that were given to a client,
+ // but haven't been active for over 5 minutes
+ $rows = $db->getRows(str_queryf(
+ "SELECT
+ run_id,
+ client_id,
+ useragent_id
+ FROM
+ run_client, clients
+ WHERE run_client.updated < %s
+ AND clients.id = client_id
+ AND run_client.status = 1;",
+ swarmdb_dateformat( strtotime( '5 minutes ago' ) )
+ ));
+
+ if ( $rows ) {
+ foreach ( $rows as $row ) {
+ // Undo run count
+ mysql_queryf(
+ "UPDATE
+ run_useragent
+ SET
+ runs = runs - 1
+ WHERE run_id = %u
+ AND useragent_id = %u;",
+ $row->row_id,
+ $row->useragent_id
+ );
+ // Remove run_client entry
+ mysql_queryf(
+ "DELETE FROM
+ run_client
+ WHERE run_id = %u
+ AND client_id = %u;",
+ $row->row_id,
+ $row->client_id
+ );
+ }
+ }
+
+ // Reset runs that race-condition deleted themselves
+ $db->query(
+ "UPDATE
+ run_useragent
+ SET
+ runs = 0,
+ completed = 0,
+ status = 0
+ WHERE runs = max
+ AND NOT EXISTS (
+ SELECT *
+ FROM run_client, clients
+ WHERE run_client.run_id = run_useragent.run_id
+ AND run_client.client_id = clients.id
+ AND clients.useragent_id = run_useragent.useragent_id
+ );"
+ );
+
+ $this->setData( "ok" );
+ }
+}
+
View
61 inc/actions/wipe.php
@@ -1,61 +0,0 @@
-<?php
-
- $result = mysql_queryf(
- "SELECT
- run_id,
- client_id,
- useragent_id
- FROM
- run_client, clients
- WHERE run_client.updated < %s
- AND clients.id = client_id
- AND run_client.status = 1;",
- swarmdb_dateformat( strtotime( '5 minutes ago' ) )
- );
-
- while ( $row = mysql_fetch_array($result) ) {
- $run_id = $row[0];
- $client_id = $row[1];
- $useragent_id = $row[2];
-
- # Update run_useragent (clients, useragents)
- mysql_queryf(
- "UPDATE
- run_useragent
- SET
- runs = runs - 1
- WHERE run_id = %u
- AND useragent_id = %u;",
- $run_id,
- $useragent_id
- );
- mysql_queryf(
- "DELETE FROM
- run_client
- WHERE run_id = %u
- AND client_id = %u;",
- $run_id,
- $client_id
- );
- }
-
- # Reset runs that race-condition deleted themselves
- mysql_queryf(
- "UPDATE
- run_useragent
- SET
- runs = 0,
- completed = 0,
- status = 0
- WHERE runs = max
- AND NOT EXISTS (
- SELECT *
- FROM run_client, clients
- WHERE run_client.run_id = run_useragent.run_id
- AND run_client.client_id = clients.id
- AND clients.useragent_id = run_useragent.useragent_id
- );"
- );
-
- echo "done";
- exit;
View
2 inc/init.php
@@ -108,13 +108,15 @@
"TestSwarmContext" => "inc/TestSwarm.php",
"WebRequest" => "inc/WebRequest.php",
# Actions
+ "CleanupAction" => "inc/actions/CleanupAction.php",
"GetrunAction" => "inc/actions/GetrunAction.php",
"LoginAction" => "inc/actions/LoginAction.php",
"LogoutAction" => "inc/actions/LogoutAction.php",
"SaverunAction" => "inc/actions/SaverunAction.php",
"ScoresAction" => "inc/actions/ScoresAction.php",
"SignupAction" => "inc/actions/SignupAction.php",
# Pages
+ "CleanupPage" => "inc/pages/CleanupPage.php", // @todo: rm Page, add Api
"GetrunPage" => "inc/pages/GetrunPage.php", // @todo: rm Page, add Api
"HomePage" => "inc/pages/HomePage.php",
"JobPage" => "inc/pages/JobPage.php",
View
40 inc/pages/CleanupPage.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * "Cleanup" page.
+ *
+ * @since 0.1.0
+ * @package TestSwarm
+ */
+class CleanupPage extends Page {
+
+ public function execute() {
+ $action = CleanupAction::newFromContext( $this->getContext() );
+
+ try {
+ $action->doAction();
+
+ if ( $action->getError() ) {
+ $response = array(
+ "error" => $action->getError(),
+ );
+ } else {
+ $response = $action->getData();
+ }
+
+ } catch ( Exception $e ) {
+ $response = array(
+ "error" => array(
+ "code" => "internal-error",
+ "info" => "An internal error occurred. Action could not be performed. Error message:\n" . $e->getMessage(),
+ ),
+ );
+ }
+
+ // This should really be in the API,
+ // but until we have an actual API, we do it as a Page
+ echo json_encode( $response );
+ exit;
+ #$this->setAction( $action );
+ #$this->content = $this->initContent();
+ }
+}
View
2 inc/pages/GetrunPage.php
@@ -9,7 +9,7 @@ class GetrunPage extends Page {
public function execute() {
$action = GetrunAction::newFromContext( $this->getContext() );
-
+
try {
$action->doAction();
View
2 inc/pages/SaverunPage.php
@@ -9,7 +9,7 @@ class SaverunPage extends Page {
public function execute() {
$action = SaverunAction::newFromContext( $this->getContext() );
-
+
try {
$action->doAction();

2 comments on commit 076edf3

@Krinkle
jQuery Foundation member

$row->row_id, should be $row->run_id,, F&R failure. Fixed locally,

@Krinkle
jQuery Foundation member

Fix committed in ca6abd4

Please sign in to comment.
Something went wrong with that request. Please try again.