Simple site and 1C integration

    image
    Recently I came across several different articles about the integration of the site and 1C. In the comments, disputes about various approaches often started, and I decided to share the way that I once managed to implement to me. Of course, the method described below does not claim universality and uniqueness, but, I think, it will be useful to those who are just going to write their own version.


    Choppers, rakes and shovels


    Not a very large store (nomenclature of about 5000 products).
    Ordinary, file 1C 8 UT. The configuration is fully supported, and cannot be removed from support.
    The base lies on one of the desktop machines, which in turn is used for work by one of the employees.
    It is enough to unload a minimum of information: name, description, photo, several types of prices.
    Breaking an already established ecosystem was also highly undesirable.
    An ordinary virtual hosting is allocated for the site, which means that you need to take into account the limited resources of that.
    Unloading may not occur often, it is enough 1 time in 3 hours.
    Subsequently, it became necessary to add the unloading of the price list to xls.
    A modest budget for the implementation of the project.

    The machine acting as a “server” was already not fast, and the presence of constant connections to the 1C base only aggravated the situation. But this suited the customer, which means that there wasn’t a lot of money to buy a separate server and rearm the rest of the fleet.
    Due to budget constraints, the option with Bitrix and its typical integration disappeared immediately. And the interest was more likely sports, to realize everything independently. It was decided to use the frame previously used for the catalog of goods. The framework was made on CodeIgniter, so adding a small module was not difficult. It is resolved.

    Let’s start


    The first thing was the question of the frequency of uploading information, and his hands reached for the scheduled tasks ... but, stop. Firstly, it is impossible to remove the configuration from support, which means that we cannot make changes to the configuration itself, and secondly, all copies of 1C are launched only if necessary, which means whether 1C will be launched at the right time or not is unknown . Yes, it would be possible to oblige the customer to always start 1C when starting the “server” and always keep it running, but this will create unnecessary inconvenience to the customer, which means the solution is not the most convenient. Alas, scheduled tasks today will not be able to help us. Obviously, we will need the help of a third-party application to start the upload procedure at a certain point. Here we recall that 1C allows you to run yourself from the command line, moreover,

    Here are the main keys to be used:
    "Путь к 1С" enterprise  /F"Путь к базе" /N"Логин" /P"Пароль" /Execute"Внешняя обработка" /C"Параметр" /DisableStartupMessages


    Now it remains to configure the launch of this design on schedule. Windows Scheduler? We configure, check ... it works!
    But there is one serious flaw. When the scheduler launches 1C on a schedule, then, of course, it opens on top of all applications and if someone is working on the computer at that time, then, firstly, it will distract him, and secondly, he can close it new window. Out of order ... What to do? We begin to dig towards launch under another account. Thanks to Google, we quickly enough find the possibility of batch launch under another user. We create a new Windows user, allow batch launch in security policies, reconfigure the scheduler, check ... voila! Everything worked, but we did not see any annoying windows! Well, that means that this option suits us ... now we proceed to the actual unloading of the data itself.

    Unloading


    The first thing, of course, I began to look in the direction of CommerceML, but after reading the documentation, it became clear that for our, quite elementary unloading, to fence this whole garden - too long, and the budget is not rubber. So, we will look for an alternative way. Why not just upload text information to xml and upload images to a separate directory? It’s decided that we are doing it. We get a fairly simple structure of the xml-file: first, records for groups go inside, and then nomenclature units themselves.

    This is how the groups look:
    код /*Код-элемента из 1С*/
    		наименование /* Собственно, наименование группы*/
    		код родителя /* Если не пусто, значит группа является вложенной*/
    	


    And so the goods:
    код /* Код-элемента из 1С */
    		код родителя /* Код группы, к которой принадлежит товар */
    		наименованиеОписание
    Артикул
    Тип ценыЦена <Сurrency>валюта Остаткишт/кг/литрыimg_dae5eacd-7d88-11de-8856-0024213f1c89.jpg /* Все изображения содержат в названии уникальный идентификатор элемента в базе 1С */


    Not a tricky request, we get information on all product groups and products, then we form an xml-ku. We also make a separate selection and pull out the information for the price list, and then save the received information in xls.
    After that, we proceed to the unloading of images. Due to the limitations of the hosting site, processing the images on the hosting side did not seem the most rational, so it was decided to do image processing + creating a preview at the stage of forming the upload. 1C is certainly a harvester, but I did not find the ability to process a pack of photos. But do we have ImageMagick? An excellent, cross-platform set of everything we need for image processing ... download, unpack ... it remains only to write a bat'nik that will perform the necessary manipulations:

    cd c:\
    set thePATH= Здесь указываем путь к изображениям
    FOR /R "%thePATH%" %%a IN (*.jpg) DO ImageMagick-6.7.0-10\convert.exe %%a -resize ШИРИНАxВЫСОТА^> -quality 70 %%a
    FOR /R "%thePATH%" %%a IN (*.jpg) DO ImageMagick-6.7.0-10\convert.exe %%a -resize ШИРИНАxВЫСОТА^> -gravity center -extent ШИРИНАxВЫСОТА -quality 70 %thePATH%mini\%%~nxa
    


    We start this bat'nik from our processing and we receive result.

    All information is uploaded, all that remains is to carefully pack it into zip and upload it via ftp to the server, which 1C does well with regular tools.

    At first glance it’s all, but ... Are we going to pull out the whole nomenclature every time, with all the pictures and upload all this colossus to the server? No, this picture is not for the faint of heart ... And what to do? Make flags and mark which item has changed? No, we cannot edit the configuration ... But are there any exchange plans? Who forbade us to use them? No one! We create an exchange plan, customize. Then we modify our upload, now during upload we create a new message in terms of exchange, upload only the units changed since the last message ... Great!

    Server side


    It's still more transparent here. We hang up the launch of the script on the crown. In the script, look at the desired directory, look for the desired archive using the mask. If there is one, then unpack, parse xml, write the information in the database. The pictures we have inside are another archive, we also unpack it, right on top of the directory with the existing pictures. If such a picture already exists, rewrite it with a new one. The xls-file is copied with the replacement in the desired directory. We are writing information to the database that they made such and such an update, then, with such and such a number, and then we clean the temporary files with us. All.

    Straws from failures


    If errors occur during the uploading of information, image processing, archiving, uploading to the remote FTP, we may lose some of the updates, and as a result, the information on the site and in the 1C database will be out of sync. Below I described the main sharp corners for which it was necessary to lay the straws:

    1. So, what will happen if, for example, at the time of loading via FTP, the connection breaks?
    If we have already written a message to the exchange plan, it turns out that 1C believes that it successfully uploaded these changes and now collects new ones, but they didn’t get to the site! That's right, that's why we finish writing to the exchange plan only after the successful completion of all stages of the upload, including uploading files via FTP! And if an exception occurs during the upload process, then we interrupt the message recording in the exchange plan and write an error in error-log.

    2. And what if our script on the server starts processing an unloaded archive?
    We first give it one name (for example, export.zip_), and only at the end we rename it and give the name that the script will look for on the server.

    3. And what if the server crashes and the script does not have time to process our archive, 1C will overwrite it with a new one?
    No, for this, each archive contains the message number from the exchange plan in the name (for example, export_1.zip). The script on the server, in turn, upon detection of several archives processes them in ascending order of numbers.

    4. And the logs will not overflow?
    Because when unloading, information about the results of each action is written in detail to the log, then the logs grow quite quickly, so do not forget to control their size at each unloading and delete old ones if necessary.

    5. And what if 1C does not have time to unload, and at this time the process starts for a new unload?
    In the scheduler settings, configure so that the new task will not be executed if the previous one is not completed.

    PS


    So, in such a simple way for a fairly short period of time was implemented the unloading of items / prices / balances on the site. Of course, this option has a number of shortcomings and does not claim to be the only true laurels, but at the same time we managed to fully fit into the restrictions set by the customer’s ecosystem. All this design works on weak machines, it does not require a constantly running copy of 1C, it works on the most common virtual hosting, absolutely does not distract employees and most importantly, it allows you to easily maintain 1C. The straw under the bed turned out to be enough, and for almost a year, fortunately, not a single one of our intervention in the system was required.

    Also popular now: