Skip to content

Commit

Permalink
Create a proper job manager for deleting transients etc. (#2802)
Browse files Browse the repository at this point in the history
* #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
Josh Pollock authored and New0 committed Nov 15, 2018
1 parent e497b8e commit 9d76ad6
Show file tree
Hide file tree
Showing 411 changed files with 34,853 additions and 397 deletions.
22 changes: 18 additions & 4 deletions caldera-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function caldera_forms_load()
*/
do_action('caldera_forms_includes_complete');


caldera_forms_get_v2_container();
}

add_action('plugins_loaded', array('Caldera_Forms', 'get_instance'));
Expand Down Expand Up @@ -170,23 +170,37 @@ function caldera_forms_fallback_shortcode()
return $field;
},1,2);



/**
* Init Cf2 system
* Setup Cf2 con
*
* @since 1.8.0
*
* @TODO move this somewhere smarter
*/
add_action( 'caldera_forms_core_init', function(){
$container = new \calderawp\calderaforms\cf2\CalderaFormsV2();
add_action( 'caldera_forms_v2_init', function(\calderawp\calderaforms\cf2\CalderaFormsV2Contract $container){
$container->setCoreDir(CFCORE_PATH);

//Setup field types
$container->getFieldTypeFactory()
->add( new \calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType() );

//Add hooks
$container->getHooks()->subscribe();

//Register other services
$container->registerService(new \calderawp\calderaforms\cf2\Services\QueueService(),true );
$container->registerService(new \calderawp\calderaforms\cf2\Services\QueueSchedulerService(),true );

//Run the scheduler with CRON
/** @var \calderawp\calderaforms\cf2\Jobs\Scheduler $scheduler */
$scheduler = $container->getService(\calderawp\calderaforms\cf2\Services\QueueSchedulerService::class);
$running = $scheduler->runWithCron();

});





43 changes: 42 additions & 1 deletion cf2/CalderaFormsV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use calderawp\calderaforms\cf2\Fields\FieldTypeFactory;
use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi;

use calderawp\calderaforms\cf2\Services\ServiceContract;
class CalderaFormsV2 extends \calderawp\CalderaContainers\Service\Container implements CalderaFormsV2Contract
{

Expand Down Expand Up @@ -38,6 +38,40 @@ public function __construct()
});
}

/**
* Register a service with container
*
* @since 1.8.0
*
* @param ServiceContract $service The service to register
*
* @param boolean $isSingleton Is service a singleton?
*
* @return $this
*/
public function registerService( ServiceContract $service, $isSingleton ){
if (! $service->isSingleton()) {
$this->bind($service->getIdentifier(), $service->register($this) );
}else{
$this->singleton($service->getIdentifier(), $service->register($this) );
}
return $this;
}


/**
* Get service from container
*
* @since 1.8.0
*
* @param string $identifier
*
* @return mixed
*/
public function getService($identifier){
return $this->make($identifier);
}

/**
* Set path to main plugin file
*
Expand Down Expand Up @@ -105,4 +139,11 @@ public function getFieldTypeFactory()
{
return $this->make(FieldTypeFactory::class );
}

/** @inheritdoc */
public function getWpdb()
{
global $wpdb;
return $wpdb;
}
}
71 changes: 54 additions & 17 deletions cf2/CalderaFormsV2Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@

use calderawp\calderaforms\cf2\Fields\FieldTypeFactory;
use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi;
use calderawp\calderaforms\cf2\Services\ServiceContract;

interface CalderaFormsV2Contract
{

/**
* Get WordPress Transients API wrapper
*
* @since 1.8.0
*
* @return Cf1TransientsApi
*/
public function getTransientsApi();
/**
* Get WordPress Transients API wrapper
*
* @since 1.8.0
*
* @return Cf1TransientsApi
*/
public function getTransientsApi();

/**
* Get WordPress Plugins API manager
*
* @since 1.8.0
*
* @return Hooks
*/
public function getHooks();
/**
* Get WordPress Plugins API manager
*
* @since 1.8.0
*
* @return Hooks
*/
public function getHooks();


/**
Expand Down Expand Up @@ -57,4 +58,40 @@ public function getCoreDir();
* @return FieldTypeFactory
*/
public function getFieldTypeFactory();
}

/**
* Get global $wpdb
*
* @since 1.8.0
*
* @return \wpdb
*/
public function getWpdb();



/**
* Register a service with container
*
* @since 1.8.0
*
* @param ServiceContract $service The service to register
*
* @param boolean $isSingleton Is service a singleton?
*
* @return $this
*/
public function registerService( ServiceContract $service, $isSingleton );


/**
* Get service from container
*
* @since 1.8.0
*
* @param string $identifier
*
* @return mixed
*/
public function getService($identifier);
}
27 changes: 27 additions & 0 deletions cf2/Jobs/DatabaseConnection.php
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;
}
}
21 changes: 21 additions & 0 deletions cf2/Jobs/DeleteTransientJob.php
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);
}
}
10 changes: 10 additions & 0 deletions cf2/Jobs/Job.php
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
{

}
91 changes: 91 additions & 0 deletions cf2/Jobs/Scheduler.php
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;

}

}
20 changes: 20 additions & 0 deletions cf2/Jobs/XJob.php
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 ] );
}
}
Loading

0 comments on commit 9d76ad6

Please sign in to comment.