สอบถามการใช้ cron ใน phpBB ครับ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

flook
PHP VIP Members
PHP VIP Members
โพสต์: 3751
ลงทะเบียนเมื่อ: 06/06/2022 9:43 am

สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย flook »

ผลอยาก set เวลาทำงานของ phpBB ต้องทำอย่างไรบ้างครับ เช่น ต้องการให้ทำงาน ทุก ๆ 3 ชม หรือ 6 ชม ต่อวัน
ภาพประจำตัวสมาชิก
eange08
PHP VIP Members
PHP VIP Members
โพสต์: 16184
ลงทะเบียนเมื่อ: 22/12/2020 10:09 am

Re: สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย eange08 »

ลองดูการเขียน cron เพื่อให้รันทำงาน เรากำหนดช่วงเวลาทำงานตามความถี่ที่เรากำหนดตามค่า config
viewtopic.php?t=88326
flook
PHP VIP Members
PHP VIP Members
โพสต์: 3751
ลงทะเบียนเมื่อ: 06/06/2022 9:43 am

Re: สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย flook »

eange08 เขียน: 13/09/2022 10:58 am ลองดูการเขียน cron เพื่อให้รันทำงาน เรากำหนดช่วงเวลาทำงานตามความถี่ที่เรากำหนดตามค่า config
viewtopic.php?t=88326
สอบถามเพิ่มเติมครับ
การ debug ค่า เราจะ debug ยังไงครับของ
หรือจะรู้ได้ไงครับว่าเข้ามาทำงานอยู่

โค้ด: เลือกทั้งหมด


<?php

/**
 *
 *  An extension for the phpBB Forum Software package.
 *
 * @copyright (c) 2020, Gregor Morrill, https://stevens-stevens.com
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 */

namespace mindphp\m_dashboard\cron\task;


class time_dashboard_data extends \phpbb\cron\task\base
{
    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\log\log */
    protected $log;

    /**
     * Constructor
     *
     * @param \phpbb\config\config $config Config object
     */
    public function __construct(\phpbb\user $user, \phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\db\driver\driver_interface $db, \phpbb\log\log $log, $table_prefix, $phpbb_root_path, $php_ext)
    {
        $this->user = $user;
        $this->config = $config;
        $this->config_text = $config_text;
        $this->db = $db;
        $this->log = $log;
        $this->table_prefix = $table_prefix;
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
    }

    /**
     * Runs this cron task.
     *
     * @return void
     */
    public function run() //ส่วนนี้ใส่การทำงานของ cron เราว่าจะให้ทำอะไร เช่น เพิ่ม - แก้ไข - ลบ ตามที่เราต้องการ และถ้าจะให้รันรอบต่อไปให้เราบันทึกวันเวลาล่าสุดเอาไว้ (ลงท้าย _gc)
    {
        // echo "test";
        // exit();
        $sql = "INSERT INTO `test_001` (`id`, `test`) VALUES (NULL, '1');";
        $this->db->sql_query($sql);
        // $this->example1();
        $this->log->add('admin', 2, '127.0.0.1', 'Update example sucess - ' . time());
        // $today_run_set = '';
        // $this->config->set('time_dashboard_data_last_gc', $today_run_set, true);
    }

    /**
     * Returns whether this cron task can run, given current board configuration.
     *
     * For example, a cron task that prunes forums can only run when
     * forum pruning is enabled.
     *
     * @return bool
     */
    public function is_runnable() //ช็คว่าถึงเวลาแล้วเริ่มรันได้เลย
    {
        return true;
    }

    /**
     * Returns whether this cron task should run now, because enough time
     * has passed since it was last run.
     *
     * @return bool
     */
    public function should_run()  //ส่วนที่เช็คว่าใกล้ถึงเวลารันตามที่คำนวณวันเวลาถัดไปที่จะรัน เพื่อเข้าคิวรอรัน
    {
        return $this->config['time_dashboard_data_last_gc'] < time() - $this->config['time_dashboard_data_gc'];
    }

    // public function example1() //สามารถเขียน method เพิ่มเติมได้
    // {
    //     $this->log->add('admin', 2, '127.0.0.1', 'Update example1' . time());
    // }
}

ภาพประจำตัวสมาชิก
eange08
PHP VIP Members
PHP VIP Members
โพสต์: 16184
ลงทะเบียนเมื่อ: 22/12/2020 10:09 am

Re: สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย eange08 »

flook เขียน: 13/09/2022 3:19 pm
eange08 เขียน: 13/09/2022 10:58 am ลองดูการเขียน cron เพื่อให้รันทำงาน เรากำหนดช่วงเวลาทำงานตามความถี่ที่เรากำหนดตามค่า config
viewtopic.php?t=88326
สอบถามเพิ่มเติมครับ
การ debug ค่า เราจะ debug ยังไงครับของ
หรือจะรู้ได้ไงครับว่าเข้ามาทำงานอยู่

โค้ด: เลือกทั้งหมด


<?php

/**
 *
 *  An extension for the phpBB Forum Software package.
 *
 * @copyright (c) 2020, Gregor Morrill, https://stevens-stevens.com
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 */

namespace mindphp\m_dashboard\cron\task;


class time_dashboard_data extends \phpbb\cron\task\base
{
    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\log\log */
    protected $log;

    /**
     * Constructor
     *
     * @param \phpbb\config\config $config Config object
     */
    public function __construct(\phpbb\user $user, \phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\db\driver\driver_interface $db, \phpbb\log\log $log, $table_prefix, $phpbb_root_path, $php_ext)
    {
        $this->user = $user;
        $this->config = $config;
        $this->config_text = $config_text;
        $this->db = $db;
        $this->log = $log;
        $this->table_prefix = $table_prefix;
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
    }

    /**
     * Runs this cron task.
     *
     * @return void
     */
    public function run() //ส่วนนี้ใส่การทำงานของ cron เราว่าจะให้ทำอะไร เช่น เพิ่ม - แก้ไข - ลบ ตามที่เราต้องการ และถ้าจะให้รันรอบต่อไปให้เราบันทึกวันเวลาล่าสุดเอาไว้ (ลงท้าย _gc)
    {
        // echo "test";
        // exit();
        $sql = "INSERT INTO `test_001` (`id`, `test`) VALUES (NULL, '1');";
        $this->db->sql_query($sql);
        // $this->example1();
        $this->log->add('admin', 2, '127.0.0.1', 'Update example sucess - ' . time());
        // $today_run_set = '';
        // $this->config->set('time_dashboard_data_last_gc', $today_run_set, true);
    }

    /**
     * Returns whether this cron task can run, given current board configuration.
     *
     * For example, a cron task that prunes forums can only run when
     * forum pruning is enabled.
     *
     * @return bool
     */
    public function is_runnable() //ช็คว่าถึงเวลาแล้วเริ่มรันได้เลย
    {
        return true;
    }

    /**
     * Returns whether this cron task should run now, because enough time
     * has passed since it was last run.
     *
     * @return bool
     */
    public function should_run()  //ส่วนที่เช็คว่าใกล้ถึงเวลารันตามที่คำนวณวันเวลาถัดไปที่จะรัน เพื่อเข้าคิวรอรัน
    {
        return $this->config['time_dashboard_data_last_gc'] < time() - $this->config['time_dashboard_data_gc'];
    }

    // public function example1() //สามารถเขียน method เพิ่มเติมได้
    // {
    //     $this->log->add('admin', 2, '127.0.0.1', 'Update example1' . time());
    // }
}

เช็คจาก log ที่เราแทรกการทำงานเอาไว้ก็ได้ค่ะ

โค้ด: เลือกทั้งหมด

$this->log->add('admin', 2, '127.0.0.1', 'Update example sucess - ' . time());
หรือใช้ extension Cron Status ในข้อ 6 มาดูสถานะ cron ได้
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ได้คำตอบที่สามารถนำไปใช้งานได้หรือยังครับ
ไม่เข้าใจตรงไหนบ้าง
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
flook
PHP VIP Members
PHP VIP Members
โพสต์: 3751
ลงทะเบียนเมื่อ: 06/06/2022 9:43 am

Re: สอบถามการใช้ cron ใน phpBB ครับ

โพสต์ที่ยังไม่ได้อ่าน โดย flook »

eange08 เขียน: 13/09/2022 10:58 am ลองดูการเขียน cron เพื่อให้รันทำงาน เรากำหนดช่วงเวลาทำงานตามความถี่ที่เรากำหนดตามค่า config
viewtopic.php?t=88326
ไม่เข้าใจในาส่วนของ function นี้ครับ มีไว้ทำอะไร

โค้ด: เลือกทั้งหมด

 public function effectively_installed() {
        return isset($this->config['m_example_version']) && version_compare($this->config['m_example'], '1.0.0', '>=');
    }
แล้วก็ส่วนเวลาตอนรันครับ เหมือนถ้าไม่มีใครเข้าใช้งานส่วนหน้าเว็บเลยจะไม่มีการ รันในส่วนของ cron ใช่ไหมครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Bing [Bot] และบุคลทั่วไป 104