Jump to content
View in the app

A better way to browse. Learn more.

Invision Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[4.7.22] Task::postComplete() fails because $data is NULL

Featured Replies

I made a custom application with a Queue extension task for a client. Before calling it, I set a custom flag for a specific member, whose ID I also pass as the $data['member_id'] variable.

Then, in the postComplete() function, I reset the flag for the member once the task is done running:

	/**
	 * Perform post-completion processing
	 *
	 * @param	array	$data		Data returned from preQueueData
	 * @param	bool	$processed	Was anything processed or not? If preQueueData returns NULL, this will be FALSE.
	 * @return	void
	 */
	public function postComplete( $data, $processed = TRUE )
	{
		$data = json_decode( $data['data'], TRUE );
		
		# Once the task is complete update the member status to READY
		$member = Member::load( $data['member_id'] );
		$member->custom_flag_field = Suggestion::STATUS_READY;
		$member->save();
	}

The code above works just fine as long as there is any data to process for the member. However, when the member has nothing to parse, and preQueueData() returns NULL, the postComplete() function doesn't have any data on which member to reset the flag for.

The Task::queue() function should be updated to at least still pass the original $data variable values instead of NULL (or an empty array in v5). This is the current code:

		if ( method_exists( $extensions[ $key ], 'preQueueData' ) )
		{
			$class = new $extensions[ $key ];
			try
			{
				$data = $class->preQueueData( $data );
			}
			catch( \OutOfRangeException $e )
			{
				$data = NULL;
			}

			if ( $data === NULL )
			{
				if ( method_exists( $class, 'postComplete' ) )
				{
					$class->postComplete( $data, FALSE );
				}
				
				return;
			}
		}

Here's my suggested change:

		if ( method_exists( $extensions[ $key ], 'preQueueData' ) )
		{
			$class   = new $extensions[ $key ];
			$oldData = $data;
			try
			{
				$data = $class->preQueueData( $oldData );
			}
			catch( \OutOfRangeException $e )
			{
				$data = NULL;
			}

			if ( $data === NULL )
			{
				if ( method_exists( $class, 'postComplete' ) )
				{
					$class->postComplete( $oldData, FALSE );
				}
				
				return;
			}
		}

I store the original values in $oldData and pass that variable to both preQueueData() and postComplete().

If $data ends up being NULL, it properly passes the original values at least, if $data is a proper array instead, the code keeps processing everything else as usual.

===

The code is slightly different in v5, where an empty array is passed instead of NULL, but the change is also still relevant for it.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.