Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:38:07+00:00 2026-06-14T21:38:07+00:00

Hello i have a browsergame where various players schedule actions that can last for

  • 0

Hello i have a browsergame where various players schedule actions that can last for example one or 2 hours. A cronjob every minutes checks all the actions that are finished (endtime <= unixtime() ) and completes them (giving rewards to the related player etc.);

It happened recently that there were – say – 100 actions to complete and the cronjob task 1 didn’t finish in a minute, so the cronjob task 2 fired and the result was that
all the actions where completed two times.

How can i avoid this to happen again? I must use a specific transaction isolation code for the session and reserve the rows for update?

PHP 5.3.18
mysql is 5.5.27
Table Engine is INNODB

The current code called every minute is this:

public function complete_expired_actions ( $charflag = false )
{       
    // Verifying if there are actions to complete...

    $db = Database::instance();
    $db -> query("set autocommit = 0");
    $db -> query("begin");

    $sql = "select * from 
        character_actions
        where status = 'running' 
        and endtime <= unix_timestamp()"; 

    $result = $db -> query ( $sql ) ;               

    // try-catch. Se si verifica un errore, l' azione che commette l' errore viene rollbackata

    foreach ( $result as $row )
    {
        try 
        {

            $o = $this->factory( $row -> action );
            $o -> complete_action ( $row );


            if ($row -> cycle_flag == FALSE)
            { 
                // non aperta ad attacchi SQl-injection perchè i parametri non sono passati via request
            if ( $charflag == true )
                    kohana::log( 'info', "-> Completing action: " . $row -> id . ' - ' . $row -> action . " for char: " . $row->character_id );

                $db->query( "update character_actions set status = 'completed' where id = " . $row->id ); 
                // in ogni caso invalida la sessione!
                Cache_Model::invalidate( $row -> character_id );                                                                    
            }

            $db->query('commit');

            } catch (Kohana_Database_Exception $e)
            {
                kohana::log('error', kohana::debug( $e->getMessage() ));
                kohana::log('error', 'An error occurred, rollbacking action:' .  $row->action . '-' . $row->character_id );
                $db->query("rollback");         
            }   

    }       

    $db->query("set autocommit = 1");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:38:08+00:00Added an answer on June 14, 2026 at 9:38 pm

    In this cases I use a new column eg: mark to the table character_actions

    then at the start of the job I call this query:

    $uniqueid = time();
    $sql="update character_actions set mark = '$uniqueid' where status = 'running' and endtime <= unix_timestamp() AND mark is NULL "; 
    

    then you code can be

    $sql = "select * from 
            character_actions
            where status = 'running' 
            and mark = '$uniqueid'"; 
    
        $result = $db -> query ( $sql ) ;   
    

    This approach has a limit, it’s start different parallel works, that slow the machine that induce more delay and more parallel works…

    Can be solved introducing a limit:

    $lim= 100 ; // tune to finish the job in 60 seconds
    $sql="update character_actions set mark = '$uniqueid' where status = 'running' and endtime <= unix_timestamp() AND mark is NULL limit $lim  "; 
    

    Of course induce a delay in points attribution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello have following strange issue, can vertical center the menu text if it's one
Hello I have a tab widget that extends the customer edit page. In one
hello I have my code that connects to my ftp server $conn_id = ftp_connect($ftp_server);
Hello I have a datatable that I would like to filter with a single
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
Hello i have been trying to find a tutorial that show how to setup
Hello i have this foreach loop that gives weird result , it displays only
hello I have a page ( http://schema.org/WebPage ) that contains a review ( http://schema.org/Review
Hello I have one question about using genetic in correct way. please look at
Hello i have that grid pane and there is labels and textboxes in that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.