Jump to content

Queue on installOther()


Adriano Faria

Recommended Posts

Hello,

I have a Queue extension on installOther():

	public function installOther()
	{
		if( \IPS\Db::i()->checkForColumn( 'core_members', 'membersaway_on' ) )
		{
			\IPS\Task::queue( 'memberaway', 'Away', array(), 1 );
		}
	}

The queue itself:

class _Away
{
	/**
	 * @brief Number of members to run per cycle
	 */
	public $rebuild	= \IPS\REBUILD_NORMAL;

	/**
	 * Parse data before queuing
	 *
	 * @param	array	$data
	 * @return	array
	 */
	public function preQueueData( $data )
	{
		return $data;
	}

	/**
	 * Run Background Task
	 *
	 * @param	mixed						$data	Data as it was passed to \IPS\Task::queue()
	 * @param	int							$offset	Offset
	 * @return	int							New offset
	 * @throws	\IPS\Task\Queue\OutOfRangeException	Indicates offset doesn't exist and thus task is complete
	 */
	public function run( $data, $offset )
	{
		$select = \IPS\Db::i()->select( 'member_id, membersaway_on, membersaway_text, membersaway_days, membersaway_date', 'core_members', array( 'membersaway_on=?', 1 ), 'member_id ASC', array( $offset, $this->rebuild ) );

		if( !$select->count() )
		{
			throw new \IPS\Task\Queue\OutOfRangeException;
		}

		foreach( $select as $row )
		{
			\IPS\memberaway\Status::setMemberData( $row['member_id'], $row['membersaway_on'], $row['membersaway_text'], $row['membersaway_days'], $row['membersaway_date'] );
		}

		return $offset + $this->rebuild;
	}

	/**
	 * Get Progress
	 *
	 * @param	mixed					$data	Data as it was passed to \IPS\Task::queue()
	 * @param	int						$offset	Offset
	 * @return	array( 'text' => 'Doing something...', 'complete' => 50 )	Text explaining task and percentage complete
	 * @throws	\OutOfRangeException	Indicates offset doesn't exist and thus task is complete
	 */
	public function getProgress( $data, $offset )
	{
		return array( 'text' => \IPS\Member::loggedIn()->language()->addToStack( 'ma_rebuilding_members_data', FALSE ), 'complete'  => ( isset( $data['count'] ) AND $data['count'] ) ? ( round( 100 / $data['count'] * $offset, 2 ) ) : 100 );
	}

	/**
	 * Perform post-completion processing
	 *
	 * @param	array	$data
	 * @return	void
	 */
	public function postComplete( $data )
	{
		try
		{
			$pluginData = \IPS\Db::i()->select( '*', 'core_plugins', array( 'plugin_location=?', "memberaway" ) )->first();
			try
			{
				$plugin = \IPS\Plugin::load( $pluginData['plugin_id'] );
				$plugin->delete();
			}
			catch ( \Exception $e ){}
		}
		catch( \UnderflowException $e ){}
	}
}

\IPS\memberaway\Status::setMemberData() used in Queue:

	public static function setMemberData( $memberId, $status, $text, $days, $date )
	{
		\IPS\Db::i()->replace( 'memberaway_data',
			array(
				'ma_mid'	=> $memberId,
				'ma_on'		=> $status,
				'ma_text'	=> $text,
				'ma_days' 	=> $days,
				'ma_date'	=> $date
			)
		);
	}

Happens that the Queue gets stuck in 100% on dashboard:

5Yj0KYL.png

I don't think there's erros because if I run it manually by clicking in the link from the message above, it runs just fine. I also tried to change the priority in the Queue but no luck.

Is there anything wrong in my code?

Tks. 👍

No error in the logs.

Link to comment
Share on other sites

Hello,

return array( 
	'text' => \IPS\Member::loggedIn()->language()->addToStack( 'ma_rebuilding_members_data', FALSE ), 
	'complete' => ( isset( $data['count'] ) AND $data['count'] ) ? ( round( 100 / $data['count'] * $offset, 2 ) ) : 100 
);

I don't see where you defined $data['count']

So the condition looks like

return array( 
	'text' => \IPS\Member::loggedIn()->language()->addToStack( 'ma_rebuilding_members_data', FALSE ), 
	'complete' => 100 
);

always 100%

Link to comment
Share on other sites

Oh yes... my bad.

	/**
	 * Parse data before queuing
	 *
	 * @param	array	$data
	 * @return	array
	 */
	public function preQueueData( $data )
	{
		$data['count'] = \IPS\Db::i()->select( 'count(*)', 'core_members', array( 'membersaway_on=?', 1 ) )->first();
		return $data;
	}

Now I got stuck in 0%.

$data['count'] returns 1 in getProgress(), which is right. I have only 1 record in a test.

core_queue table:

b1o1tYz.png&key=5b5eefbf903c74dd8af2b9b2

The only difference now is that I changed the the key to Away instead of memberAway.

Manually it works just fine.

Link to comment
Share on other sites

The reason is that during installation the application is disabled and enabled on the finished step

installother.thumb.jpg.d533bbecb9fbf6decdfa35067df59bad.jpg

But the queue runs for enabled apps

enabled.thumb.jpg.1a14e5cb5e02035242cd5e661e1e12c3.jpg

So your queue is stopped


Enable your application before run queue


To IPS developers:

- Any reason why you use 

				$application->enabled	= 1;
				$application->save();

instead of 

				$application->_enabled	= 1;
				$application->save();

_enabled.thumb.jpg.f5a493d03719b77eb943e2a5b39ae10c.jpg

- The installHooks method calls twice

1 call

						/* Rebuild data */
						\IPS\Application::load( $data['key'] )->installJsonData();
	/**
	 * Rebuild common data during an install or upgrade. This is a shortcut method which
	 * * Installs module data from JSON file
	 * * Installs task data from JSON file
	 * * Installs setting data from JSON file
	 * * Installs ACP live search keywords from JSON file
	 * * Installs hooks from JSON file
	 * * Updates latest version in the database
	 *
	 * @param	bool	$skipMember		Skip clearing member cache clearing
	 * @return void
	 */
	public function installJsonData( $skipMember=FALSE )
	{
		................		
		/* Rebuild hooks */
		$this->installHooks();

2 call

				/* Install hooks - do this after enabling the application */
				$application->installHooks();

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...