-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a proper job manager for deleting transients etc. (#2802)
* #2801 add a job manager * #2801 run queue manager * schedule delete with job manager #2801 * Remove var_dump #2801 * mild cleanup for starting the runner #2801 * add queue run endpoint #2801 * rest api route for running queue now checked with CF PRo api keys #2801 * #2801 more tests for the run queue endpoint and fixed its response * Resolve Conflicts #2802 * Resolve conflicts #2802 * Resolve conflicts #2802
- Loading branch information
Showing
411 changed files
with
34,853 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Jobs; | ||
|
||
|
||
class DatabaseConnection extends \WP_Queue\Connections\DatabaseConnection | ||
{ | ||
|
||
const QUEUED_JOBS_TABLE = 'cf_queue_jobs'; | ||
const FAILED_JOBS_TABLE = 'cf_queue_failures'; | ||
/** | ||
* DatabaseQueue constructor. | ||
* | ||
* Creates database connection with cf prefixed tables | ||
* | ||
* @param \wpdb $wpdb | ||
* | ||
* @since 1.8.0 | ||
*/ | ||
public function __construct( $wpdb ) { | ||
|
||
$this->database = $wpdb; | ||
$this->jobs_table = $this->database->prefix . static::QUEUED_JOBS_TABLE; | ||
$this->failures_table = $this->database->prefix . static::FAILED_JOBS_TABLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Jobs; | ||
|
||
|
||
class DeleteTransientJob extends Job | ||
{ | ||
|
||
protected $transientId; | ||
public function __construct($transientId) | ||
{ | ||
$this->transientId = $transientId; | ||
} | ||
|
||
|
||
public function handle() | ||
{ | ||
\Caldera_Forms_Transient::delete_transient($this->transientId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Jobs; | ||
|
||
|
||
abstract class Job extends \WP_Queue\Job | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Jobs; | ||
|
||
|
||
use WP_Queue\Queue; | ||
|
||
class Scheduler | ||
{ | ||
/** | ||
* Queue manager | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @var Queue | ||
*/ | ||
protected $queue; | ||
|
||
/** | ||
* Scheduler constructor. | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param Queue $queue | ||
*/ | ||
public function __construct(Queue $queue) | ||
{ | ||
$this->queue = $queue; | ||
} | ||
|
||
/** | ||
* Get the queue worker | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @return \WP_Queue\Worker | ||
*/ | ||
public function getWorker(){ | ||
return $this->queue->worker(2); | ||
} | ||
|
||
/** | ||
* Schedule a job to run | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param Job $job Job to schedule. | ||
* @param int $delay Optional. Delay in seconds until job runs. Default is 0. | ||
*/ | ||
public function schedule(Job $job, $delay = 0){ | ||
$this->queue->push($job, $delay ); | ||
} | ||
|
||
/** | ||
* Run the queue with WP_Cron | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @return bool | ||
*/ | ||
public function runWithCron() | ||
{ | ||
return $this->queue->cron(2)->init(); | ||
} | ||
|
||
/** | ||
* Run jobs independently of WP_Cron | ||
* | ||
* @since 1.8.0 | ||
* | ||
* @param int $numberOfJobs | ||
* | ||
* @return int | ||
*/ | ||
public function runJobs($numberOfJobs = 5){ | ||
$worker = $this->getWorker(); | ||
$jobsRan = 0; | ||
for( $totalJobs = 0; $totalJobs < $numberOfJobs; $totalJobs++ ){ | ||
if (!$worker->process()) { | ||
return $jobsRan; | ||
} | ||
$jobsRan++; | ||
|
||
} | ||
|
||
return $jobsRan; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
|
||
namespace calderawp\calderaforms\cf2\Jobs; | ||
|
||
|
||
class XJob | ||
extends \calderawp\calderaforms\cf2\Jobs\Job { | ||
|
||
protected $x; | ||
public function __construct($x) | ||
{ | ||
$this->x = $x; | ||
} | ||
|
||
public function handle() | ||
{ | ||
wp_insert_post(['post_type' => 'post', 'post_status' => 'publish', 'post_content' => 'fasfsd', 'post_title' => 'job1' . $this->x ] ); | ||
} | ||
} |
Oops, something went wrong.