Jump to content

Lazy Cron problem


vxda
 Share

Recommended Posts

Hi i made a module for deleteing users that didnt confirmed their accounts so im using Lazy Cron for doing it. but its not dooing anything.
I made module file :
removeInactiveUsers.module

in it i have this :
 

<?php

class removeInactiveUsers extends WireData implements Module {

	 public static function getModuleInfo() {
        return array(
            'title' => 'Remove inactive users',
            'version' => 101,
            'summary' => 'Every X minutes removes inactive accounts',
            'singular' => true,
            'autoload' => true
            );
	}

	public function init() {
	    $this->addHook('LazyCron::everyMinute', $this, 'removeInactiveUsers');
	}

	public function removeInactiveUsers(HookEvent $e) {
//If field "user_activation" is not 0. (zero);
	    $inactiveUsers = $this->$users->find("!user_activation$=0"); 
	    if(count($inactiveUsers)){
	    	foreach ($inactiveUsers as $u) {
	    		$users->delete($u);
	    	}
	    }
	}
}

so i installed module and nothing happends.
Any tips of what im doing wrong ?

Ps. i have Lazy Cron module installed ;)

Link to comment
Share on other sites

Try replacing:

$users->delete($u);

with:

$this->users->delete($u);

Also, turn on debug mode so you get some error reporting - these should be showing up.

That did the job :) ty.

Here is my code:

<?php

class removeInactiveUsers extends WireData implements Module {

	 public static function getModuleInfo() {
        return array(
            'title' => 'Remove inactive users',
            'version' => 101,
            'summary' => 'Every X minutes removes inactive accounts',
            'singular' => true,
            'autoload' => true
            );
	}

	public function init() {
	    $this->addHook('LazyCron::everyMinute', $this, 'removeInactiveUsersFn');
	}

	public function removeInactiveUsersFn(HookEvent $e) {
	    $inactiveUsers = $this->users->find("!user_activation$=0");
	    if(count($inactiveUsers)){
	    	foreach ($inactiveUsers as $u) {
	    		$this->users->delete($u);
	    	}
	    }
	}
}

Ty for help guys

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hey, I was just googling for the lazycron documentation and this post popped up #2 so I figure I will throw my implementation of the exact same thing in this post in case anyone looking needs another idea. 

The only thing that differs in my code is that I compared the creation time to current to allow users an amount of time to validate their account. I also have it log so you can keep an eye on what your server was up to while you weren't looking. 

public function init() {
    $this->addHook('LazyCron::every10Minutes', $this, 'userPrune');
}

public function userPrune(HookEvent $e) {

	$usersToValidate = wire('pages')->find('template=user,include=hidden,active=0');
	
	if(count($usersToValidate)){
		$log = wire('log');			
		$now = time();
		$invalid = $now - (1*60*60);
				
		foreach($usersToValidate as $u){
			//if user was created longer than 2hrs ago then delete them
			if($u->created < $invalid){			
				$entry = date('Y-m-d',$now)." ".date('H:i:s',$now)." | ".$u->title." | ".$u->email."\r\n";
				$u->delete();
				$log->save('user-delete-log', $entry);
			}
		}
		
		
	}
} 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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